arrays - What is the top of the stack in various stack implementation? -
when implementing stack, element stack's top?
- stack implemented linked list
- stack implemented array
stack implemented linked list
in implementation, have linked list, , pointer/reference first element of list. element top of stack.
- when want add element, create new node, link current head, , modify head new element.
push(x)
in stack. - when
pop()
element, take head, set new head following element, , return value of old head.
both operations done in constant time.
stack implemented array
when implementing stack array, circular array implementation. means if array of length n
, refer element @ index 0
, 1 @ index n-1
adjacent.
in implementation, hold index of "top" of stack, , modify whenever pop or push elements stack. top of stack not in constant index in implementation.
Comments
Post a Comment