Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

5 - 6 Short Answer: Bags, Stacks and Queues [ 8 points ] 4 . Trace a stack ( called S ) through the following

5-6 Short Answer: Bags, Stacks and Queues [8 points]4. Trace a stack (called S) through the following operations:
Stack S = new Stack(); S.push(new Integer(2));
S.push(new Integer(7));
Integer X = S.pop(); S.push(new Integer(8)); S.push(new Integer(5));
Integer X = S.pop(); Integer Y = S.peek();
S.push(new Integer(3)); Integer Z = S.pop();
(a) Give the contents of the stack after this code has been executed. Indicate the elements in order and label the top. [2 points]
2
(b) Give the contents of the stack after this code has been executed. Indicate the elements in order and label the top. [2 points]
5. Trace a queue (called Q) through the following operations:
Queue Q = new Queue (); Q.enqueue(new Integer(3));
Integer X = Q.dequeue(); Q.enqueue(new Integer(12)); Q.enqueue(new Integer(5)); Q.enqueue(new Integer(1));
Integer Y = Q. first (); Q.enqueue(new Integer(9));
Integer Z = Q.dequeue();
(a) What is the value of Y after it has been assigned? [2 points]
(b) Give the contents of the queue after the code has been executed. Indicate the elements in order and label the front. [2 points]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions