answersLogoWhite

0

implementing stack using two queues

initially q1 is full and q2 empty

1] transfer elements from q1 to q2 till last element left in q1

2] loop till q2 is not empty

deque element from q2 and again push in q2 till last element is left in q2

transfer this last element in q1

reduce size of q2 by 1

3]finally q1 contains all elements in reverse order as in stack

eg

1]

q1 = 1 2 3 4

q2 =

2]

3 deques till last element left in q1

q1 = 4

q2 = 1 2 3

3]

deque and again queue q2 till last element left

q1 = 4

q2 = 3 1 2

4] deque q2 and queue q1

q1 = 4 3

q2 = 1 2

5] again

deque and queue q2 till last element left

q1= 4 3

q2= 2 1

6] queue q1

q1= 4 3 2

7] queue last element of q2 to q1

q1= 4 3 2 1

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

Can implement circular lists using stack?

No. A stack is a data structure that allows insertion and removal at the top. A circular list allows insertion and removal anywhere in the list. The two types of data structure are too different to be reasonably implementable in terms of each other.


What is stacks in data structure using c?

queue is an abstract data type that which entities in a collection are kept in order, this makes the sense of fifo(first in first out). stack is a container of the object which works on the principle of lifo(last in first out)


What is flowchart of stack using array in c prog?

Two little problems: 1. stack doesn't have a flow-chart 2. there are no flow-charts in a C program


How do you determine symmetry of a data structure For example how do you know if a Stack or a queue or a priority queue or a double ended queue are symmetric?

You can determine symmetry of a data structure in two ways. One is when the stacks and queues data are put in the application and when the stacks are put in during run-time.


Why stack is implemented by link list?

A Linked-List is basically a series of Nodes. Each Node contains two things: The contents, and the pointer to the next Node in the Linked-List. So you can traverse the Linked-List by following the "next" pointers in each Node, a bit like following road directions from city to city. A stack is an abstract data type where you have two operations: "push" and "pop". Pushing means to put an item in the stack, Popping means to get the first element of the stack. When you push an item onto a stack, you put the item at the top: so its like cutting in line to the very front. The last one in is now first, and thus, the first one out. Another helpful image is a "stack" of trays at a cafeteria -- you can only get the tray from the top of the stack, or put a tray on top of the stack. The very first tray in the stack is actually the one at the very bottom, and thus, the last one to be used. "First in, Last Out." A stack deals with what comes first/last, while a Linked-List describes how data is stored. A stack needs to store data, so a stack can be implemented as a Linked-List.

Related Questions

How can you implement a queue using stacks efficiently?

To implement a queue using stacks efficiently, you can use two stacks. One stack is used for enqueueing elements, and the other stack is used for dequeueing elements. When dequeueing, if the dequeue stack is empty, you can transfer elements from the enqueue stack to the dequeue stack to maintain the order of elements. This approach allows for efficient implementation of a queue using stacks.


What is the relationship between a stack and an array?

There is no inherent relationship between the two. It's possible to implement a stack using an array to store date, but that's about it.


Can implement circular lists using stack?

No. A stack is a data structure that allows insertion and removal at the top. A circular list allows insertion and removal anywhere in the list. The two types of data structure are too different to be reasonably implementable in terms of each other.


What is stacks in data structure using c?

queue is an abstract data type that which entities in a collection are kept in order, this makes the sense of fifo(first in first out). stack is a container of the object which works on the principle of lifo(last in first out)


How many minimum no of queues Rae needed to implement priority queue?

Two possible solutions: 1. Separated queue for every possible priority value. 2. One shared queue for every elements, sorted by priority.


What interface do you implement to do the sorting?

we can do sorting by using two interfaces like comparator and comparable


What is flowchart of stack using array in c prog?

Two little problems: 1. stack doesn't have a flow-chart 2. there are no flow-charts in a C program


What systems implement access controls using a two-phase process called what?

identification and authentication


What systems implement access controls using a two phase process called what?

identification and authentication


How can we do push and pop operations on both sides of stack?

This is not possible as this violates the basic definition of stack...That can have only two primitive operations "Push" "POP" If u want to implement what u want u can do that by some implementation of Push pop combination but that is what other data strutures like queue do....


How do you determine symmetry of a data structure For example how do you know if a Stack or a queue or a priority queue or a double ended queue are symmetric?

You can determine symmetry of a data structure in two ways. One is when the stacks and queues data are put in the application and when the stacks are put in during run-time.


Write a sequence of instruction to exchange two register contents using stack in 8086 processor?

To exchange two registers, say the BX and CX registers, in the 8086 using the stack, you can use...PUSH BXPUSH CXPOP BXPOP CX... Of course, this is for 16 bit operation. If you want 8 bit operation, you will need to do more than that, because stack operations are always 16-bit operations.