-
Notifications
You must be signed in to change notification settings - Fork 2
/
QUEUE.CPP
105 lines (95 loc) · 1.79 KB
/
QUEUE.CPP
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
96
97
98
99
100
101
102
103
104
105
#include<iostream.h>
#include<conio.h>
#define MAX 10
class Queue
{
int front,rear;
int queue[MAX];
public:
Queue()
{
front=rear=-1;
}
void qdisplay(void);
void qpush(int item)
{
if(rear==MAX-1)
{
cout<<"\nQUEUE OVERFLOW";
}
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=item;
cout<<"\nITEM INSERTED: "<<item;
}
else
{
rear++;
queue[rear]=item;
cout<<"\nITEM INSERTED: "<<item;
}
}
void qpop()
{
int item;
if(rear==-1)
{
cout<<"\nQUEUE UNDERFLOW";
}
else if(front==0 && rear==0)
{
item=queue[front];
front=rear=-1;
cout<<"\n\nITEM DELETED: "<<item;
}
else
{
item=queue[front];
front++;
cout<<"\n\nITEM DELETED: "<<item;
}
}
};
void Queue::qdisplay(void)
{
if(front==-1)
{
cout<<"\n\nQUEUE IS EMPTY\n";
}
else
{
cout<<"\n\nQUEUE ITEMS\n";
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<" ";
}
cout<<endl;
}
}
void main()
{
Queue q;
int i,ch;
clrscr();
do{
cout<<"\nQueue operations : \n1.Push\n2.Pop\n3.Display\n4.Exit";
cout<<"\nEnter the option : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the item to be inserted : ";
cin>>i;
q.qpush(i);
break;
case 2:q.qpop();
break;
case 3:q.qdisplay();
break;
case 4:cout<<"\nThanks for using";
break;
default:cout<<"\nInvalid option ";
}
}while(ch!=4);
getch();
}