Queue in C.

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

 OUTPUT :
 
 
  Queue Implementation
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 1
 
  Enter the number to be inserted : 10
 
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 1
 
  Enter the number to be inserted : 20
 
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 1
 
  Enter the number to be inserted : 30
 
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 3
    10   20   30
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 2
 
 
  The delete_itemd item is 10
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 3
  20   30
 
  Main Menu
  1 : Insert
  2 : Delete
  3 : Display
  4 : Exit
  Select Operation : 4