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

Implement the stack using a linked list. Use the followingclass definition for t

ID: 3611951 • Letter: I

Question

Implement the stack using a linked list. Use the followingclass definition for the stack and use the given main method totest your implementation.

public class Stack

{

private class node

     {

           intdata;

          node next;

     }

    private node myTop;//pointer to the top ofthe stack

   

   public Stack()

     {//Create an empty stack }

     public boolean empty()

     {// return true if stack is empty,otherwise return false}

     public void push(int value)

     {//add to the top of the stack }

     public void display()

     {//display data stored in stack fromtop to bottom}

    

     public int top()

     {//return the top of the stack}

     public void pop()

     {//remove the value at the top of thestack}

}

public static void main(String[] args)

{

    Stack S = new Stack();

    // insert each value 1 through 5 onto thestack

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

     S.push(x);

    //Display the content of the stack to thescreen

      S.display();  

    //Remove and display each value on thestack

      while (!S.empty())

      {    int x;

     x = S.top();

     System.out.println();

     System.out.println(“Popping… “ + x);

     S.pop();

    S.display();      

    

       }

    

      if (S.empty())

     System.out.println(“Stack isempty.”);

}

Explanation / Answer

public class Stack {     private class Node     {         int data;         Node next;     }     private Node myTop;//pointer to the top of thestack         public Stack()     {         //Create an empty stack         myTop=null;     }     public boolean empty()     {         // return true if stack isempty, otherwise return false                 if(myTop==null)             returntrue;         else             returnfalse;             }         public void push(int value)     {          //add to the top of thestack          Node temp= newNode();          temp.data = value;          temp.next= myTop;                   myTop= temp;     }         public void display()     {         //display data stored instack from top to bottom         Node temp= myTop;         while(temp !=null)         {            System.out.print( ( temp.data) + "     ");             temp=temp.next;         }                 System.out.println();     }     public int top()     {         //return the top of thestack         if(myTop !=null)             returnmyTop.data;         else         {               System.out.println("Stack is empty");             return-1;         }     }         public void pop()     {         //remove the value at the topof the stack         if(myTop !=null)         {            System.out.println("The element popped is: " + (myTop.data));             myTop=myTop.next;         }         else         {            System.out.println("The Stack is empty");         }     } public static void main(String[] args) {     Stack S = new Stack();     // insert each value 1 through 5 onto thestack     for(int x = 1; x