Stack:
Stack is a container in which elements inserted and removed in LIFO (Last In First Out) manner.
- “top” is address of top element of stack.
- Basic stack operations are given below:
- push(): Insert an object into stack top.
- pop(): Delete object in stack top.
- top(): Get object in stack top.
Explanation of Solution
- a.
Given stack is “S” and the additional stacks are “S1” and “S2”.
The elements in stack “S” can be reverse as shown below:
- First pop each element from “S” and push it into “S1” till “S” becomes empty.
- Pop each element from “S1” and push it into “S2” till “S1” becomes empty.
- Pop each element from “S2” and push it into “S” till “S2” becomes empty.
- Now “S” contains elements in reverse order.
Example,
Initially “S” contains 1, 2, 3 and 4.
First pop each element from “S” and push it into “S1”.
Then pop each element from “S1” and push it into “S2”.
Last pop each element from “S2” and push it into “S”.
Finally “S” contains in reverse order.
Queue:
- Queue is another data structure in which insertion and removal of elements are in FIFO(First In First Out) manner.
- Basic operations are given below:
- Enqueue: Insert element into back of queue.
- Dequeue: Remove item from queue’s front.
Explanation of Solution
- b.
Given stack is “S” and additional queue is “Q”.
The elements in stack “S” can be reverse as shown below:
- Pop each element from “S” and insert it into “Q” till “S” becomes empty.
- Delete each element from “Q” and push it into “S” till “Q” becomes empty.
- Now stack “S” contains elements in reverse order.
Example,
Initially “S” contains 1, 2, 3 and 4.
First pop each element from “S” and insert it into “Q”.
Then delete each element from “Q” and push it into “S”.
Finally, “S” contains in reverse order.
Explanation of Solution
- c.
Given stack is “S”, additional stack is “S1” and additional variables are “Num”, Num2 and “top_element”. The elements in stack “S” can be reverse as shown below.
- “Num” is number of elements initially in stack “S”.
- Decrement “Num” by one.
- Till “Num” becomes zero.
- Equate “Num” into “Num2”.
- Pop stack top of “S” and store in “top_element”.
- Pop “Num2” elements from “S” and push into “S1”.
- Then push element in “top_element” into “S”.
- Then pop each element from “S1” and push into “S” till “S1” becomes empty.
- Decrement value of “Num” by one.
- Finally “S” contains elements in reverse order.
Example,
Initially “S” contains 1, 2, 3 and 4. Stack contains four elements. So, “Num” is 4. Decrement it by one.
First iteration:
“Num” is 3, pop stack top and store in “top_element”. Then pop other three elements from “S” and push into “S1”.
- Then push element in “top_element” into “S” and pop each element from “S1” and push into “S”.
Second iteration:
“Num” is 2, pop stack top and store in “top_element”. Then pop other two elements from “S” and push into “S1”.
- Push element in “top_element” into “S” and pop each element from “S1” and push into “S”.
Third iteration:
“Num” is 1, pop stack top and store in “top_element”. Then pop one element from “S” and push into “S1”.
- Push element in “top_element” into “S” and pop each element from “S1” and push into “S”
Fourth iteration:
“Num” is 0, exit loop.
Finally, the stack “S” contains elements in reverse order.
Want to see more full solutions like this?
Chapter 4 Solutions
EBK DATA STRUCTURES AND ALGORITHMS IN C
- implement a queue with 2 stacks so that each queue operation take a constant amortized number of stack operations (using java)arrow_forwardImplement a queue Q with two stacks S1 and S2, so that the stacks that operate together as a queue may perform the operations ENQUEUE(Q, X) and DEQUEUE(Q, Y), where Q appears to be a queue and X is the element to be inserted into Q and Y is the element to be removed from Q. Assume that the ADT operations PUSH, POP, and EMPTYSTACK are only available for stacks.Show how your technique works on a list a, b, c, which will be utilised as a queue by the stacks S1 and S2.arrow_forwardThe contents of the linked stack L_S and linked queue L_Q are swapped in both operations. While WHAT_IS_COOKING1 exchanges the data items of the lists, WHAT_IS_COOKING2 just manipulates the pointers and so appears more elegant.arrow_forward
- Describe how we might construct a Queue as a set of Stacks, known as a "stack pair." (Hint: Consider one of the piles to be the top of the queue and the other to be the tail.)arrow_forward10.7 Suppose you wish to reverse the order of elements of a queue. Usingonly Queue operations, describe how this would be done. (Hint: While you can’tuse a stack, you can use something similar.)10.8 Over time, the elements 1, 2, and 3 are pushed onto the stack, amongothers, in that order. What sequence(s) of popping the elements 1, 2 and 3 offthe stack are not possible?arrow_forwardImplement a stack S with two queues Q1 and Q2 so that the operations PUSH (S, X) and POP(S, Y), where S is presumably a stack and X is the element to be added to S and Y is the element removed from S, are handled by the queues that function as a stack as a whole. Assume that only queues are accessible to ADT's ENQUEUE, DEQUEUE, and EMPTYQUEUE operations.Show how your function operates on a list of items (a, b, and c) that will be used as a stack by the queues Q1 and Q2.arrow_forward
- There is one stacks and one queue q and then following operations are performed upon these two. Push A, enqueue B, push C, pop, pop, enqueue D, push E, dequeue, enqueue F .What is the total number of elements, which are left in the queue and stack in the end.arrow_forwardWhereas the unbounded stack has no such limits, could you perhaps elaborate on how this works?arrow_forwardWrite a procedure to reverse a linked stack implemented as a doubly linked list,with the original top and bottom positions of the stack reversed as bottom and top,respectively.For example, a linked stack S and its reversed version Srev are shown as follows:arrow_forward
- Find out how effectively the stack operates when it is given the opportunity to work in line with its inherent inclinations.arrow_forwardStack can be implemented using _________ and ________ ?(java) a. Array and Binary Tree b. Linked List and Graph c. Queue and Linked List d. Array and Linked Listarrow_forwardimplement queue and stack ( c, C++, array, linked list) insert delete search print all elementsarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education