Stack Implementation
Related videos:
Stack intro: https://youtu.be/L3ud3rXpIxA
Stack implementation: https://youtu.be/RAMqDLI6_1c
Data Structures Source Code:
https://github.com/williamfiset/algorithms
My website: http://www.williamfiset.com
What You'll Learn
The video demonstrates how to implement a stack using a singly linked list and provides source code for a doubly linked list implementation on GitHub.
Full Transcript
welcome to part two of three in the stack Series this is going to be a short video on one way to implement a stack so Stacks are often implemented as either arrays singly linked lists or even sometimes doubly link lists here I will cover how to push nodes onto a stack with a singly linked list later on we will look at the source code which is actually written using a doubly linked list okay to begin with we need somewhere to start to be in our link list so we're going to point the head to a null node this means that the stack is initially empty then the trick to creating a stack using a singly linked list is to insert the new elements before the head and not at the tail of the list this way we have pointers pointing in the correct direction when we need to pop elements off of the stack as we will soon see the next element however we need to push onto the stack is at two so let's do that so we create a new node adjust the head pointer to be the newest node and then hook on the nodes next pointer to where the head was before and we use the same idea for five and also 13 now let's have a look at popping elements this isn't too hard either just move the head pointer to the next node and deallocate the last node so here we po the first node off the stack and set the nodes reference to be null so that will be picked up by the garbage collector if you're coding in Java and it will since there are no other references pointing to it if you're in another programming language that re requires you to explicitly deallocate and free memory yourself like C or C++ now is the time to do that or you will get memory leaks getting a memory leak in a data structure is one of the worst kinds of memory leaks especially if it's a custom data structure that you intend on reusing so keep watching out for not only in this video but in all the data structures that uh we will be covering if you see in an implementation that I'm not correctly cleaning up my memory please please point it out to me or send a poll request to the GitHub repository so we can patch that okay so we keep proceeding by removing the head and advancing the head pointer down to the next node pop again and pop again and there we go we've stopped popping we've reached last node and the stack is now empty all right time to look at some source code I implemented a stack using a dou link list that we can look at in some detail if you're interested in the source code for the stack in the next video have a link at the code repo provided on the slide it should be provided in the description thanks for watching and I'll see you in the next video
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 35 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
▶
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI