Simulating a stack of push, pop and peak in C++ using array of 1000.
Here is the c++ code for generic Stack class implementation with push, pop and peak functions:
#include <iostream>
using namespace std;
template <typename T>
class Stack
{
T *array;
int stackTop;
int maxStackSize;
public:
//default constructor
Stack()
array = new T[1000];
stackTop = -1;
maxStackSize = 1000;
//function to push element to stack
void push(T input)
stackTop++;
if (stackTop == maxStackSize)
std::cout << "Stack Overflow!" << std::endl;
stackTop--;
else
array[stackTop] = input;
//function to return top element
T peak()
if (stackTop == -1)
std::cout << "Stack is empty" << std::endl;
return -1;
else
return array[stackTop];
//function to pop
void pop()
if (stackTop == -1)
std::cout << "Stack is empty" << std::endl;
else
stackTop--;
//function to check is stack is empty
bool isEmpty()
if (stackTop == -1)
return true;
return false;
//destructor
~Stack()
delete[] array;
};
//driver function
int main()
{
Stack<int> s;//object s of type integer Stack
for (int i = 0; i < 20; i += 2)//push 0, 2, 4, ..., 18 in stack
s.push(i);
while (!s.isEmpty())
cout << s.peak() << endl;
s.pop();
}
Sample output:
Simulating a stack of push, pop and peak in C++ using array of 1000.
Simulating a stack of push, pop and peak in C++ using array of 1000.
Consider these functions: push() : push an element into the stack pop() : pop the top-of-the-stack element top() : returns the item stored in top-of-the-stack-node What will be the output after performing these sequence of operations (after performing top()) push(20); push(4); pop(); push(10); push(6); pop(); pop(); push(5); top();
I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...
Stack manipulation: a) The following operations are performed on a stack: PUSH A, PUSH B, POP, PUSH C, POP, PUSH D, POP, PUSH E, POP, PUSH F What does the stack contain after each operation? 1 b) If the input stream is ZYXWV, create a sequence of pushes and pops such that the output stream is XYVWZ. (Note: The input stream of a stack is a list of all the elements we pushed onto the stack, in the order that...
Please write a C++ code for 2D matrix by using STACK push and pop function.
Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...
Suppose we execute the following stack operations on a stack of ints. push(1); pop(); // #1 push(10); pop(); // #2 push(7); push(4); push(3); pop(); // #3 push(5); pop(); //#4 Write the final state of the stack, and for each pop() operation, write the value that will be popped off the stack (pops are numbered so you can refer to them).
C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...
Say R1 = 5, R2 = 6. Push or pop the following registers to the stack and see if they are equal. Specify the top and bottom of the stack. push {r2, r1} pop{r2, r1} AND push {r1, r2} pop{r2} pop{r1} AND Push {r1} Push{r2} Pop {r1, r2} Are these operations equivalent?
What would happen to the time complexity (Big-oh) of the methods (push, pop) in an array implementation of the stack if the top of the stack were at position 0? Explain.