Create a stack S with 13 elements by using 4220145971536. Consider an empty queue Q, remove the elements one-by-one from S and insert them into Q, then remove them oneby-one from Q and re-insert them into S. Finally, show the elements of S (from top to bottom). Show all the steps.
Solution:
Given,
=>Stack S with size = 13
=>Elements = 4, 2, 2, 0, 1, 4, 5, 9, 7, 1, 5, 3, 6
=>Queue Q
Explanation:
Initially:
Stack S:
6 |
3 |
5 |
1 |
7 |
9 |
5 |
4 |
1 |
0 |
2 |
2 |
4 |
top = 12
Queue Q:
0 1 2 3 4 5 6 7 8 9 10 11 12
front = rear = -1
=>top pointer in stack is used for inserting and removing the elements.
=>front pointer is used for inserting the elements into the queue and rear pointer is used for removing the elements from the queue.
Step 1:
=>pop off element 6 from stack and inert into the queue.
Stack S:
3 |
5 |
1 |
7 |
9 |
5 |
4 |
1 |
0 |
2 |
2 |
4 |
top = 11
Queue Q:
6 |
0 1 2 3 4 5 6 7 8 9 10 11 12
front = rear = 0 as it is first insertion.
Step 2:
=>Pop off element 3 from stack and insert into queue.
Stack S:
5 |
1 |
7 |
9 |
5 |
4 |
1 |
0 |
2 |
2 |
4 |
top = 10
Queue Q:
6 | 3 |
0 1 2 3 4 5 6 7 8 9 10 11 12
front = 1 and rear = 0
and so on.
Final step:
Stack S:
top = -1
Queue Q:
6 | 3 | 5 | 1 | 7 | 9 | 5 | 4 | 1 | 0 | 2 | 2 | 4 |
0 1 2 3 4 5 6 7 8 9 10 11 12
front = 12 and rear = 0
Now removing the elements from queue and insert into stack S:
Step 1:
=>Remove element 6 from Q and insert into the stack S.
Queue Q:
3 | 5 | 1 | 7 | 9 | 5 | 4 | 1 | 0 | 2 | 2 | 4 |
0 1 2 3 4 5 6 7 8 9 10 11 12
front = 12 and rear = 1
Stack S:
6 |
top = 0
Step 2:
=>Remove element 3 form queue Q and insert into stack S.
Queue Q:
5 | 1 | 7 | 9 | 5 | 4 | 1 | 0 | 2 | 2 | 4 |
0 1 2 3 4 5 6 7 8 9 10 11 12
front = 12 and rear = 2
Stack S:
3 |
6 |
top = 1
and so on
Final step:
Queue Q:
0 1 2 3 4 5 6 7 8 9 10 11 12
front = rear = -1 for last deletion.
Stack S:
4 |
2 |
2 |
0 |
1 |
4 |
5 |
9 |
7 |
1 |
5 |
3 |
6 |
top = 12
=>Elements of stack S from top to bottom = 4, 2, 2, 0, 1, 4, 5, 9, 7, 1, 5, 3, 6
I have explained each and every part with the help of statements attached to it.
Create a stack S with 13 elements by using 4220145971536. Consider an empty queue Q, remove...
Given a 5 element queue Q (from front to back: a, x, t, c, z), and an empty stack S, remove the elements one-by-one from Q and insert them into S, then remove them one-by-one from S and re-insert them into Q. Write down the content of the Q from front to back:
(ii) [6 marks] Assume that we have an empty stack S and an empty queue Q. Given a series of stack operations on S as below: Push(S,10), Push(S, 7), Push(S, 23), Pop(S), Push(S, 9), Pop(S), Pop(S) Output the element returned by each Pop operation. Given a series of queue operations on Q as below: Enqueue(0,10),Enqueue(Q,20), Enqueue(0,33), Dequeue(Q), Enqueue(Q,55), Dequeue(Q), Dequeue(Q) Output the element returned by each Dequeue operation. (iii) [8 marks] Given an empty binary search tree T, draw the...
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD
COMMENTS:
stackARRAY:
#include<iostream>
#define SIZE 100
#define NO_ELEMENT -999999
using namespace std;
class Stack {
int arr[SIZE]; // array to store Stack elements
int top;
public:
Stack() {
top = -1;
}
void push(int); // push an element into Stack
int pop(); // pop the top element from Stack
int topElement(); // get the top element
void display(); // display Stack elements from top to bottom
};
void Stack...
Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...
Using C
2.A Stack data structure with at least following functions pushO - Insert an element at one end of the stack called top. )-Remove and return the element at the top of the stack, if it is not empty. "peek0- Return the element at the top of the stack without removing it, if the stack is not empty. size0 -Return the number of elements in the stack. isEmpty0-Return true if the stack is empty, otherwise return false isFullO-Return true...
C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you are given a stack of string values like Stack s; using a Queue, cause all of the items in the stack to be reversed. For example, if you have the following contents on the stack s Top --- bird cat dog turtle ----- Bottom After you run your code, you stack contents should look like Top --- turtle dog cat bird ----- Bottom
2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current: /* create Stack stk and Queue que / Stacks: the function push places the given parameter on the top of the stack, the function pop removes and returns the top of the stack, and the function empty returns true (i.c., 1) if the stack is empty or false (i.e., 0) otherwise. Queues: the function insert places the given parameter on the end of the...
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...