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

I\'m given the code below to create a linked list of strings using nodes, but I

ID: 3674301 • Letter: I

Question

I'm given the code below to create a linked list of strings using nodes, but I don't understand how to properly add elements to the linked list. Can anyone provide code and an explanation?

public class StringStack {

private Node head = null;

private int size = 0;
  
public class Node {
public String item;
public Node next;
  
// constructors
public Node() {
item = "";
next = null;
}
  
public Node(String n) {
item = n;
next = null;
}
  
public Node(String n, Node p) {
item = n;
next = p;   
}
}
  
// your code here for push, pop, top, size, and isEmpty
  
public Node top = head;
  
  
public void push(String n) {
Node temp = new Node(n);
  
while (top.next != null) {
top.next = top;
  
}
top.item = temp.item;
size++;

}

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class StringStack {
   private Node head = null;
   private int size = 0;

   public class Node {
       public String item;
       public Node next;

       // constructors
       public Node() {
           item = "";
           next = null;
       }

       public Node(String n) {
           item = n;
           next = null;
       }

       public Node(String n, Node p) {
           item = n;
           next = p;
       }

       /*
       * (non-Javadoc)
       *
       * @see java.lang.Object#toString()
       */
       @Override
       public String toString() {
           return item + "|";
       }

   }

   // your code here for push, pop, top, size, and isEmpty

   public Node top = head;

   /**
   * returns top node of the stack
   *
   * @return
   */
   public Node top() {

       return top;
   }

   /**
   * pushes an element at top of the stack
   *
   * @param n
   */
   public void push(String n) {
       Node temp = new Node(n);

       if (top == null) {
           top = temp;
       } else {

           temp.next = top;
           top = temp;

       }

       size++;
   }

   /**
   * to remove top element of the stack
   *
   * @return
   */
   public Object pop() {
       if (top == null)
           return null;

       Object o = top.item;
       top = top.next;
       size--;
       return o;
   }// Gets the object from the stack

   /**
   * check it is empty or not
   *
   * @return
   */
   public boolean isEmpty() {

       return (size == 0);
   }

   /**
   * return total size of the stack
   *
   * @return
   */
   public int size() {

       return size;
   }

   public static void main(String[] args) {

       StringStack stack = new StringStack();
       System.out.println("Pushes 3 elements :");
       stack.push("hai1");
       stack.push("hai2");
       stack.push("hai3");

       System.out.println("size of stack becomes:" + stack.size());
       System.out.println("Pops one node from stack:" + stack.pop());
       System.out.println("size of stack becomes:" + stack.size());
       System.out.println("top of the stack :" + stack.top());
       System.out.println("stack is empty ?:" + stack.isEmpty());

   }
}

OUTPUT:

Pushes 3 elements :
size of stack becomes:3
Pops one node from stack:hai3
size of stack becomes:2
top of the stack :hai2|
stack is empty ?:false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote