1

I got following code set up:

public class ListStack implements Stack {
    private class List {
        List next;
        Object object;

        public List(Object o, List n) {
            object = o;
            next = n;
        }
    }

    private List firstItem;
    private int size;

    public ListStack() {
        firstItem = new List(null, null);
        size = 0;
    }

    public List getEnd() {
        List endEl = firstItem;

        while (endEl.next != null) {
            endEl = endEl.next;
        }
        return endEl;
    }

    public boolean push(Object o) {
        List e1 = new List(o, null);
        this.getEnd().next = e1;
        size++;
        return true;
    }

    public Object pop() {
        if (this.firstItem.next == null) {
            return null;
        } else {
            List endEl;
            List tempEl;

            endEl = this.getEnd();
            tempEl = firstItem;
            while (tempEl.next != endEl) {
                tempEl = tempEl.next;
            }
            tempEl.next = null;
            size--;
            return tempEl.object;
        }
    }

    public int size() {
        return size;
    }

    public static void main(String[] args) {
        Stack s = new ListStack();

        Object test = new Object();
        Object test2 = new Object();

        System.out.println("pushing Object test to List: " + s.push(test));
        System.out.println("pushing Object test2 to List: " + s.push(test2));

        System.out.println("popping Object from List: " + s.pop());
        System.out.println("popping Object from List: " + s.pop());
        System.out.println("popping Object from List: " + s.pop());
    }
}

And this one:

 public interface Stack {  
     public int size();
     public boolean push(Object o);
     public Object pop();
 }

But its only giving me the first object and twice "null" but it should give me the two objects :( where is my mistake? It is asking for the last item and gives it back (.object) but only returns first object adress

6
  • 5
    Mistake #1: posted link to code instead of relevant code... Commented May 6, 2013 at 20:42
  • Could someone please translate the comments from German to English? Commented May 6, 2013 at 21:19
  • @WChargin removed the comments in an edit, they were standard "what this method does" type comments. Commented May 6, 2013 at 21:23
  • 4
    Hi! Shouldn't your pop() function return endEl.object? Commented May 6, 2013 at 21:27
  • @choice_sk: i think this is the solution , Try to write it an answer :) Commented May 6, 2013 at 21:30

2 Answers 2

4

I think what your pop() function should return is endEl.object.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is way too long-winded. A stack is a data structure that can efficiently push and pop elements. But your code has to traverse the whole stack for both operations (i. e. runs in O(n) instead of O(1) time.).

Prepending to your list is much more efficient as appending.

Example for an efficient push:

public void push(Object o) {
    firstItem = new List(o, firstItem);
    size++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.