25. Code a GUI program that visually demonstrates the changes to contents of the
ID: 671937 • Letter: 2
Question
25. Code a GUI program that visually demonstrates the changes to contents of the array, and the other data members that make up a Queue object (see Figure 3.23) when an Enqueue or a Dequeue operation is performed. When the program is launched, the structure should be shown in its initialized state. Four buttons should be available to the user: an “Enqueue” button, a “Dequeue” button, a “reinitialize” button, and a “quit” button. Provide text boxes for the input of the nodes' fields and for the output of a fetched node.
size4 Size 4 numOfNodes 0 numOfNodes front0 front 0 rear 0 rear 100 datal3] null data 2 null data] null datal0] null datal3null datal2]100 data 1 44 data[o] 300 Carol Avenue C 234 8989 Vick Avenue V 936 7281 300 Mike Avenuc M 546 7373 Initialized State After Mike, then Vick, then Carol Have Been Inserted size 4 numOf Nodes front0 600 rear Bill Avenue 978 5634 100 datal3]600 datal2]100 data44 datato] 300 Carol Avenuc C 234 8989 Vick Avenuc V 936 7281 300 Mike Avenue M 546 7373 Full State: Next Enqueue Generates an Overflow ErrorExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct sNode
{
int data;
struct sNode *next;
};
struct queue
{
struct sNode *stack1;
};
void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
void enQueue(struct queue *q, int x)
{
push(&q->stack1, x);
}
int deQueue(struct queue *q)
{
int x, res;
if(q->stack1 == NULL)
{
printf("Q is empty");
getchar();
exit(0);
}
else if(q->stack1->next == NULL)
{
return pop(&q->stack1);
}
else
{
x = pop(&q->stack1);
res = deQueue(q);
push(&q->stack1, x);
return res;
}
}
void push(struct sNode** top_ref, int new_data)
{
struct sNode* new_node =
(struct sNode*) malloc(sizeof(struct sNode));
if(new_node == NULL)
{
printf("Stack overflow ");
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
int pop(struct sNode** top_ref)
{
int res;
struct sNode *top;
if(*top_ref == NULL)
{
printf("Stack overflow ");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
int main()
{
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
q->stack1 = NULL;
enQueue(q, 1);
enQueue(q, 2);
enQueue(q, 3);
printf("%d ", deQueue(q));
printf("%d ", deQueue(q));
printf("%d ", deQueue(q));
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.