Usage of data structures Stack and Queue

Asked By 15 points N/A Posted on -
qa-featured

Hello! I am a student of computer science. I have this question in my mind if we could actually make a queue from a stack and a stack from a queue. Since stack is Last in First Out(LIFO) and Queue is First in First Out(FIFO) so how could we make a queue by using stack which means that stack start behaving like a queue?Similarly how can we make a stack using queue which means queue starts behaving like a stack?

SHARE
Answered By 0 points N/A #183041

Usage of data structures Stack and Queue

qa-featured
A stack is a collection of elements, with two types of operations:
* push adds the element to the stack;
* pop removes the last element from the stack.
This makes the stack a Last-In-First-Out (LIFO) data structure.
 
A queue is a collection of elements, with two types of operations:
* enqueue adds an element to the rear terminal position;
* dequeue removes an element from the front terminal position.
This makes the queue a First-In-First-Out (FIFO) data structure.
 
Let's take simple examples for both the scenarios.
 
To make a Queue by using Stack
 
I need to take 2 stacks, lets say stack A and B. For stack A, if I have a list "m, n, o" and I add "p", it gets stacked at the end, so I end up with "m, n, o, p". Now I will pop an element of the list from stack A, and push it into stack B. After a pop, my list in stack A is now "m, n, o" again and the list in stack B is "p". Now, I repeat the process to pop an element from A and push it into B. So my end result in stack B would be "p, o, n, m". So, now if I pop the element from stack B, it would behave like a queue as it will pop the last element "m", that was inserted first into stack A and so on.
 
To make a Stack using Queue
 
I need to take 2 queues, lets say queue X and Y. For queue X, I have a list "a, b, c" and after adding "d", it becomes "a, b, c, d". Now when I dequeue, I have to take an element from the front of the list from queue X and enqueue it in queue Y, so it becomes "b, c, d" in X and "a" in Y. I have to repeat the process of dequeue and have to check whether it is the last element in the queue X. So, if the element in queue X is the last, i.e., "d" in this case, then I will display it. Now, queue Y will have "a, b, c" and queue X will be empty. This way, I have to repeat the process until all the elements are displayed in LIFO order.
Answered By 0 points N/A #183042

Usage of data structures Stack and Queue

qa-featured

Well as you know that queue is a FIFO and stack is a LIFO use that in your advantage to solve that problem, you can make a stack form a queue by simply getting queue data to the stack, and then again stack data which you take form a queue, and in last take queue data to the stack so it just reversed the data for you. But from stack to queue is slightly difficult but not impossible, solution for that is simply empty the stack when you are going to add an element, perform this step every time you add into stack.

Answered By 0 points N/A #183044

Usage of data structures Stack and Queue

qa-featured

A stack is a basic data structure that can be logically thought as linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. TThe basic implementation of a stack is also called a LIFO (Last In First Out) to demonstrate the way it accesses data, since as we will see there are various variations of stack implementations.

There are basically three operations that can be performed on stacks . They are 1) inserting an item into a stack (push). 2) deleting an item from the stack (pop). 3) displaying the contents of the stack(pip).

 

Queues-

A queue is a basic data structure that is used throughout programming. You can think of it as a line in a grocery store. The first one in the line is the first one to be served. A queue is also called a FIFO (First In First Out) to demonstrate the way it accesses data.

Related Questions