You are given the code skeleton for implementing a queue using Stack (that is in
ID: 3724399 • Letter: Y
Question
You are given the code skeleton for implementing a queue using Stack (that is in turn implemented using a Doubly Linked List). Complete the code for the enqueue), dequeue) and isEmptyO functions of the Queue class. The code for the main function is also given to you. After you implement the above three functions, you can run your main function and capture screenshot. The queue size can range from 5 to 10 and the maximum value for an element in the queue can be 50 As you can notice, there are two Stacks (stackl and stack2) declared as private member variables in the Queue class. You need to use these two Stacks for implementing the functionalities of a queue. I suggest the following design (you are free to choose your own design; provide a detailed explanation in your project report if your design is different from mine) Use stackl to store the elements of the queue (with the invariant that the topmost element of stackl is the element at the front of the queue and the bottommost element of stackl is the element at the end of the queue) and use stack2 as an auxiliary data structure to implement the enqueue function. Since the topmost element of stackl is the element in the front of the queue, the dequeue function can be simply implemented as the result of a pop operation on stackl. It is the enqueue function that needs to be thought out in greater detail and implemented. I suggest the following idea for enqueue of an integer 'data First check if stackl is empty or not. If it is empty, simply push the 'data' to it and return from the enqueue function. If stack1 is not empty to start with, then pop out all the elements of stackl and push cach of them to stack2. After stack1 gets empty, push the 'data' to it. Now, pop out all the elements of stack2 and push them back to stackl. As a result of this, the newly enqueued 'data' will be in the bottom of stackl Submission (through Canvas): (1) Write a pseudo code of the enqueue function implemented for this problem. Explain how the invariant that "the topmost element of stacki is the element at the front of the queue and the bottommost element of stack1 is the element at the end o (2) Discuss the theoretical time complexity of the enqueue and dequeue implementations. (3) Include the complete C++ code of the (Doubly Linked List-based) Stack-based Queue implementation, including the main function (4) Show a screenshot of the execution of your implementation for a queue of size anywhere chosen from 5 to 10, with the maximum value of an element in the queue being 50 f the queue is maintained with each enqueue and dequeue operation.Explanation / Answer
Program:
#include<stdio.h>
#include<stdlib.h>
struct stack_node
{
int data;
struct stack_node *newval;
};
void push(struct stack_node** stack_top, int new_data);
int pop(struct stack_node** stack_top);
struct queue
{
struct stack_node *first_stack;
struct stack_node *second_stack;
};
void enQueue(struct queue *q, int val)
{
push(&q->first_stack, val);
}
int deQueue(struct queue *q)
{
int val;
if(q->first_stack == NULL && q->second_stack == NULL)
{
printf("Q is empty");
exit(0);
}
if(q->second_stack == NULL)
{
while(q->first_stack != NULL)
{
val = pop(&q->first_stack);
push(&q->second_stack, val);
}
}
val = pop(&q->second_stack);
return val;
}
void push(struct stack_node** stack_top, int new_data)
{
struct stack_node new_node = (struct stack_node) malloc(sizeof(struct stack_node));
if(new_node == NULL)
{
printf("Stack overflow ");
//getchar();
exit(0);
}
new_node->data = new_data;
new_node->newval = (*stack_top);
(*stack_top) = new_node;
}
int pop(struct stack_node** stack_top)
{
int result;
struct stack_node *top;
if(*stack_top == NULL)
{
printf("Stack overflow ");
//getchar();
exit(0);
}
else
{
top = *stack_top;
result = top->data;
*stack_top = top->newval;
free(top);
return result;
}
}
// Driver function
int main()
{
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
q->first_stack = NULL;
q->second_stack = NULL;
enQueue(q, 4);
enQueue(q, 6);
enQueue(q, 100);
printf("%d ", deQueue(q));
printf("%d ", deQueue(q));
printf("%d ", deQueue(q));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.