Circular queue in C.

 

01 #include<stdio.h>
02 #include<stdlib.h>
03
04 #define size 5
05
06 /*global variable*/
07
08 int queue[size];
09 int front=-1;
10 int rear =0;
11
12
13 int queueFull()
14 {
15 if(front==(rear+1)%size)
16 return 1;
17 else
18 return 0;
19 }
20
21 int queueEmpty()
22 {
23 if(front ==-1)
24 return 1;
25 else
26 return 0;
27 }
28
29 void insert_item(int Item)
30 {
31 if(front ==-1)
32 front=rear=0;
33 else
34 rear =(rear+1)%size;
35 queue[rear]=Item;
36
37 }
38
39
40 void delete_item()
41 {
42 int Item;
43
44 Item=queue[front];
45 if(front ==rear)
46 front=rear=-1;
47 else
48 front =(front+1)%size;
49 printf("n The deleted item is %d",Item);
50
51 }
52
53 void display()
54 {
55 int i;
56 if(queueEmpty())
57 {
58 printf("nThe Queue Is Empty");
59 return;
60 }
61 i=front;
62 while(i!=rear)
63 {
64 printf(" %d",queue[i]);
65 i=(i+1)%size;
66 }
67 printf(" %d",queue[i]);
68 }
69
70 int main()
71 {
72 int ch,item;
73
74 printf("n Circular Queue Implementation");
75 do
76 {
77 printf("nn Main Menu");
78 printf("n 1 : Insert");
79 printf("n 2 : Delete");
80 printf("n 3 : Display");
81 printf("n 4 : Exit");
82
83 printf("n Select Operation : ");
84 scanf("%d",&ch);
85
86 switch(ch)
87 {
88 case 1:
89 if(queueFull())
90 {
91 printf("n Queue is Full");
92 }
93
94 else
95 {
96 printf("n Enter the number to be inserted : ");
97 scanf("%d",&item);
98 insert_item(item);
99 }
100 break;
101
102 case 2:
103 if(queueEmpty())
104 {
105 printf("n Queue is Empty");
106 }
107 else
108 {
109 delete_item();
110 }
111 break;
112
113 case 3:
114 if(queueEmpty())
115 {
116 printf("nQueue is Empty");
117 }
118 else
119 {
120 display();
121 }
122 break;
123
124 case 4 :
125 printf("n End of Program...");
126
127 }
128
129 }
130 while(ch!=4);
131 return 0;
132 }
 
 OUTPUT :


Circular Queue Implementation

Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 1

Enter the number to be inserted : 50


Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 1

Enter the number to be inserted : 100


Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 1

Enter the number to be inserted : 150


Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 3

50 100 150

Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 2

The deleted item is 50

Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 3
100 150

Main Menu
1 : Insert
2 : Delete
3 : Display
4 : Exit
Select Operation : 4

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.