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

Queue

The document defines functions for implementing a queue data structure using an array, including initializing the queue, displaying its contents, checking if it is empty or full, pushing elements to the rear, and popping elements from the front. It defines a queue struct with front and rear pointers and an array. The main function tests the queue by pushing and popping elements and displaying the results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Queue

The document defines functions for implementing a queue data structure using an array, including initializing the queue, displaying its contents, checking if it is empty or full, pushing elements to the rear, and popping elements from the front. It defines a queue struct with front and rear pointers and an array. The main function tests the queue by pushing and popping elements and displaying the results.
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 queue{
5 int front;
6 int rear;
7 int arr[MAX] ;
8 };
9  
10 int initial(struct queue *q){
11 q->front = 0;
12 q->rear  = 0;
13 }
14  
15 int display(struct queue q){
16 int i;
17 for(i = q.front ; i<= q.rear ; i++){
18 printf("%5d",q.arr[i]);
19 }
20 printf("\n");
21 }
22  
23 int empty(struct queue q){
24 if(q.rear == q.front){
25 return 1;
26 }else{
27 return 0;
28 }
29 }
30  
31 int full(struct queue q){
32 if(q.rear == MAX){
33 return 1;
34 }else{
35 return 0;
36 }
37 }
38  
39 /*Inserted from rear & deleted from top {First-In-First-Out}*/
40 int push(struct queue *q , int no){
41 if(q->rear == MAX){
42 printf("OVERFLOW");
43 }else{
44 q->arr[q->rear] = no;
45 q->rear++;
46 }
47 }
48  
49 int pop(struct queue *q){
50 int popno;
51 if(q->front == q->rear){
52 printf("UNDERFLOW");
53 return 0;
54 }else{
55 popno = q->arr[q->front];
56 q->front++;
57 return popno;
58 }
59 if(q->front == q->rear){
60 q->front =0;
61 q->rear = 0;
62 }
63 }
64  
65 int main(){
66 struct queue q;
67 initial(&q);
68 push(&q, 10);
69 push(&q, 20);
70 push(&q, 30);
71 push(&q, 40);
72 push(&q, 50);
73 push(&q, 60);
74 push(&q, 70);
75 push(&q, 80);
76 push(&q, 90);
77 push(&q, 100);
78 display(q);
79 printf("\n\n");
80 int popno;
81 popno = pop(&q);
82 display(q);
83 printf("Poped Element = %5d",popno);
84 printf("\n\n");
85 popno = pop(&q);
86 display(q);
87 printf("Poped Element = %5d",popno);
88 printf("\n\n");
89 popno = pop(&q);
90 display(q);
91 printf("Poped Element = %5d",popno);
92 printf("\n\n");
93 popno = pop(&q);
94 display(q);
95 printf("Poped Element = %5d",popno);
96 printf("\n\n");
97 popno = pop(&q);
98 display(q);
99 printf("Poped Element = %5d",popno);
100 printf("\n\n");
101 popno = pop(&q);
102 display(q);
103 printf("Poped Element = %5d",popno);
104 printf("\n\n");
105 popno = pop(&q);
106 display(q);
107 printf("Poped Element = %5d",popno);
108 printf("\n\n");
109 popno = pop(&q);
110 display(q);
111 printf("Poped Element = %5d",popno);
112 printf("\n\n");
113 popno = pop(&q);
114 display(q);
115 printf("Poped Element = %5d",popno);
116 }

You might also like