Stack & Queue
Last updated
Was this helpful?
Last updated
Was this helpful?
βοΈAll the code snippets you will see below are just Pseudo code.
Why to use Linked Lists while implementing Stacks and Queues ?
I found some lines over this β
β¦because linked lists store data elements in linear sequences, they can be used to give alternative implementations of stacks and queues. One advantage to using linked lists is that we donβt have to worry about filling up something like an array β we can just keep allocating cells as long as we need to (unless we run out of memory). Implementing a stack using a linked list is particularly easy because all accesses to a stack are at the top. One end of a linked list, the beginning, is always directly accessible. We should therefore arrange the elements so that the top element of the stack is at the beginning of the linked list, and the bottom element of the stack is at the end of the linked list. We can represent an empty stack with nullβ¦
another reason is that you will able to practice linked-lists while implementing stacks and queues which will give you the understanding of how Stacks and Queues works while making you strong with linked-lists.
read full article over ..
This is how stack worksβ¦
Push
Pop
Peek
Reverse
Length
Search
IsEmpty
Traverse
Lets do some code nowβ¦
Initial Stack
Push Operation
Pop Operation
Peek Operation
β οΈ It is same as reversing a Singly-Linked List
Reverse Operation
Length, Search & IsEmpty Operation
Traverse Operation
Queue Definition
This is how Queue worksβ¦
Enqueue
Dequeue
Length
Peek
IsEmpty
Traverse
Initial Queue
Enqueue Operation
Dequeue Operation
Length, Peek & IsEmpty Operation
Traverse Operation
You can implement any operation over these stacks and queues which you can implement using Linked Lists.
Hackerrank β