Stack (Static Dynamic)
Stack (Static Dynamic)
}
bool stack::empty()
{
if (top == -1)
return true;
else
return false;
}
bool stack::full()
{
if (top + 1 == STACKSIZE)
return true;
else
return false;
}
int stack::size()
{
return top;
}
void stack::display()
{
if (top == -1)
{
cout << "No elements to display" << endl;
return;
}
for (int i = 0; i <=top; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
stack mystack;
int option = 0;
do {
cout << "1) stack size" << endl;
cout << "2) Push in stack" << endl;
cout << "3) Pop from stack" << endl;
cout << "4) Display stack" << endl;
cout << "5) check stack status" << endl;
cout << "6) Exit" << endl;
cin >> option;
switch (option)
{
case 1:
cout << mystack.size() << endl;
break;
case 2:
int input;
cout << "Enter Data" << endl;
cin >> input;
mystack.push(input);
break;
case 3:
cout<<mystack.pop();
cout << endl;
break;
case 4:
mystack.display();
cout << endl;
case 5:
if (mystack.full())
cout << "stack is full" << endl;
if (mystack.empty())
cout << "stack is empty" << endl;
break;
case 6:
cout << "Exit" << endl;
break;
default:
cout << "Invalid Choice" << endl;
}
} while (option != 6);
}
/*
#include <iostream>
using namespace std;
class stack {
private:
int top;
int maxSize;
char *contents=NULL;
public:
stack(int stack_size);
~stack();
void push(char x);
char pop();
bool empty();
bool full();
int size();
void display();
};
stack::stack(int stack_size)
{
contents = new char[stack_size];
top = -1; //empty initially stack
maxSize = stack_size;
}
stack::~stack()
{
delete[] contents;
contents = NULL;
top = -1;
}
void stack::push(char x)
{
if (!full())
contents[++top] = x;
else
cout << "Stack is full, Cannot push " << x << endl;
}
char stack::pop()
{
if (!empty())
return contents[top--];
else
cout << "Stack is empty, cannot pop" << endl;
}
bool stack::empty()
{
if (top == -1)
return true;
else
return false;
}
bool stack::full()
{
if (top + 1 == maxSize)
return true;
else
return false;
}
int stack::size()
{
return top;
}
void stack::display()
{
if (top == -1)
{
cout << "No elements to display" << endl;
return;
}
for (int i = 0; i <= top; i++)
cout << contents[i] << " ";
cout << endl;
}
int main()
{
stack mystack(10);
int option = 0;
do {
cout << "1) stack size" << endl;
cout << "2) Push in stack" << endl;
cout << "3) Pop from stack" << endl;
cout << "4) Display stack" << endl;
cout << "5) check stack status" << endl;
cout << "6) Exit" << endl;
cin >> option;
switch (option)
{
case 1:
cout << mystack.size() << endl;
break;
case 2:
char input;
cout << "Enter Data" << endl;
cin >> input;
mystack.push(input);
break;
case 3:
cout << mystack.pop();
cout << endl;
break;
case 4:
mystack.display();
cout << endl;
case 5:
if (mystack.full())
cout << "stack is full" << endl;
if (mystack.empty())
cout << "stack is empty" << endl;
break;
case 6:
cout << "Exit" << endl;
break;
default:
cout << "Invalid Choice" << endl;
}
} while (option != 6);
}
*/