0

I'm trying to implement a simple Stack in javascript (I know it can be implemented with a simple array, but that's not my point.) So here's my design:

pop(): remove and return the item from the top of the stack
push(item): add the item at the top of the stack
peek(): return the item from the top of the stack
isEmpty(): return true if stack is empty

It'll have a top property that keeps track of the top element and here's my class definition:

class Stack {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.top = Stack(data);    // <- Here's my problem
  }

  pop() {
    if(this.top == null) return new Error('Trying to pop, but the stack is empty.');

    let item = this.top.data;
    this.top = this.top.next;
    return item;
  }

  push(data) {
    let item = new Stack(data);
    item.next = this.top;
    this.top = item;
  }

  peek() {
    if(this.top == null) return new Error('Peeking the stack, but the stack is empty.');

    return this.top.data;
  }

  isEmpty() {
    return top == null;
  }
}

I want the top property to be a Stack element, however, as you can see, that'd throw me into an infinite loop. I could just set top as an object that has data, next, and top. But is that the only way? Perhaps I can have a member function that generates the top property when initializing the class? But still, I'll eventually have to set that to an object instead of a Stack object.

Any suggestion?

2 Answers 2

1

If you want to implement your stack as a linked list, that list will need to end at some point, with its "next"/"top" reference being null.

However, you should not confuse your stack (representing the whole thing, possibly being empty) with a stack element (one data item in the stack). Don't mix them into the same class, use two separate classes:

class StackElement {
  constructor(data) { // takes the data as an argument (possibly also `next`)
    this.data = data;
    this.next = null;
  }
}
class Stack {
  constructor() { // takes no arguments
    this.top = null; // initialises reference with nothing
    // has no other properties
  }
  …
}

I'll leave the implementation of the methods as an exercise to the reader.

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

1 Comment

Thank you! this is exactly where I'm confused about. Everything makes sense now.
1

You are confusing the stack, with the data. What your code tries to do is push the entire stack into the stack in the constructor, possibly running into endless recursion.

Define what data is, maybe turn it into a class Data, or treat it as an Array. In your Stack constructor, just push the data (as primitive or as an instance of a class Data) into the stack.

2 Comments

Thanks for the quick response! I think you are essentially saying the same as the accepted answer, but they better clarified their explanation by giving me some guidance on the code.
Yes. Well, I wrote beforehands, he elaborated a little bit :)

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.