CC4 - Finals Flashcards
- Aka Last-In-First-Out (LIFO)
- Top – entry/exit point
Stacks
– Insert or push some item “x” onto the stack.
* Before this operation, the stack if often checked if it has enough capacity to accommodate the new item
* If stack is full, an overflow would occur
Push (x)
- – Removing the most recent item or element from the stack.
- Must ensure that the stack is not empty before this operation
- If stack contains no item, popping would cause an underflow error
Pop( )
- – Returns the copy of the item or element at the top of the stack.
- The item itself is not removed from the stack
- This operation is also called peek
Top( )
- – It will return true if the stack is empty, false otherwise.
- Normally applied before the pop operation to avoid stack undeflow
IsEmpty( )
- – It will return true if the stack is full, false otherwise.
- Normally applied before the push operation to avoid stack overflow
- Not used when dynamic memory allocation is used
IsFull()
- generates an empty stack object and reserves necessary storage space
Create
– removes all elements from the stack and releases memory allocated to stack
Destroy
Array Implementation
- the top of the stack is at the beginning of the array, which is inefficient due to the need to shift elements
- the top is at the end, which is* more efficient as it only requires constant time* for push and pop operations.
- uses an array to store data, with a variable “top” tracking the current position of the top element, and the array size defining the maximum capacity, where the top is incremented on push and decremented on pop.
Disadvantage of Array Implementation of Stacks
- Size of stack needs to be specified at compile time
- If an application requires less space than the array size, storage space will be wasted
- The program will crash if application requires more storage space at runtime
- uses a pointer to track the top of the list, with elements added and removed only at that top, ensuring a Last-In, First-Out data structure.
Linked-List Implementation
Push
- Create a new node
- Copy element to the new node
- Link pointer of new node is set to point to pre-existing front node
- Head pointer is re-set to point to the new node
Pop
- Topmost element is copied for removal
- A new pointer is created to point to front mode
- Head pointer is reset to point to the next node
- Using the new pointer, front node is deleted
: inserts an element on the front of the list
* insertFront(x)
: returns and removes the element at the front of the list
* removeFront()
: inserts an element on the back of the list
* insertBack(x)
: returns and removes the element at the end of the list
* removeBack()
: inserts an element on the front of the list.
1) Allocate a new node
2) Have new node point to old head
3) Update head to point to new node
insertFront(x)
: returns and removes the element at the front of the list.
1) Update head to point to next node in the list.
2) Return element of previous head and delete the node.
removeFront()
: inserts an element on the back of the list.
1) Allocate a new node
2) If tail is NULL, update head and tail to point to the new node; otherwise.
* Have the old tail point to the new node.
* Update tail to point to new node.
insertBack(x)
: returns and removes the element at the end of the list.
1) No efficient way of doing.
2) Typically would not use a linked-list if this operation is commonly used
removeBack()
- Most programming languages use stacks to manage subroutine calls by storing necessary information like return addresses, parameters, and local variables in an activation record on the stack, which is then popped off when the subroutine returns.
Recursion Implementation