0% found this document useful (0 votes)
29 views

Stack (Static Dynamic)

The document defines a Stack class with methods like push(), pop(), empty(), full(), size() and display(). It implements a stack using an array with maximum size of 10. A main() function demonstrates usage of the Stack class to perform stack operations like checking size, pushing/popping elements and displaying the stack.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Stack (Static Dynamic)

The document defines a Stack class with methods like push(), pop(), empty(), full(), size() and display(). It implements a stack using an array with maximum size of 10. A main() function demonstrates usage of the Stack class to perform stack operations like checking size, pushing/popping elements and displaying the stack.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

using namespace std;


const int STACKSIZE = 10;
class stack {
private:
int arr[STACKSIZE];
int top;
public:
stack();
void push(int x);
int pop();
bool empty();
bool full();
int size();
void display();
};
stack::stack()
{
top = -1; //empty initially stack
}
void stack::push(int x)
{
if (!full())
arr[++top] = x;
else
cout << "Stack is full, Cannot push " << x << endl;
}
int stack::pop()
{
if (!empty())
return arr[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 == 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);
}

*/

You might also like