March 2019
Intermediate to advanced
336 pages
9h 9m
English
The Pop method on the Stack implementation removes the last element from the element array and returns the element, as shown in the following code. The len method returns the length of the elements array:
// Pop removes and returns a node from the stack in last to first order.func (stack *Stack) Pop() *Element { if stack.elementCount == 0 { return nil } var length int = len(stack.elements) var element *Element = stack.elements[length -1] //stack.elementCount = stack.elementCount - 1 if length > 1 { stack.elements = stack.elements[:length-1] } else { stack.elements = stack.elements[0:] } stack.elementCount = len(stack.elements) return element}
The example output after the Pop method is invoked is as follows. The element value ...
Read now
Unlock full access