473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 12284
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

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

Similar topics

6
by: herrcho | last post by:
#include <stdio.h> #define MAX 10 int queue; int front, rear; void init_queue() { front=rear=0; }
2
by: Bob Jenkins | last post by:
I have this cute data structure that I just found a second opportunity to use. I googled for references to it, but came up empty. I probably just don't know its name. The algorithm on this...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
2
by: William Stacey | last post by:
Working with implementing a circular buffer for a producer/consumer deal. Have not done one in a while. The following seems to work. However, I remember and have seen other implementation that...
10
by: avsrk | last post by:
Hi Folks I want to create a circular queue for high speed data acquistion on QNX with GNU C++ . Does any one know of efficient ways either Using STL or in any other way . I am thing of ...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
2
by: marco.furlan | last post by:
Hi there, I have to write an optimized circular buffer to log events in C++. The elements are of type std::string. This should run under Linux on an ARM embedded system. I can dedicate to this...
5
by: shanknbake | last post by:
Here is my code. I've noted where the program crashes. I'm doing this program as a project for school. //cqueue.h file //HEADER FILE http://rafb.net/paste/results/Nh0aLB77.html...
2
by: lavender | last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10. Where is the wrong in my code? Why it cannot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.