4.2.3.1 Stacks Flashcards
What does pushing do in stacks?
Adds a new specified item to the top of the stack
What does popping do in stacks?
Removes the item from the top of the stack.
What does peeking mean in stacks?
Returns the value at the top of the stack but doesn’t remove it
Write a pseudo-code algorithm for the Push operation to push a value stored in the variable ANumber onto the stack.
Function Push(StackArray, TopOfStackPointer, ANumber) IF TopOfStackPointer < 20 THEN StackArray[TopOfStackPointer] = ANumber TopOfStackPointer += 1
End Function
Advantages / disadvantages of a stack being implemented using an array
Empty entries waste space // Maximum/fixed/static size
Used when the size of the stack /amount of data is known
Memory saved since no pointers
Advantages / disadvantages of a stack being implemented using a linked list
Space used by pointers // more complex to program
Used when the size of the stack is unknown