-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment7.cpp
123 lines (120 loc) · 1.97 KB
/
experiment7.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#define size 5
using namespace std;
class node
{
public:
node()
{
next=NULL;
}
int data;
node *next;
}*front=NULL,*rear=NULL;
class circular_queue
{
public:
void Enqueue();
void dequeue();
void disp();
};
int c=1;
void circular_queue :: Enqueue()
{ if(c>size)
{
cout<<"Overflow\n";
}
else
{
int ele;
cout<<"Enter Element:\n";
cin>>ele;
node *temp=new node;
temp->data=ele;
temp->next=front;
if(front==NULL&& rear==NULL)
{
front=temp;
temp->next=front;
rear=temp;
c=c+1;
}
else
{
node *p;
p=front;
while(p->next!=front)
{
p=p->next;
}
p->next=temp;
c=c+1;
rear=temp;
temp->next=front;
}
}
}
void circular_queue :: dequeue()
{
if(front==NULL && rear==NULL)
{
cout<<"Underflow\n";
}
else if(front->next==front)
{
front=NULL;
rear=NULL;
c--;
}
else
{
front=front->next;
rear->next=front;
c--;
}
}
void circular_queue :: disp()
{
node *p;
if(front==NULL&&rear==NULL)
{
cout<<"Link List is empty\n";
}
else
{
p=front;
while(p->next!=front)
{
cout<<p->data<<" -> ";
p=p->next;
}
cout<<p->data<<" -> ";
cout<<p->next->data<<endl;
}
}
int main()
{
int n,ch;
circular_queue q;
cout<<"Enter Size: ";
cin>>n;
for(int i=0;i<n;i++)
{
q.Enqueue();
}
do
{
cout<<"1) Enqueue\n2) Dequeue\n3) Display\n4) Exit\n";
cin>>ch;
switch(ch)
{
case 1: q.Enqueue();
break;
case 2 : q.dequeue();
break;
case 3 : q.disp();
break;
}
}while(ch!=4);
return 0;
}