Wes
Wes

Reputation: 17

Stack Linked List Troubleshooting

I have this assignment to implement a Linked List Stack and I can't understand why the test output isn't showing any data. When ever compile and run the program the output is:

This is what it is suppose to look like
Sample Expected output:

OK: stack is empty
Push 3 data: 10, 30, 50
Print stack [50,30,10,]
OK: stack size is 3
OK: peek stack top is 50
OK: stack is not empty
Pop 2 data: 50, 30
Print stack [30,10,]
Print stack [10,]
OK: stack pop data is 30
Clear stack
Print stack []

This is what I am getting instead
Your Test output:

OK: stack is empty
Push 3 data: 10, 30, 50

But right here it is doesn't run anything and it does not provide me with any errors. I don't understand what is wrong with this. My code is below:

/**
    A class of stacks whose entries are stored in a chain of nodes.
    Implement all methods in SimpleLinkedStack class using 
    the inner Node class. 

    Do not change or add data fields 
    Do not add new methods
    You may access Node object fields directly, i.e. data and next 
*/

package PJ2;

public class SimpleLinkedStack<T> implements StackInterface<T>
{

   // Data fields
   private Node topNode;    // references the first node in the chain
   private int count;       // number of data in this stack

   public SimpleLinkedStack()
   {
       topNode = null;
       count = 0;
      // add stataments
   } // end default constructor

   public void push(T newData)
   {
       Node newNode = new Node (newData, topNode);
       topNode = newNode;
       count++;

      // add stataments
   } // end push

   public T peek()
   {
       T top = null;
       if (topNode != null)
           top = topNode.data;

       return top;
      // add stataments
   } // end peek

   public T pop()
   {
       T top = peek();
       if (topNode != null) {
           topNode = topNode.next;
           count--;
       }

       return top;
      // add stataments
   } // end pop

   public boolean empty()
   {
       return (count == 0) && (topNode == null);
      // add stataments
   } // end empty

   public int size()
   {
       return count;
      // add stataments
   } // end isEmpty

   public void clear()
   {
       topNode = null;
       count = 0;
      // add stataments
   } // end clear

   @Override
   public String toString()
   {
       String result = "[";
       Node currentNode = topNode;
       while (currentNode != null) {
           result = result + topNode.data + ", ";
           currentNode = topNode.next;
       }
       result = result + "]";
       return result;
      // add stataments
      // note: data class in stack must implement toString() method
      //       return a list of data in Stack, separate them with ','
   }


   /****************************************************
    private inner node class
        Do not modify this class!!
        you may access data and next directly
    ***************************************************/

    private class Node
    {
      private T data; // entry in list
      private Node next; // link to next node
      private Node (T dataPortion)
      {
        data = dataPortion;
        next = null; // set next to NULL
      } // end constructor

      private Node (T dataPortion, Node nextNode)
      {
        data = dataPortion;
        next = nextNode; // set next to refer to nextNode
      } // end constructor
    } // end Node


   /****************************************************
      Do not modify: Stack test
   ****************************************************/
   public static void main (String args[])
   {

        System.out.println("\n"+
    "*******************************************************\n"+
        "Sample Expected output:\n"+
    "\n"+
        "OK: stack is empty\n"+
        "Push 3 data: 10, 30, 50\n"+
        "Print stack [50,30,10,]\n"+
        "OK: stack size is 3\n"+
        "OK: peek stack top is 50\n"+
        "OK: stack is not empty\n"+
        "Pop 2 data: 50, 30\n"+
        "Print stack [30,10,]\n"+
        "Print stack [10,]\n"+
        "OK: stack pop data is 30\n"+
        "Clear stack\n"+
        "Print stack []\n"+
    "\n"+
    "*******************************************************");

        System.out.println("\nYour Test output:\n");
    StackInterface<Integer> s = new SimpleLinkedStack<Integer>();
    if (s.empty()) 
            System.out.println("OK: stack is empty");
    else
            System.out.println("Error: stack is not empty");

    s.push(10);
    s.push(30);
    s.push(50);
        System.out.println("Push 3 data: 10, 30, 50");
        System.out.println("Print stack " + s);

    if (s.size() == 3) 
            System.out.println("OK: stack size is 3");
    else
            System.out.println("Error: stack size is " + s.size());

    if (s.peek() == 50) 
            System.out.println("OK: peek stack top is 50");
    else
            System.out.println("Error: peek stack top is " + s.size());

    if (!s.empty()) 
            System.out.println("OK: stack is not empty");
    else
            System.out.println("Error: stack is empty");

        System.out.println("Pop 2 data: 50, 30");
        s.pop();
        System.out.println("Print stack " + s);
    int data=s.pop();
        System.out.println("Print stack " + s);
    if (data == 30) 
            System.out.println("OK: stack pop data is 30");
    else
            System.out.println("Error: stack pop data is " + data);

        System.out.println("Clear stack");
        s.clear();
        System.out.println("Print stack " + s);
   }

} // end Stack

Upvotes: 1

Views: 98

Answers (1)

Stephen C
Stephen C

Reputation: 718788

Hint: The behavior you are seeing suggests that:

    System.out.println("Print stack " + s);

never finishes. Now you know that + s will call toString() on s. So look carefully at what toString is doing to see if it has an infinite loop. (I think it does ...)

Hint 2: If you can't find the bug by "eye-balling" the code, then try using a Java debugger and single-stepping it. (You still need to use your powers of observation and deduction ...)

Upvotes: 1

Related Questions