468,512 Members | 1,485 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

circular queue implementation in array

I have a problem in imploementation of circular queue through array in C language. Please send me the code of functions for insertion, deletion and display the elements of array.

Plz send me quick reply if you know the solution.
Oct 6 '06 #1
2 12022
send the code which you have tried..
Oct 6 '06 #2
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]
Oct 6 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by herrcho | last post: by
2 posts views Thread by Bob Jenkins | last post: by
6 posts views Thread by T Koster | last post: by
2 posts views Thread by William Stacey | last post: by
10 posts views Thread by avsrk | last post: by
7 posts views Thread by toton | last post: by
2 posts views Thread by marco.furlan | last post: by
5 posts views Thread by shanknbake | last post: by
2 posts views Thread by lavender | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.