WELCOME TO MY BLOG BY SHABEEB.K

C,C++ PROGRAMMING: 11/26/13

Search This Blog

Tuesday, 26 November 2013

QUEUE


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream.h>
#include<conio.h>
int front=0,rear=0; //initializing varible for needed  operations in queue.
int flag=0;
int i,size;

int q[10];
class qu
{
public:


void insert()  //Function to insert elements into queue.
{

if(rear>=size)    //insertion is not possible if rear met to its maximum size of queue,
{
cout<<"QUEUE IS FULL\n";
}
               //element can be inserted if rear less than size of queue
else
{
rear++;  //increment rear by 1.
cout<<"ENTER ELEMENT TO ADD\n";
cin>>q[rear];               //element is stored on q[rear] and rear vlue is incremented by 1 for each insertion until rear become equal to size.
}
}

void dele()   // function to deletion is possible only if there is atlest 1 element in queue. that is rear>0.
{
if(rear>0)
{
front++;
q[front]=NULL;

for(i=1;i<=rear;i++)
{
q[i]=q[i+1];   //deletion of an front element in queue will leads to a NULL value in front so rewrite all the element back to avoid NULL space.
}
rear--;         //decrement rear by 1.
front--;            //decrement front by 1.
}
else
{
cout<<"QUEUE IS EMPTY\n";
}
}

void display()      //function to disply element in the queue.
{

if(rear>0)
{
for(i=1;i<=rear;i++)
{
cout<<q[i]<<",";
}
}
else
{
cout<<"QUEUE IS EMPTY\n";
}
}
};

void main()
{
int ch;

qu s;

if(flag==0)
{
clrscr();
cout<<"ENTER ARRAY SIZE\n";  //recieve size of queue from user.
cin>>size;
flag=1;
}

cout<<"\n\n1:INSERT\n2:DELETE\n3:DISPLAY\n";
cin>>ch;
switch(ch)  //choice for user to select their function to insert,delete and display
{
case 1 : s.insert(); main();
break;
case 2 : s.dele(); main();
break;
case 3 : s.display(); main();
break;
default : cout<<"INVALID KEY VALUE\n";
break;
}

getch();
}