Stack implementation details

3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

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 w length lists. Here I will cover how to push nodes onto a stack with a singly linked list later on and 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 plus, so we're going to point the head to a null note. 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.

List. This way, we have pointers pointing in the correct direction when we need to pop elements off 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. To 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 pull up the first node off the stack and set the nodes reference to be no. So they will be picked up by the garbage collector if you're coding in Java. It will since there are no other references pointing to it. If you're in another programming language that requires you to explicitly deallocate 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 that not only in this video, but in all the data structures that we will be covering. If you see in an implementation and not correctly cleaning up my memory, please please point out to me, or send a pull 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 Park Again, and pop again. There we go.

We've stopped popping we've reached last note and the stack is now empty. Alright, time to look at some source code I implemented a stack using a doubly linked 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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.