Introduction
There are various types of structures to store date that can play important roles. But why do we have many structures to form more effective storage? With the same thought I play with some date structures and choose the queue and stack to renovate this advanced capability in data structures. Of course I am curious to have a tour with this. So let us proceed.
The Stacked Queue
This structure contains the basic logic of a queue and a stack. Well stacks play a major role here with the operations of push and pop with the additional operation of a queue, in other words, enqueue and dequeue. Here the stack has enqueue and dequeue methods.
Stacked Queue Operation
Operation |
Description |
Push |
Insert an item into the stack with queue |
Pop |
Pop all items in the current Stack item |
Enqueue |
Insert an item into the current stack queue item |
Dequeue |
Get an item from the current stack queue item |
Algorithms
Pop Operation
- Create a new queue
- If Space.Count is less than one then
- Assign a NewQueue as the last queue
- Remove the last item
- Set the current item
- Else
- Through an empty message
Push Operation
- Create a new queue
- Enter a value in the new queue
- Add a new queue in the stack
Enqueue Operation
- Call the current queue enqueue
Dequeue Operation
- Call the current queue dequeue
- If the last queue value then
- Remove the current queue from the stack
C# code
Operations Example
Push an item onto the stack, for example “11”; it will be stacked as the very first element in the stack.
![push 11 item]()
Now insert the next item onto the stack. For example 12 is pushed onto the stack.
![push 12 item]()
Now insert an item with the same element. For example when inserting an item “13” it will create a queue and provide you the support of the Queue operations.
![Enqueue item 13]()
Now push another element onto the stack, for example an item 14 is pushed onto the stack as a new stack element.
![push item 15]()
Now “15” is a new stack element.
![push item 15]()
And “16” is pushed onto the stack.
![push item 16]()
Now try to insert “17”, “18”, “19”, “20” with “16” and it forms a new queue and is stored in the same stack element.
![enqueue 1718f]()
Let us now see what happens when doing a Dequeue operation with this stack. It is removing the first element inserted with the current stack element.
![dequeue performed]()
Again doing the dequeue the first element is removed.
![dequeue first element]()
Let us check what happened when doing the Pop operation of the stack. Yes it will pop all there items present in the current queue element.
![poping item 19 20 18]()
After doing the pop operation of the Stack two times, “15” and “14” are popped out.
![pop 15 and 14]()
It will show an empty message when doing the dequeue three times when a stack queue element contains two items.
Advantages
- Provide an additional meaningful operation like Enqueue and Dequeue for a stack.
- Provided a structure more advanced than the existing classic structure to access the data in a more efficient manner.
- A single structure can behave like a stack or a queue.
Conclusion
Hence this Stacked queue provides the combined capabilites of both structures. That would help us to access the data in a fine way for proper memory allocation of the data.