Data Structures in Golang - Stacks and Queues

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this tutorial I'll be talking about stacks and queues like the linkless we learned in the last video stacks and queues are also very basic linear data structures they both have flexible sizes which means that they're gonna grow as you added more data and it's going to shrink as you take it out but the main difference between a stack and a queue is the way data is removed stacks have a last in first out data structure it's also known as Lefou the last one you put in would be the one that you take out the first that's why it's called the stack it's like a stack in real life for example if there's a stack of books and you want to remove a book from the stack you'll have to take away the book that is on the top and that book on the top would have been placed there at the last when we're putting something in a stack when you're putting an item or data in a stack or we call it push and when we take it out we call it pop so push and pop would be the two main functions that we use when we handle a stack and for queues it's gonna be FIFO first in first out which works like normal queue in real life just think of a line at a supermarket cashier the first person to to join the line won't be the first one to be served so for cute when we add an item into a queue we say that we include an item and when we take it out we'll say we DQ'd it which means we took out the one that went that has been there for the longest time so that's the basic explanation of stacks and queues now let's go and implement this in code first let's start working on snap stacks so and we'll define a struct and the struck will hold a slice that holds the values items and we're gonna make two methods for stack we're going to make push and pop so I'm just gonna make a memo here push and pop push will add a value at the back it needs to take in a stack as a pointer receiver because we would actually want to change the receiver we're going to make modifications so and this will have to take in an integer that should be added so we'll add I integer and there's not going to be any returns we're just going to append this integer to two items and because append will return a slice with the appended integer we need to replace that with the existing items so we'll have to do this okay now let's see if this works we haven't done much but let's just check if it works okay oh we will make a new stack and cool it my stack and to see the before and after we're just going to print it out and then we're going to push in something we're going to push in three numbers and then let's print it okay and here we can see that push works fine we successfully pushed in three numbers okay now let's go and make pop okay right here okay I'm just gonna go and fill in the comments and we'll okay and returns okay so pop is going to remove a value at the end and it's going to return the remove value okay we're going to need a pointer receiver and we're not going to take in anything but we need to return the integer that was removed so int okay nice and inside will replace items with the one without the last integer so it should be replace items this means it's going to start from the beginning and it's going to just leave one out at the end and replace items we need to return the removed item but we can't get the removed item from items anymore because we already removed it so that's why we need to store the removed item in a variable first let's store it in to remove okay yes and we return to remove but I really don't like it how I'm writing I'm calling the length function twice I'm going to put that in a variable as well and then just go like that okay good let's go and try to run the pop method just need to I will just pop one value okay let's let's save it and run it okay good you can see that we popped out 300 the integer that entered the last in the list is popped out okay now that was very simple now let's go and implement a queue okay we're going to implement two functions and Q and D Q and of course we need to define the struct called Q okay okay just like stacks we're just going to go just put in a slice so like I mentioned before a stacks and queues the way you input data is going to be the same the only difference between the two is how you remove it so in queue would look the same as stacks but DQ is going to have a little bit of a change okay let's go to in queue need a pointer receiver it needs to take in an integer and no returns okay it's just the same as stacks nothing special but still we're just going to go and check if it works we'll make it a new queue called my queue and just gonna print it and we will add in three numbers just like before and we will print it after okay okay you can see that we successfully added three numbers okay let's go and do the DQ turns we will need a pointer receiver we don't need any parameters but we need to return the remove values so it goes like that and inside we need to return the removed integer so first let's store it in to remove and we will need to remove the first integer the one in the front index zero and then we will actually have to remove it so cute items so we're going to skip index zero start from one to the end okay and then we will return to move okay and we can run it by adding my qdq and then okay this time it's going to DQ but instead of taking away the one added last it's going to take away what the one that's added first okay good you can see that 100 is DQ'd yes I think this is this is it so this is what stacks and queues are this tutorial we just aim to understand the basic concepts and I try to be as concise as possible so I hope you got the point and I hope this was helpful yes I'll see you in the next video bye
Info
Channel: Junmin Lee
Views: 6,593
Rating: undefined out of 5
Keywords: stacks, queues, data structures, golang, leetcode, coding, programming, coding tutorial, programming tutorial, algorithms, computer science, stacks and queues, software development, stacks and queues in go, stacks and queues in golang, stacks and queues golang, stacks and queues go, coding interview
Id: fsbm1FOSDJ0
Channel Id: undefined
Length: 11min 44sec (704 seconds)
Published: Mon Jun 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.