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.
2 12314
send the code which you have tried..
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]
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: herrcho |
last post by:
#include <stdio.h>
#define MAX 10
int queue;
int front, rear;
void init_queue()
{
front=rear=0;
}
|
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 data structure typically has an array of
pointers, and in a loop refers to array .. array,
modifies array, then increments i. I do away with i by having
an array of size 2*n, where array = array. Then I use array2
instead of array, increment...
|
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 stumped again with
this little love triangle. Here is some background:
m_commands.h defines
- struct command_callbacks, which contains
- a struct conn * reference
- struct command, which contains
- a struct command_callback reference
|
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 make the buffer 1 more then
the needed capacity so that a full buffer and an empty buffer can be
differentiated. However this implementation does not do that (i.e. the
capacity and the buffer size is the same.) I assume the following does not...
|
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 using queue or dqueue to implement it ...
any good info is appreciated . I want it to be efficient , high speed
for developing device drivers .
| |
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 cost.
STL vector is suitable for removing form tail? or it is as costly as
removing from middle?
any other std container to serve this purpose? (note , I dont need
linked list implementation of any container, as I want random access)
|
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 circular log a preallocated
block of fixed size and work in there.
Can anybody indicate me an example or technique to do this ?
Thanks
Marco
|
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
--------------------------------------------------------------------
//cqueue.cpp file
//IMPLEMENTATION OF cqueue CLASS
http://rafb.net/paste/results/A2gXAr73.html
|
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 store up to 10 items?
Output from my code:
Enter you choice: 1
Enter ID document to print : 21
Enter you choice: 1
Enter ID document to print : 22
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |