Data Structures and Algorithms
muigai unaka
Week 4
Stack
A Last-In-First-Out data structure that is a common use of an array. Think of a stack of plates in your cupboard, new plates are added on top of the previous plates and to get a plate, you would remove the top plate
Stack
Add to a stack using .push()
Stack
let stack = [];
stack.push(3)
Remove from a stack using .pop()
let stack = []; stack.push(3); stack.push(2); stack.push(1);
stack.pop(); // removes 1, which was the last added element
- Get item that is at the top of the Stack
- Add item to the top of the Stack
- Implement a Queue using a Stack
- Depth First Search
- Pre-order, In-order, Post-order traversal of a binary tree
Common Operations with Stack
Queue
A First-In-First-Out data structure that is a common use of an array. Think of a single register line at the movie theater, new people are added to the end or taken from the beginning once they have their ticket
Queue
Add to a queue using .push()
Queue
let queue = [];
queue.push(1)
Remove from a queue using .shift()
let queue = []; queue.push(1); queue.push(2);
queue.shift(); // removes 1 from the beginning
- Get item that is at the front of the Queue
- Add item to the end of the Queue
- Implement a Stack using a Queue
- Breadth First Search
- Level Order Traversal
Common Operations with Queue
ጥያቄዎች?
(t’iyak’ēwochi)
Linked List
A sequence data structure, which connects elements, called nodes, through links. Nodes in a linked list are connected by pointers. Linked lists are null terminating, which means whenever the pointer to the next node is null, the list has reached the end.
Linked List
When you think of a Linked List, a real world example would be a music playlist. The first song in the playlist would be the head. It connects to the next song and each song has a reference to a previous and next song. You know you've reached the end of the playlist when there is no next song.
Linked List
// To create a Linked List you will need "Nodes"
// Nodes will have data and a reference to the next Node
function Node (data) {
this.data = data;
// every node starts off with their next set to null
this.next = null;
}
let head = new Node(1);
// We "link" lists by reassigning the next value of a node
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(true);
head.next.next.next.next = new Node({
fun_fact: "Linked Lists can store any data"
})
let LinkedList = head;
- Get, insert, reassign or remove a Node at a specific position
- Reverse a Linked List
- Merge two Linked Lists
- Deep copy a Linked List
- Implementation of Linked List with some common operations here
Common Operations with Linked Lists
Hash Table
Hash Table
Time complexity
- Insertion operation is Linear [O(1)]
- Searching operation is Linear [O(1)]
- Deletion operation is Linear [O(1)]
Leetcode Problems
Ajuju?
Data Structures and Algorithms Week 4
By Muigai Unaka
Data Structures and Algorithms Week 4
Data Structures and Algorithms week 4 at Resilient Coders. Data Structures I: Stack, Queue, Linked List, Hash Table
- 342