Note
######
In case of any doubt please comment
//#####################################################################
Given mystery(14)
Inside mystery() function first while loop will be
executed
First while loop:
when num=14
Here 14>8
so 14 will be inserted to queue --> que(14)
num=14-2=12
when num=12
Here 12>8
so 12 will be inserted to queue --> que(14,12)
num becomes 10
when num=10
similar to above
10 will be inserted to queue -->que(14,12,10)
num becomes 8
when num=8
Here 8 is not greater than 8, so else loop will execute
8 will be pushed to stack--> stk(8)
num becomes 6
similary for different
values of num 6, 4,2, num will be pushed to stack
so stack becomes--> stk(2,4,6,8)
In second while loop:
when stack is not
empty:
pop(stk) gets 2 and makes -->stk(4,6,8)
insert 2 to que makes---> que(14,12,10,2)
print(2)
Similary 4,6,8 will be poped and inserted to que
At the end stack--> stk()
queue--> que(14,12,10,2,4,6,8)
print(4,6,8)
print(newline)
In third while loop:
while queue is not
empty
remove each element from fornt of queue and push to stack
At the
end stack--> stk(8,6,4,2,10,12,14)
queue--> que()
print(14,12,10,2,4,6,8)
print(newline)
In fourth while loop
while stack is not
empty
pop an element from stack
insert the element to queue only if stack is not empty after
pop
At the end stack-stk()
queue --> que(8,6,4,2,10,12)
print(8,6,4,2,10,12,14)
print(newline)
In fifth while loop
while queue is not
empty
remove an element from queue and print the element
At the end stack-stk()
queue -->que()
print(8,6,4,2,10,12)
So final result is
2 4 6 8
14 12 10 2 4 6 8
8 6 4 2 10 12 14
8 6 4 2 10 12
//############################################################################################
When que is priority queue
***************************
Given mystery(14)
Inside mystery() function first while loop will be
executed
First while loop:
when num=14
Here 14>8
so 14 will be inserted to queue --> que(14)
num=14-2=12
when num=12
Here 12>8
so 12 will be inserted to queue --> que(14,12)
num becomes 10
when num=10
similar to above
10 will be inserted to queue -->que(14,12,10)
num becomes 8
when num=8
Here 8 is not greater than 8, so else loop will execute
8 will be pushed to stack--> stk(8)
num becomes 6
similary for different
values of num 6, 4,2, num will be pushed to stack
so stack becomes--> stk(2,4,6,8)
In second while loop:
when stack is not
empty:
pop(stk) gets 2 and makes -->stk(4,6,8)
insert 2 to que makes---> que(14,12,10,2)
print(2)
Similary 4,6,8 will be poped and inserted to que
At the end stack--> stk()
queue--> que(14,12,10,2,4,6,8)
print(4,6,8)
print(newline)
In third while loop:
while queue is not
empty
remove
largest element from queue and push it to stack
At the end
queue will become-->que()
stack--> stk(2,4,6,8,12,14)
print(14,12,10,8,6,4,2)
print(newline)
In fourth while loop
while stack is not
empty
pop an element from stack
insert the element to queue only if stack is not empty after
pop
At the end stack-stk()
queue --> que(2,4,6,8,10,12)
print(2,4,6,8,10,12,14)
print(newline)
In fifth while loop
while queue is not
empty
remove an element from queue and print the element
At the end stack-stk()
queue -->que()
print(2,4,6,8,10,12)
So final result is
2 4 6 8
14 12 10 8 6 4 2
2 4 6 8 10 12 14
2 4 6 8 10 12
2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current:...
Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in Stack class and Queue interface. The Stack class is a generic class containing these methods: public void push(E value) - pushes a new value onto the Stack public E pop() - pops the next value off of the stack and returns it; throws EmptyStackExceptionl if stack is empty public E peek() - returns the next value on the Stack but does not pop it...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially 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...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...
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...
Create a C++ program. Include comment, input and output
file.
STACK IMPLEMENTATION USING AN link
list
IMPLEMENT THE FOLLOWING STACK OPERATIONS using a
TEMPLATED CLASS.
WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the
OPERATIONS.
OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK.
Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...
Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then...
Define a class DoubleStack which implements two stacks of objects of type Object using a single shared array, so that the push and pop operations specify which of the two stacks is involved in the operation (as described below). (a) Specify necessary instance variables and write a constructor “DoubleStack(int n)” that takes integer n and creates an empty DoubleStack object with the shared array with room for n elements (2 pt); b) Write methods "boolean push(int i, Object o)" and...
For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*; public class GFG { // This is the temporary stack static Stack<Integer> res = new Stack<Integer>(); static Stack<Integer> tmpStack = new Stack<Integer>(); //...
help finish Queue, don't think
I have the right thing.
# 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...