Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will implement the Queue interface by writing a new class called LinkedQueue. This class will build the queue using Node objects that are linked

image text in transcribed
image text in transcribed
You will implement the Queue interface by writing a new class called LinkedQueue. This class will build the queue using Node objects that are linked together. There will be no array in this class. Use the LinkedStack as a model. With the LinkedStack, we had a top variable that identified the top Node in the stack. With a LinkedQueue, you will have two such variables: head identifies the first Node in the queue tail identifies the last Node in the queue head tail You always add to the tail and remove from the head. Complete the LinkedQueue class by doing the following: 1) Add the inner class Node that is exactly like the Node class in LinkedStack. 2) Add two fields for head and tail. These are like tog in LinkedStack. 3) You do not need a constructor method, but you can add one that sets both head and tail to null. 4) Complete the remove method. This is similar to the pop method in LinkedStack. Here's a special case: if the queue is empty after removing the element, you must set tail to null. 5) Complete the add method. This is not quite the same as the push method in LinkedStack because you're adding to the end of the queue, not the beginning. Look at the diagram of the queue above to figure out what you need to do here. Here's a special case: If the queue is empty, you need to set both head and tail to the new Node. 6) Complete the peek and isEmpty methods. 7) Test the LinkedQueue class by writing a main method to make sure you can add and remove clements successfully. E element; Node link; } private Node top; private int size; public LinkedStack() { top = null; size = 0; } @Override public void push(E elem) { Node n = new Node(); n.element - elem; n.link = top; top = n; size++; } @Override public E pop() { if (size == 0) throw new EmptyStackException(); E elem - top.element; top = top.link; size--; return elem; } @Override public E peek() { if (top == null) throw new EmptyStackException(); return top.element; } @Override public int size() { return size; } @Override public boolean empty() {

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago