Hey Mark, very nice article. You demonstrate object hierarchy and genetics nicely. Just one question about the design, Why does the pop method on Stack<T> returns StackNode<T> instead of T. I push: string I should be pop: string. I push number I should pop number. What do you think?
Hi Nik, Good question. Yes, that's right you're pushing a value with type string or number but as you can see at right here.
push(val:T){constnode=newStackNode(val)// logic}
the variable node holds a value object. If we have an example that looks like this.
conststack=newStack()stack.push('1')
that node variable will hold a value
StackNode{value:'1',next:null}
the value you're talking about is inside the object. We're using an object so that we can easily get the next value in the stack. If we use the pop method.
constpopData=stack.pop()console.log(popData)// we will get the object StackNode { value: '1', next: null }.
I agree. Let me rephrase. The user of stack should be only aware of stack and the valueType it pushes or pops. It adds no value (in my opinion ) to give back stacknode of popped value.
So, it makes sense to use stacknode internally in stack to keep track of next value but donβt expose stacknode to the user of stack.
Awh ok. Sorry for the late reply. I think I understand what you're talking about. You want just the value correct not the object?. If that's what you want, you need two change two parts.
Hey Mark, very nice article. You demonstrate object hierarchy and genetics nicely. Just one question about the design, Why does the
popmethod onStack<T>returnsStackNode<T>instead ofT. Ipush: stringI should bepop: string. I push number I should pop number. What do you think?Hi Nik, Good question. Yes, that's right you're pushing a value with type
stringornumberbut as you can see at right here.the variable
nodeholds a value object. If we have an example that looks like this.that
nodevariable will hold a valuethe value you're talking about is inside the object. We're using an object so that we can easily get the next value in the stack. If we use the pop method.
I agree. Let me rephrase. The user of stack should be only aware of stack and the valueType it pushes or pops. It adds no value (in my opinion ) to give back stacknode of popped value.
So, it makes sense to use stacknode internally in stack to keep track of next value but donβt expose stacknode to the user of stack.
Awh ok. Sorry for the late reply. I think I understand what you're talking about. You want just the value correct not the object?. If that's what you want, you need two change two parts.
Stack Interface
Stack pop implementation