Write a C program to implement a queue of at most  elements using an array of size n. ⦁    At the beginning of your code (outside all subprograms), declare a datatype named `struct Queue`. It must be designed according to the pseudo codes (refer to Figure 2) and the provided main function ⦁    Define the subprograms to implement the build, enqueue, and dequeue operations. The meaning of enqueue and dequeue is explained in the pseudocodes in Figure 2. The pseudocodes do not include error checking for underflow and overflow. You are required to extend and implement them to prevent underflow and overflow of the queue.  ⦁    Additionally, your code must check for any other potential errors, such as accessing null pointers. Also, add a few comments to your code to explain the behavior of the main and subprograms. Ensure that your build, enqueue, and dequeue operations print informative messages as demonstrated in Figure 3.  Ensure you test your code with varying sequences of enqueue and dequeue operations. Including five test cases should be sufficient

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a C program to implement a queue of at most  elements using an array of size n.

⦁    At the beginning of your code (outside all subprograms), declare a datatype named `struct Queue`. It must be designed according to the pseudo codes (refer to Figure 2) and the provided main function
⦁    Define the subprograms to implement the build, enqueue, and dequeue operations. The meaning of enqueue and dequeue is explained in the pseudocodes in Figure 2. The pseudocodes do not include error checking for underflow and overflow. You are required to extend and implement them to prevent underflow and overflow of the queue. 

⦁    Additionally, your code must check for any other potential errors, such as accessing null pointers. Also, add a few comments to your code to explain the behavior of the main and subprograms. Ensure that your build, enqueue, and dequeue operations print informative messages as demonstrated in Figure 3. 

Ensure you test your code with varying sequences of enqueue and dequeue operations. Including five test cases should be sufficient

API
void build(struct Queue **q, unsigned int length)
void enqueue(struct Queue *q, char x)
char dequeue(struct Queue *q)
(a) Q
Return value
Builds the queue of the specified length'. Initializes
the queue elements. The memory address of the
created queue is stored in q.
No return value. Side effect; x stored at the tail of
Q. The tail is incremented by one.
1
(b) Q35
Returns the value stored at the head. Head
incremented. Returns error code, if some
encountered.
1 2 3 4 5 6 7 8 9
15 69
Q.head = 7
2 3 4 5 6 7 8 9
10 11 12
84
Q.tail 3 Q.head = 7
Q.tail = 12
10 11 12
15 69 84 17
1
2 3 4 5 6 7 8 9 10 11 12
(c) Q35
15 698 4 17
Q.tail = 3
Q.head=8
Figure 1 A queue implemented using an array Q[1..12].
Figure 1 shows the effects of the enqueue and dequeue operations. Queue elements
appear only in the lightly shaded positions. Figure 1(a) shows a case where the queue
has 5 elements, in locations Q[7..11]. Figure 1(b) shows the configuration of the queue
after the calls ENQUEUE(Q.17), ENQUEUE(Q, 3), and ENQUEUE(Q, 5). Figure
1(c) shows the configuration of the queue after the call DEQUEUE(Q) which returns
the key value 15 formerly at the head of the queue. The new head has the value 6
(stored at the position 8).
The following pseudo codes describe the meaning of the enqueue and dequeue
operations.
Transcribed Image Text:API void build(struct Queue **q, unsigned int length) void enqueue(struct Queue *q, char x) char dequeue(struct Queue *q) (a) Q Return value Builds the queue of the specified length'. Initializes the queue elements. The memory address of the created queue is stored in q. No return value. Side effect; x stored at the tail of Q. The tail is incremented by one. 1 (b) Q35 Returns the value stored at the head. Head incremented. Returns error code, if some encountered. 1 2 3 4 5 6 7 8 9 15 69 Q.head = 7 2 3 4 5 6 7 8 9 10 11 12 84 Q.tail 3 Q.head = 7 Q.tail = 12 10 11 12 15 69 84 17 1 2 3 4 5 6 7 8 9 10 11 12 (c) Q35 15 698 4 17 Q.tail = 3 Q.head=8 Figure 1 A queue implemented using an array Q[1..12]. Figure 1 shows the effects of the enqueue and dequeue operations. Queue elements appear only in the lightly shaded positions. Figure 1(a) shows a case where the queue has 5 elements, in locations Q[7..11]. Figure 1(b) shows the configuration of the queue after the calls ENQUEUE(Q.17), ENQUEUE(Q, 3), and ENQUEUE(Q, 5). Figure 1(c) shows the configuration of the queue after the call DEQUEUE(Q) which returns the key value 15 formerly at the head of the queue. The new head has the value 6 (stored at the position 8). The following pseudo codes describe the meaning of the enqueue and dequeue operations.
ENQUEUE (Q, x)
Q[Q.tail] = X
if Q.tail == Q.length
Q.tail 1
=
else Q.tail = Q.tail + 1
1
2
3
4
DEQUEUE(Q)
1 x
2
3
x = Q[Q.head]
if Q.head == Q.length
Q.head = 1
4
5 return x
Figure 2 Pseudocodes for enqueue and dequeue subprograms
else Q.head =
enqueue(qptr, 1);
enqueue(qptr, 2);
enqueue(qptr, 3);
enqueue(qptr, 4);
enqueue(qptr, 5);
enqueue(qptr, 6);
build(&qptr, 5);
//====== Beginn Test Case
Test
Your code will be tested using the following main function:
int main(int argc, const char * argv[]) {
struct Queue *qptr = NULL;
Q. head + 1
printf("Dequeue() %d\n", dequeue(qptr));
printf("Dequeue() %d\n", dequeue(qptr));
printf("Dequeue() %d\n", dequeue(qptr));
printf("Dequeue() %d\n", dequeue(qptr));
printf("Dequeue() %d\n", dequeue(qptr));
printf("Dequeue() %d\n", dequeue(qptr));
//====== End Test Case
// free anything that was created with malloc
if (qptr->data) free(qptr->data);
if(qptr) free(qptr);
Transcribed Image Text:ENQUEUE (Q, x) Q[Q.tail] = X if Q.tail == Q.length Q.tail 1 = else Q.tail = Q.tail + 1 1 2 3 4 DEQUEUE(Q) 1 x 2 3 x = Q[Q.head] if Q.head == Q.length Q.head = 1 4 5 return x Figure 2 Pseudocodes for enqueue and dequeue subprograms else Q.head = enqueue(qptr, 1); enqueue(qptr, 2); enqueue(qptr, 3); enqueue(qptr, 4); enqueue(qptr, 5); enqueue(qptr, 6); build(&qptr, 5); //====== Beginn Test Case Test Your code will be tested using the following main function: int main(int argc, const char * argv[]) { struct Queue *qptr = NULL; Q. head + 1 printf("Dequeue() %d\n", dequeue(qptr)); printf("Dequeue() %d\n", dequeue(qptr)); printf("Dequeue() %d\n", dequeue(qptr)); printf("Dequeue() %d\n", dequeue(qptr)); printf("Dequeue() %d\n", dequeue(qptr)); printf("Dequeue() %d\n", dequeue(qptr)); //====== End Test Case // free anything that was created with malloc if (qptr->data) free(qptr->data); if(qptr) free(qptr);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Use of XOR function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education