JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node...

70.2K

Verified Solution

Question

Programming

JAVA DATA STRUCTURE (Linked Lists/Queue)

public class Node {
   int value;
   Node nextNode;

   Node(int v, Node n){
       value = v;
       nextNode = n;
   }

   Node (int v){
       this(v,null);
   }
}

public class Stack {
   protected Node top;

   Stack(){
       top = null;
   }

   boolean isEmpty(){
       return( top == null);
   }
   void push(int v){
       Node tempPointer;
       tempPointer = new Node(v);
       tempPointer.nextNode = top;
       top = tempPointer;
   }
   int pop(){
       int tempValue;
       tempValue = top.value;
       top = top.nextNode;
       return tempValue;
   }
   void printStack(){
       Node aPointer = top;
       String tempString = \"\";
       while (aPointer != null)
       {
           tempString =tempString + aPointer.value + \"\n\";
           aPointer =aPointer.nextNode;
       }

      System.out.println(tempString);
   }
}

public class StackWithLinkedList {
   public static void main(String[] args){
       int popValue;
       Stack myStack = new Stack();
       myStack.push(5);
       myStack.push(7);
       myStack.push(9);
       myStack.printStack();
       popValue = myStack.pop();
       popValue = myStack.pop();
       myStack.printStack();
   }
}

Add the Queue class to the program introduced above. NewClass should be CREATED (-->NOT importing<-- ANY TYPE ofCLASS from Java Library, just using the provided classes), Youshould create Queue class like we created the Stack class(see theCODE above). Create the Queue class with the followingspecifications:

a. It must create a queue(constructor)

b. Insert one end (enqueue)

c. Do extractions at the other end(dequeue)

d. Determine if queue is empty (isEmpty)

e. Print the content of the queue (printQueue)

f. Finally, you must develop a main class QueueWithLinkedList thatproves that every method works (just like we did with theStackWithLinkedList class provided in the programabove)

Answer & Explanation Solved by verified expert
4.0 Ratings (737 Votes)
Code for Queue is provided below alongwith the outputscreenshot Code is Explained in code comments If need any furtherclarification please ask in commentsCODEode for storing elemnts class Node int value to store data Node nextNode to store next node constructor Nodeint v Node n value v nextNode n constructor Node int v thisvnull Queue classclass Queue protected    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students