Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Queue and stack 1) Draw the diagram for this as well, and write a few lines of c

ID: 3855349 • Letter: Q

Question

Queue and stack

1) Draw the diagram for this as well, and write a few lines of code (it should be pretty short) for b) part.

2) Again, draw the diagram and write the code for b) part.

3) What's the output of the following code? Again, draw the diagram and please explain to me what "line.enqueue (stack.peek())" does?

IntegerStack stack = new IntegerStack ();

IntegerQueue line = new IntegerQueue ();

for ( int num = 1; num < 5; num ++)

{

   stack.push (num);

line.enqueue (stack.peek());

}

for (int num = 1; num < 5; num++

{

line.enqueue(stack.peek());

line.enqueue(line.front());

stack.pop();

line.dequeue();

}

while (!line.isempty())

{

System.out.println(line.front ());

line.dequeue();

}

A queue of integers is implemented using a circular array public class Int@ueue i private int[i data; private int count private int front private int rear; a. Give the output from the following code IntQueue g = new IntQueue(); q.enqueue (5) q.enqueue (15) q.enqueue (110); int val -q.dequeue ) val = q. dequeue ( ) ; q.enqueue (17) q.enqueue (19) enqueue(21); val = g. dequeue ( ) ; encrudeue while Iaemptvueue)) System.outpzint (adegueueO): Output: b. Write the code for the dequeue method using the variables above

Explanation / Answer

For question number 1st b part you can use following code-

public String dequeue()

{

if (isEmpty())

{

throw new RuntimeException("Queue underflow");

}

else if (front == rear)

{

String s = front.data;

front = null;

rear = null;

return s;

}

String s = front.data;

front = front.next ;

return s;

}

Note-For other questions to be answered you can provide as separate question

Thanking You