LinkedLists Python
LinkedLists Python
and Queues
Dr. Baldassano
[email protected]
Yu’s Elite Education
Last week recap
Heaps
Heap structure and ordering
Adding elements
Removing elements
Using as priority queue
Heapsort
Problems with arrays
http://visualgo.net/list.html
Big-O for linked lists
Node structure
Data field
Pointer to next node (NULL if end of list)
Head pointer to first node
Example in python
Reversing linked list
Uses of queues?
Website requests
Simulations or game engines
Expanding algorithms like Dijkstra's
Stacks
Uses of stacks?
Recursive algorithms like mergesort
Backtracking algorithms like constraint
satisfaction (e.g. Sudoku)
Compiling and running programs
Implementing Queue with
linked list
Why implement with linked list?
Enqueue operation:
Add node at head
Dequeue operation
Remove and return node at tail
http://visualgo.net/list.html
Implementing Stack with
linked list
Why implement with linked list?
Push operation:
Add node at head
Pop operation:
Remove and return node at head
http://visualgo.net/list.html
Stack example:
Reverse Polish Notation
Traditional math notation: requires knowing
order of operations, using parentheses
7+(1+2)*5 = 22
Reverse Polish notation avoids these issues by
using a stack
712+5*+
Implement in python
Assignment: Median of sorted
circularly linked list
Sorted singly-linked circular list: