implement a stack using two queues

G

Gene

Can any body tell me

How to implement a stack using two queues

Thax in advance

Don't you mean implement a queue using 2 stacks, which is a standard
homework problem?
 
C

CoL

Can any body tell me

How to implement a stack using two queues

Thax in advance

Hi ,
I am just giving you hint for its possible algo.

1. Use one queue to push data into...say Q-a.Make a common push() and
pop() common function passing front and rear initially NULL.

2. For pop operation,
if (Q-a.front!=Q-a.rear)
{
pop and add to Q-b;
}
else
{
pop(); //this is our output
Q-a.front=Q-b.front
Q-a.rear=Q-b.rear;
Q-b.front=Q-b.rear=NULL
}

Hope this helps.

~Col
 
S

Sam

You need to define 'stack' and 'queue'.

I think the idea is to simulate LIFO by having data structures that
works in FIFO order.
Lets assume there are 2 queues Q1,Q2
the most simple way that I think, would be
pop -
Delete item from front of Q1 and return.
push -
1. if Q1 is not empty, delete all item one by one from Q1 and insert
in Q2.
2. insert item in Q1.
3. delete all item one by one from Q2 and insert in Q1.

the Step2 ensures that the item is most recently added item would be
at first in Q1's front ...that means ..the most recent item will be
deleted when requested.
that's what the LIFO is.
 
M

Mark Bluemel

Sam said:
I think the idea is to simulate LIFO by having data structures that
works in FIFO order.
Lets assume there are 2 queues Q1,Q2
the most simple way that I think, would be
pop -
Delete item from front of Q1 and return.
push -
1. if Q1 is not empty, delete all item one by one from Q1 and insert
in Q2.
2. insert item in Q1.
3. delete all item one by one from Q2 and insert in Q1.

the Step2 ensures that the item is most recently added item would be
at first in Q1's front ...that means ..the most recent item will be
deleted when requested.
that's what the LIFO is.

I have two things to say to this

1) What the **** has this got to do with C?
2) Why would anyone outside an academic ivory tower want to waste their
time concocting something so ugly and sick?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top