Jump to content
xisto Community
Sign in to follow this  
varalu

Data Structure -- Queue -- Implement Using Stack

Recommended Posts

Implement a Queue using a stack.

No restriction on space complexity.

 

One possible Solutions

a costly procedure...

1. Use a temp stack

2. Insertion into queue

- Push the element into the original stack

3. Deletion from queue

- Pop all the elements from stack into a temp stack

- pop out the first element from the temp stack

- pop all the remaining elements back to the original stack

 

 

What is a queue?

A queue is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.

for more... Follow the below link.

 

http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

reverse the string using two queues

Data Structure -- Queue -- Implement Using Stack

 

Reverse the string using two queues

 

-reply by mahalaxmi

Share this post


Link to post
Share on other sites

well, as u rightly said, it is a costly procedure.

let me tell you how to do it without increasing the computational cost:

use two stacks s1, and s2

start off with s1 empty and s2 full

for enque: push onto stack s1

for deque: pop from stack s2

when s2 gets empty pop all elements from s1 into s2

Implement a Queue using a stack.

No restriction on space complexity.

 

One possible Solutions

a costly procedure...

1. Use a temp stack

2. Insertion into queue

- Push the element into the original stack

3. Deletion from queue

- Pop all the elements from stack into a temp stack

- pop out the first element from the temp stack

- pop all the remaining elements back to the original stack

 

 

What is a queue?

 

 

for more... Follow the below link.

 

http://forums.xisto.com/no_longer_exists/

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.