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
If this is a homework assignment, please consider doing this yourself first, otherwise you will lose the value of the reinforcement of the lesson that doing the homework provides.
To reverse the elements of a stack, you do not need two additional queues. You either need one additional queue, or you need a second stack.
The algorithm using an additional queue is to pop the elements off of the stack and place them in a queue. Once the stack is empty, read the queue and push the elements back onto the stack. When done, the stack will be in reverse order.
The algorithm using a second stack is very similar. Pop the elements off of the first stack and push them onto the second stack. When done, replace the first stack with the second stack.
A stack is a last-in-first-out type of data structure. A queue is a first-in-first-out type of data structure. The two types are sufficiently inconsistent to make the question unanswerable - the question does not make sense.
If anyone knows an acceptable alternative usage, please feel free to refine the answer.
It is not possible to efficiently construct a queue (first in, first out storage structure) using two or more stacks (last in, first out storage structures). I leave the proof of this as an exercise, but the generalized pidgeonhole principle may be useful in understanding why this is so.
However, a queue can be implemented using stacks in an inefficient way as follows: when items are pushed onto the queue, put them on top of stack #1. When an item is retrieved from the queue, (1) repeatedly pop items off of stack #1 and push them onto stack #2, thereby reversing their order; (2) pop the top item off stack #2 and return it to the caller; (3) set a boolean flag for future reference so you know to reverse step #1, when items needs to be pushed onto the queue again.
Does that answer your homework question?
You could store the data in an array, and maintain a variable that points to the bottom of stack (i.e., the array index of the last element you added).
You could store the data in an array, and maintain a variable that points to the bottom of stack (i.e., the array index of the last element you added).
You could store the data in an array, and maintain a variable that points to the bottom of stack (i.e., the array index of the last element you added).
You could store the data in an array, and maintain a variable that points to the bottom of stack (i.e., the array index of the last element you added).
Consider two stacks p1 and p2 of same size.
Enqueue:
Push element into p1. If p1 is full, then pop all elements from p1 and push it into p2. Now push the new element into p1.
Dequeue:
Pop element from p2. If p2 is empty, the pop all elements from p1 and push it into p2. Now pop element from p2.
There are two methods on how to implement queue by using two stacked in Java. First by making enqueue operation cost and second one by making dequeue operation costly.
Implement a generic stack-type, and create two instances of it.
Please don't quote homework assignments as questions.
You could store the data in an array, and maintain a variable that points to the bottom of stack (i.e., the array index of the last element you added).
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.
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)
Two little problems: 1. stack doesn't have a flow-chart 2. there are no flow-charts in a C program
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.
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.
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.
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.
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)
Two possible solutions: 1. Separated queue for every possible priority value. 2. One shared queue for every elements, sorted by priority.
we can do sorting by using two interfaces like comparator and comparable
identification and authentication
identification and authentication
Two little problems: 1. stack doesn't have a flow-chart 2. there are no flow-charts in a C program
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....
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.
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.
There are two types of plumbing stacks: A stack vent and a soil or waste stack.