0% found this document useful (0 votes)
75 views

Circular Queue

The document defines a circular queue data structure with functions to initialize, display, check if empty/full, push, and pop elements. It implements a circular queue using a fixed-size array, with front and rear pointers. The main function demonstrates using the circular queue by pushing and popping elements and displaying the queue after each operation.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Circular Queue

The document defines a circular queue data structure with functions to initialize, display, check if empty/full, push, and pop elements. It implements a circular queue using a fixed-size array, with front and rear pointers. The main function demonstrates using the circular queue by pushing and popping elements and displaying the queue after each operation.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 /*RAHUL UPPALWAR*/

2 #include<stdio.h>
3 #define MAX 10
4 struct cqueue{
5 int front;
6 int rear;
7 int count;
8 int arr[MAX];
9 };
10  
11 int initial(struct cqueue *q){
12 q->front = 0;
13 q->rear  = 0;
14 q->count = 0;
15 }
16  
17 int display(struct cqueue q){
18 int i;
19 int count = 0;
20 printf("\n\n");
21 if(q.count == 0){
22 printf("UNDERFLOW");
23 }else{
24 if(q.rear > q.front){
25 for(i = q.front ; i< q.rear ; i++){
26 printf("%5d",q.arr[i]);
27 }
28 }else{
29 for(i = q.front ; i < MAX ; i++){
30 printf("%5d",q.arr[i]);
31 }
32 for(i = 0 ; i <= q.rear ; i++){
33 printf("%5d",q.arr[i]);
34
35 }
36 }
37 }
38 printf("\n");
39 }
40  
41 int empty(struct cqueue q){
42 if(q.count == 0){
43 return 1;
44 }else{
45 return 0;
46 }
47 }
48  
49 int full(struct cqueue q){
50 if(q.count == MAX){
51 return 1;
52 }else{
53 return 0;
54 }
55 }
56
57 int push(struct cqueue *q , int no){
58 if(q->count == MAX){
59 printf("OVERFLOW");
60 }else{
61 q->arr[q->rear] = no;
62 q->rear = (q->rear +1) % MAX;
63 q->count++;
64 }
65 }
66  
67 int pop(struct cqueue *q){
68 int popno;
69 if(q->count == 0){
70 printf("UNDERFLOW");
71 return 0;
72 }else{
73 popno = q->arr[q->front];
74 q->front = (q->front + 1) % MAX;
75 q->count--;
76 return popno;
77 }
78 }
79  
80 int main(){
81 struct cqueue q;
82 initial(&q);
83 push(&q, 10);
84 push(&q, 20);
85 push(&q, 30);
86 push(&q, 40);
87 push(&q, 50);
88 push(&q, 60);
89 push(&q, 70);
90 push(&q, 80);
91 push(&q, 90);
92 display(q);
93 printf("\n\n");
94 int pn;
95 pn = pop(&q);
96 printf("\nDeleted Element = %d",pn);
97 display(q);
98 pn = pop(&q);
99 printf("\nDeleted Element = %d",pn);
100 display(q);
101 pn = pop(&q);
102 printf("\nDeleted Element = %d",pn);
103 display(q);
104 pn = pop(&q);
105 printf("\nDeleted Element = %d",pn);
106 display(q);
107 pn = pop(&q);
108 printf("\nDeleted Element = %d",pn);
109 display(q);
110 pn = pop(&q);
111 printf("\nDeleted Element = %d",pn);
112 display(q);
113 }

You might also like