Answer to Question #249352 in C++ for Ahmed Ali

Question #249352
LAB TASK (STACK) 1)
1)Perform the Stack Program using Array.
2) Array Size is 2999. And Array name is BSCS Stack
3) All Variable Names will be changed.
4) Create Function after main body.
1
Expert's answer
2021-10-11T00:05:46-0400
#include <iostream>
using namespace std;


#define MAX 2999


class Stack {
	int top;


public:
	int BSCS_Stack[MAX];


	Stack() { top = -1; }
	bool push(int x);
	bool isEmpty();
	int pop();
	int peek();
	
};


bool Stack::push(int x)
{
	if (top >= (MAX - 1)) {
		cout << "Overflow";
		return false;
	}
	else {
		BSCS_Stack[++top] = x;
		cout << x << " is pushed into the stack"<<endl;
		return true;
	}
}


int Stack::pop()
{
	if (top < 0) {
		cout << "Underflow";
		return 0;
	}
	else {
		int x = BSCS_Stack[top--];
		return x;
	}
}
int Stack::peek()
{
	if (top < 0) {
		cout << "The stack is Empty";
		return 0;
	}
	else {
		int x = BSCS_Stack[top];
		return x;
	}
}


bool Stack::isEmpty()
{
	return (top < 0);
}




int main()
{
	Stack s1;
	s1.push(1);
	s1.push(2);
	s1.push(3);
	s1.push(4);


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS