-
Notifications
You must be signed in to change notification settings - Fork 1
/
8. Circular Queue.cpp
99 lines (92 loc) · 1.85 KB
/
8. Circular 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
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <windows.h>
#define MAX 5
struct queue
{
int arr[MAX];
int f,r,count;
};
void insq(struct queue *,int);
void delq(struct queue *);
void display(struct queue *);
void main()
{
int ele,ch;
struct queue q;
q.f=q.r=q.count=0;
while(true)
{
system("cls");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter Element To Insert : ");
scanf("%d",&ele);
insq(&q,ele);
break;
case 2:delq(&q);
break;
case 3:display(&q);
break;
case 4:exit(0);
break;
default:printf("Invalid Selection");
}
getch();
}
}
void insq(struct queue *q,int ele)
{
if(q->count==MAX)
{
printf("Queue Full");
return;
}
q->arr[q->r]=ele;
q->r=(q->r+1)%MAX;
q->count++;
}
void delq(struct queue *q)
{
if(q->count==0)
{
printf("Queue Empty");
return;
}
printf("Element Deleted : %d",q->arr[q->f]);
q->f=(q->f+1)%MAX;
q->count--;
}
void display(struct queue *q)
{
int i,t;
if(q->count==0)
{
printf("Empty Queue");
return;
}
t=q->f;
for(i=0;i<q->count;i++)
{
printf("%d ",q->arr[t]);
t=(t+1)%MAX;
}
}
/*
License
=======
Copyright (C) 2011 SkyKOG
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/