DEV Community

Discussion on: thank u, next: an introduction to linked lists

Collapse
 
rhymes profile image
rhymes

I Got myself misleaded when you said you can implement stacks and queues on top od linked list. What do you mean by that?

Super quick, a stack is a stack of books. You can only put books on top and remove them from the top. So the first book you put is always the last to come in, because after a few books are added it will be at the bottom. To get to that you need to get each and every book sitting on top.

A linked list is a collection of nodes with data in which every node points to something, except the first one that points to nothing.

If you see the example Ali made:

to get to Ariana Grande (the book at the bottom in our stack example) you need to go through all the ex boyfriends. You would have to implement the two operations push and pop instead of remove_value which removes an arbitrary node (forbidden in a stack) but I hope you can see how you can map a stack on top of a linked list.

A queue is a similar concept. The difference between a stack and a queue is that a stack is last-in-first-out (the last boyfriend you add to the structure is the first one to go), a queue is first-in-first-out (the last boyfriend you add to the structure is the last one to go, in this case it's more similar to real-life).

I hope it's a bit clearer