Java - Linked stack implementation, can't figure out why same number is always printed

Question


So I've been trying to finish up the following lab for university. Most of this code was supplied to us, but we were told to fill in the methods for the LinkedStack.java code. The concept is pretty simple, each Node() object contains an Object type variable (an Integer in this case) and another Node() object. The LinkedStack() class is meant to initialize these Nodes and set a limit on the amount of them. The problem is, after executing StackTest.java, the following is returned:

18 18 18 18 18 18 18 18 18 18
6 6 6 6 6 6

When it is supposed to return the following:

18 16 14 12 10 8 6 4 2 0
6 5 4 3 2 1

P.S. The code is extensively commented on, so I'm sure you'll be able to figure out whatever I haven't mentioned. Also, StackADT is just a simple interface, all it does is state the methods used in LinkedStack.

And of course, thanks so much to anyone who helps!

Here is the all of the code (Sorry, I'm new here and didn't know how to keep it from grouping together):

    // ***************************************************************
    // LinkedStack.java
    //
    // A linked implementation of an Object stack class with operations push,
    // pop, and isEmpty and isFull.
    //
    // ***************************************************************
    public class LinkedStack implements StackADT {

    private Node top; // reference to top of stack
    private int size;
    private int counter = 0;

    // ---------------------------------------------------
    // Constructor -- initializes top
    // ---------------------------------------------------
      public LinkedStack() {
        top = new Node();
        size = 10;
      }

    // ---------------------------------------------------
    // Adds element to top of stack if it's not full, else
    // does nothing.
    // ---------------------------------------------------
      public void push(Object val) {
        counter++; 
        if(counter > 0 && counter <= size) {]
          top.setNext(top);
          top.setElement(val);
        }
        else {
          top.setElement(val);
        }
      }

    // ---------------------------------------------------
    // Removes and returns value at top of stack. If stack
    // is empty returns null.
    // ---------------------------------------------------
      public Object pop() {
        if (counter > 0) {
          Object val = top.getElement();
          top = top.getNext();
          counter--;
          return(val);
        }
        else {
          return(null);
        }
      }

    // ---------------------------------------------------
    // Returns true if stack is empty, false otherwise.
    // ---------------------------------------------------
      public boolean isEmpty() {
        if(counter == 0) {
          return(true);
        }
        else {
          return(false);
        }
      }

    // ---------------------------------------------------
    // Returns true if stack is full, false otherwise.
    // ---------------------------------------------------
      public boolean isFull() {
        if(counter == size) {
          return(true);
        }
        else {
          return(false);
        }
      }
    }

Node.java:

//***********************************************************
// Node.java
// A general node for a singly linked list of objects.
//***********************************************************

public class Node {

  private Node next;
  private Object element;

//----------------------------------------------------
// Creates an empty node
//----------------------------------------------------
  public Node() {
    next = null;
    element = null;
  }

//----------------------------------------------------
// Creates a node storing a specified element
//----------------------------------------------------
  public Node(Object element) {
    next = null;
    this.element = element;
  }

//----------------------------------------------------
// Returns the node that follows this one
//----------------------------------------------------
  public Node getNext() {
    return next;
  }

//----------------------------------------------------
// Sets the node that follows this one
//----------------------------------------------------
  public void setNext(Node node) {
    next = node;
  }

//----------------------------------------------------
// Returns the element stored in this node
//----------------------------------------------------
  public Object getElement() {
    return element;
  }

//----------------------------------------------------
// Sets the element stored in this node
//----------------------------------------------------
  public void setElement(Object element) {
    this.element = element;
  }
}

StackTest.java:

// *******************************************************
// StackTest.java
//
// A simple driver that exercises push, pop, isFull and isEmpty.
// Thanks to autoboxing, we can push integers onto a stack of Objects.
//
// *******************************************************

public class StackTest {

public static void main(String[] args) {
   StackADT stack = new LinkedStack ();

   //push some stuff on the stack
   for (int i=0; i<10; i++)
     stack.push(i*2);

   //pop and print
   //should print 18 16 14 12 10 8 6 4 2 0
   while (!stack.isEmpty())
     System.out.print(stack.pop() + " ");
     System.out.println();

   //push a few more things
   for (int i=1; i<=6; i++)
     stack.push(i);

   //should print 6 5 4 3 2 1
   while (!stack.isEmpty())
     System.out.print(stack.pop() + " ");
     System.out.println();
  }
}


Answer


// ---------------------------------------------------
// Adds element to top of stack if it's not full, else
// does nothing.
// ---------------------------------------------------
  public void push(Object val) {
    counter++; 
    if(counter > 0 && counter <= size) {]
      top.setNext(top);
      top.setElement(val);
    }
    else {
      top.setElement(val);
    }

Look at top.setNext(top). This is why you're getting your output.

Read the comment. Now look at the if .. else. You're actually doing something when the stack is full.

Edit: Looking at other methods I can see logical flaws throughout. I would recommend using a pen and paper to decide what should be happening. Alternatively, start from scratch and implement a method at a time. Run the method and ensure it works as expected. If that works, add another method, test it and previous methods. I'm suggesting this as I assume you're fairly novice to programming -- I believe this will do you the world of good.