How to Create Stack using Array
How to create Stack using Array
#include <iostream>
using namespace std;
class Stack
{
private:
int maxStackSize;
int stackTop;
int list[];
public:
Stack(int);
bool isEmptyStack();
bool isFullStack();
void push(int);
int top();
void pop();
};
Stack::Stack(int stacksize)
{
maxStackSize=stacksize;
stackTop=0;
}
void Stack::push(int n)
{
if(!isFullStack())
{
list[stackTop]=n;
stackTop++;
}
else
cout<<"Stack is full"<<endl;
}
bool Stack::isEmptyStack()
{
return(stackTop==0);
}
bool Stack::isFullStack()
{
return(stackTop==maxStackSize);
}
int Stack::top()
{
if(isEmptyStack())
cout<<"empty stack"<<endl;
else
return list[stackTop-1];
}
void Stack::pop()
{
if(!isEmptyStack())
stackTop--;
else
cout<<"Empty Stack"<<endl;
}
int main()
{
Stack stack(100);
//stack.initializeStack();
stack.push(7);
stack.push(12);
stack.push(8);
cout<< stack.top();
system("pause");
return 0;
}
By Hafiz Muhammad Umar Hayat
Comments
Post a Comment