Given an empty stack s, an input array a = {1,2,3,4}. What is printed when the following function is executed?
public void pushAndPop(Stack s, int[] a){
s.push(a[0]);
s.push(a[1]);
System.out.println(s.pop());
s.push(a[2]);
System.out.println(s.pop());
s.push(a[3]);
System.out.println(s.pop());
System.out.println(s.pop());
}
A.2, 1, 4, 3 |
|
B.2, 3, 1, 4 |
|
C.1, 2, 3, 4 |
|
D.2, 3, 4, 1 |
ANSWER:-
OPTION (D) 2, 3, 4, 1
Explanation :- first statement define that ,push first element of array in the stack means push 1 in the stack so top of the stack will be 1
second statemnet define that ,push second element of array in the stack , means push 2 in the stack so top of the stack will be 2
third statement pop the element from stack ,means pop the top element of stack .currently top element of stack is 2 so poped 2 in stack
fourth statement define that ,push third element of array in the stack , means push 3 in the stack so top of the stack will be 3
fifth statement pop the element from stack , means pop the top element of stack . currently top element of stack is 3 so poped 3 in stack
sixth statement define that ,push fourth element of array in the stack , means push 4 in the stack so top of the stack will be 4
seventh statement pop the element from stack , means pop the top element of stack . currently top element of stack is 4 so poped 4 in stack
eightth statement pop the element from stack , means pop the top element of stack . currently top element of stack is 1 so poped 1 in stack
Given an empty stack s, an input array a = {1,2,3,4}. What is printed when the...
stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...
A stack S of integers initially contains the following data: 6 (top) 2 7 3 The following code is then executed: int x = S.pop(); int y = S.pop(); int z = S.pop(); S.push(x + y); int w = S.pop(); S.push(w + z); After this code has been executed, what are the contents of the stack? A. 8 10 B. 10 8 C. 15 3 D. 10 8 6 3
Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15 points The purpose of this lab is to implement and test a static and dy namic stack Part 1: Static Stack In this part, you are going to design a stack of characters. Assume a simple static array implementation. Complete the code below as specified by the comments below const int MAX- 5 // define an alias for the element type class Stack private:...
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...
Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...
Please answer question 8.35 Python 3.0 for language Comment for each part of line of code explaining what is being done 8.35 A stack is a sequence container type that, like a queue, supports very restrictis methods: All insertions and removals are from one end of the stack, typicly aces the top of the stack. Implement container class Stack that implements cked to a a subclass of object, support the len) overloaded operator, and support the me be referred a...
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...
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...