Code 401: Class 10 - Stacks and Queues
- Stack data structure that consists of nodes that each reference the next but not the previous
- FILO First In Last Out
- LIFO Last In First Out
- Pushing something in to a stack will place it at the top
- Popping something off of the stack removes the top item
- To Push O(1)
- Assign new node’s next to current top node
- Reassign reference to top node to the new node
- To Pop O(1)
- Set a temp to hold the top pointer
- Reassign top to next
- Remove top node
- Clear out temp reference
- To Peek O(1)
- Run isEmpty() first to check if the list is empty
- Return the value of the top node
- isEmpty() O(1)
- Check to see if the top node is null
- Queue
- Enqueue O(1)
- Set rear.next to new node
- Reassign rear to new node
- Dequeue O(1)
- Create a temp reference that points to the same location as Front
- Reassign Front to next
- Clear the temp reference
- Peek O(1)
- Check isEmpty() first to make sure queue is not empty
- Return front value
- isEmpty O(1)
- Return true if front = null
Return to reading-notes Deployed Site
Return to reading-notes Mark Down