-
Notifications
You must be signed in to change notification settings - Fork 2
Arrays, Lists, Stacks and Queues
alichtman edited this page Apr 2, 2018
·
3 revisions
Arrays
Given index,
- Sorted Array -- Efficient find (Binary Search), inefficient insert/remove.
- Unsorted Array -- Inefficient find ($O(n)$), efficient insert/remove
List ADT
There are two implementation strategies for a List.
-
Linked List-based Implementation
- insertAtFront in
$O(1)$ time if head pointer exists, and same for back if tail pointer exists.
- insertAtFront in
-
Array-based Implementation
- insertAtFront in
$O(n)$ because all the elements need to be shifted back. - Allocate extra space on the front and back of the array for front/back inserts and keep pointer to next "front" and "back" space. "Circular" array.
- Array Resize by doubling array capacity and copying data over.
$O(n)$ every$n$ times for amortized$O(1)$ per insert.
Methods Linked List Array push/pop Front() O(1)
O(1)
*due to extra space leftinsert/remove NotFront() O(1)
*IF have pointer to prevO(n)
*need to copy datainsert/remove Arbitrary() O(n)
O(n)
*need to copy data - insertAtFront in
Stack ADT
Last In, First Out
Methods | Linked List | Array |
---|---|---|
push() | O(1) |
O(1) |
pop() | O(1) |
O(1) |
Queue ADT
First In, First Out
Methods | Linked List | Array |
---|---|---|
enqueue() | O(1) |
Amortized O(1)
|
dequeue() | O(1) |
O(1) |
Important Takeaways
O(1) push() / enqueue()
and pop() / dequeue()
operations with both Array and Linked List implementation for Stacks and Queues.