The QUEUE class The header file, queue.h , should look like: The header file con
ID: 3879293 • Letter: T
Question
The QUEUE class
The header file, queue.h, should look like:
The header file contains the function signatures of your public methods while the code module, queue.c, contains their implementations.
The only local includes that queue.c should have are queue.h and the header file of the underlying data structure on which the array is based.
Method behavior
Here are some of the behaviors your methods should have. This listing is not exhaustive; you are expected, as a computer scientist, to complete the implementation in the best possible and most logical manner.
newQUEUE - The constructor is passed a function that knows how to display the generic values stored in the queue. That function is stored in a display field of the QUEUE object:
enqueue - The enqueue method runs in constant or amortized constant time. The value to be enqueued is stored in the underlying data structure.
removeQUEUE - The dequeue method runs in constant or amortized constant time. The value to be dequeued is removed in the underlying data structure.
peekQUEUE - The peek method returns the value ready to come off the queue, but leaves the queue unchanged. It runs in constant time.
sizeQUEUE - The size method returns the number of items stored in the queue. It runs in amortized constant time.
displayQUEUE - This display method prints the items stored in the queue. If the integers 5, 6, 2, 9, and 1 are enqueued in the order given, the method would generate this output:
with no preceding or following whitespace. An empty queue displays as <>.
visualizeQUEUE - The visualize method simply calls the display method of the data structure upon which the queue is based.
Assertions
Include the following assertions in your methods:
newQUEUE - The memory allocated shall not be zero.
dequeue - The size shall be greater than zero.
peekQUEUE - The size shall be greater than zero.
Explanation / Answer
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue ");
printf("2.Delete element from queue ");
printf("3.Display all elements of queue ");
printf("4.Quit ");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice ");
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow ");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow ");
return ;
}
else
{
printf("Element deleted from queue is : %d ", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;
if (front == - 1)
printf("Queue is empty ");
else
{
printf("Queue is : ");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf(" ");
}
} /*End of display() *
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.