I wrote this badly but funda remains same the--> % operator to go to correct place in array. I have not tested it properly.. but some work reqd by you :)
[HTML] 1 #include<stdio.h>
2 #include<assert.h>
3
4 #define SIZE 10
5
6
7 struct Queue {
8 char arr[SIZE] ;
9 int front;
10 int back ;
11 int free ;
12 short q_init ;
13 short isEmpty;
14 };
15
16 struct Queue* InitQueue();
17 int insert( struct Queue *q, char val);
18 char pop(struct Queue *q );
19
20 int main(){
21 struct Queue *qPtr = InitQueue();
22 char res= 0;
23 insert(qPtr,'D');
24 insert(qPtr,'N');
25 insert(qPtr,'A');
26 insert(qPtr,'B');
27 insert(qPtr,'U');
28 pop(qPtr);
29 insert(qPtr,'R');
30 insert(qPtr,'S');
31 insert(qPtr,'T');
32 insert(qPtr,'B');
33 printf("----------------\n");
34 while ( !(qPtr->isEmpty) ){
35 printf("Got %c\n",pop(qPtr));
36 }
37 printf("----------------\n");
38 insert(qPtr,'H');
39 insert(qPtr,'E');
40 insert(qPtr,'L');
41 insert(qPtr,'O');
42 printf("----------------\n");
43 while ( !(qPtr->isEmpty) ){
44 printf("Got %c\n",pop(qPtr));
45 }
46 printf("----------------\n");
47 return(0);
48 }
49
50
51 struct Queue* InitQueue(){
52 static struct Queue queue;
53 queue.front = 0;
54 queue.back= 0;
55 queue.free = SIZE;
56 queue.q_init = 1;
57 queue.isEmpty = 1;
58 return &queue;
59 }
60
61 int insert( struct Queue *q, char val){
62 assert(q->q_init == 1) ;
63 assert(q->free > 0);
64 q->arr[q->back] = val;
65 q->free = q->free -1 ;
66 q->back = (q->back + 1) % SIZE;
67 q->isEmpty = 0;
68 return(0);
69 }
70
71 char pop(struct Queue *q ){
72 char ret = 0;
73 assert(q->free != SIZE);
74 q->free = q->free + 1;
75 ret = q->arr[q->front];
76 q->front = (q->front +1 )%SIZE;
77 if (q->front == q->back ) q->isEmpty = 1;
78 return (ret);
79 }[/HTML]
I got output :
[PHP][jsingh]:/home/jsingh% !cc
cc q.c
[jsingh]:/home/jsingh% ./a.out
----------------
Got N
Got A
Got B
Got U
Got R
Got S
Got T
Got B
----------------
----------------
Got H
Got E
Got L
Got O
----------------
[jsingh]:/home/jsingh% less q.c[/PHP]