Question

I'm just fooling around in Java right now, trying to implement something similar to a stack using linked lists.

class ListStack {

    int value;
    int size;
    ListStack next;

    public ListStack (int add) {
        this.size = 1;
        this.value = add;
        this.next = null;
    }

    public void push (int add) {
        this.next = this;
        this.value = add;
        this.size++;
    }

    public int pop() {
        if (this.size == 0) { throw new EmptyListStackException(); }
        int i = this.value;
        this = this.next;
        this.size--;
        return i;
    }

    public int size() {
        return this.size;
    }

    public int peek() {
        return this.value;
    }
}

Basically it's an insertion-in-front linked list which also removes from front. NetBeans pops an error when I try to do this = this.next; it says I cannot reassign final value this.

I'd like my final implementation to do something like the below:

ListStack var = new ListStack(5); //var is now 5 -> null
var.push(3); //var is now 3 -> 5 -> null
int val = varr.pop(); //var is now 5 -> null, val == 3

Commenting put that this = this.next code, the rest seems to work.

ListStack a = new ListStack(5);
System.out.println(a.size()); //prints 1
System.out.println(a.peek()); //prints 5
a.push(4);
System.out.println(a.size()); //prints 2
System.out.println(a.peek()); //prints 4
a.push(6);
System.out.println(a.size()); //prints 3
System.out.println(a.peek()); //prints 6
a.push(1);
System.out.println(a.size()); //prints 4
System.out.println(a.peek()); //prints 1
//a is 1 -> 6 -> 4 -> 5 -> null
Was it helpful?

Solution

There is a conceptual error in your code: basically you are not creating any new stack element with your push method.

But the problem is that calling the class listStack becomes misleading, because actually what you want to create are new elements of the stack, so you should create a class node maybe. Moreover you can't ovveride "this" because it is a java keyword and it always refers to the current object.

To give you a hint of what to do to implement a stack as a linked list you should create class Node with a value field value and a reference to the previous Node (the first node will just have a null pointer) .

In the class ListStackyou should have a reference to the last node and its push() method should create a new Node and set this one as the new last node.

OTHER TIPS

Let me point you in right direction. As others have commented, this stack does not work properly Consider doing something like this:

public class ListStack {

private class Node {
    private int value;
    private Node next;

            //inner class which holds your each element and reference to next
            //fill all details required     
}

private Node head;
private int size;

public ListStack() {
    head = null;
    size = 0;
}

public void push(int value) {
    Node temp = new Node(value);
    if(head == null)
        head = temp;
    else {
        temp.setNext(head); // provide link to already existing stack
        head = temp;       // bring new element on top
    }
}

public int pop() {
    if(head==null);
        //throw exception
    int temp = head.getValue();
    head = head.getNext(); //remove element and bring down the stack
    return temp;
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top