Bharathiar University
Programming in C++ LabC++ Program 1Write a C++ Program to create a class to implement the data structure STACK. Write a constructor to initialize the TOP of the STACK. Write a member function PUSH() to insert an element and member function POP() to delete an element check for overflow and underflow.
In this page
Write a C++ Program to create a class to implement the data structure STACK. Write a constructor to initialize the TOP of the STACK. Write a member function PUSH() to insert an element and member function POP() to delete an element check for overflow and underflow.
In this page
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE
#include<iostream>using namespace std; #define MAX 1000 //max size for stackclass Stack{ public: int top; int myStack[MAX]; //stack array Stack() { top = -1; } bool push(int x); int pop(); void viewStack();}; //pushes element on to the stack bool Stack::push(int item) { if (top >= (MAX-1)) { cout << "Stack Overflow!!!"; return false; }else { myStack[++top] = item; cout<<item<<endl; return true; }} //removes or pops elements out of the stackint Stack::pop(){ if (top < 0) { cout << "Stack Underflow!!"; return 0; }else { int item = myStack[top--]; return item; }}
void Stack::viewStack(){ if(top<0) { cout<<"Stack has no elements\n"; } else { for(int i=top;i>=0;i--) cout<<"| "<<myStack[i]<<" |\n------\n"; }}
// main program to demonstrate stack functionsint main(){ class Stack st; int item,ch=0; do { cout<<"1.PUSH\n"; cout<<"2.POP\n"; cout<<"3.VIEW STACK\n"; cout<<"4.Quit\n"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case 1: cout<<"PUSH the Item into Stack\n"; cout<<"Enter the item:"; cin>>item; st.push(item); break; case 2: cout<<"POP the Item from Stack\n"; item=st.pop(); cout<<"Item "<<item<<" is deleted\n"; break; case 3: cout<<"STACK\n"; st.viewStack(); break; case 4: cout<<"Thank You"; break; default: cout<<"Please enter Correct choice\n"; break; } }while(ch!=4); return 0;}
OUTPUT
1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:1PUSH the Item into StackEnter the item:30301.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:3STACK
| 30 |------| 20 |------| 10 |------1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:2POP the Item from StackItem 30 is deleted1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:3STACK| 20 |------| 10 |------1.PUSH2.POP3.VIEW STACK4.QuitEnter your choice:4Thank You
SOURCE CODE FOR TUBO C++(MS DOS)
#include<iostream.h>#include<stdio.h>#include<conio.h>
#define MAX 1000class Stack{ public: int top; int myStack[MAX];
Stack() { top = -1; } void push(int x); int pop(); void viewStack();}; //pushes element on to the stack void Stack::push(int x) { if (top >= (MAX-1)) { cout << "Stack Overflow!!!";
}else { myStack[++top] = x; cout<<"Item "<<x<<"is pushed into stack"<<endl; }}
//removes or pops elements out of the stackint Stack::pop(){ if (top < 0) { cout << "Stack Underflow!!"; return 0; }else { int x = myStack[top--]; return x; }}
void Stack::viewStack(){ if(top<0) { cout<<"Stack has no elements\n"; } else { for(int i=top;i>=0;i--) cout<<"| "<<myStack[i]<<" |\n------\n"; }}
// main program to demonstrate stack functionsint main(){ class Stack st; int x,ch=0; clrscr(); do { cout<<"1.PUSH\n"; cout<<"2.POP\n"; cout<<"3.VIEW STACK\n"; cout<<"4.Quit\n"; cout<<"Enter your choice:"; cin>>ch;
switch(ch) { case 1: cout<<"PUSH the Item into Stack\n"; cout<<"Enter the item:"; cin>>x; st.push(x); break; case 2: cout<<"POP the Item from Stack\n"; x=st.pop(); cout<<"Item "<<x<<" is deleted\n"; break; case 3: cout<<"STACK\n"; st.viewStack(); break; case 4: cout<<"Thank You"; break; default: cout<<"Please enter Correct choice\n"; break; } }while(ch!=4); getch(); return 0;}
SAMPLE OUTPUT
#include<iostream>
using namespace std;
#define MAX 1000 //max size for stack
class Stack
{
public:
int top;
int myStack[MAX]; //stack array
Stack() { top = -1; }
bool push(int x);
int pop();
void viewStack();
};
//pushes element on to the stack
bool Stack::push(int item)
{
if (top >= (MAX-1)) {
cout << "Stack Overflow!!!";
return false;
}
else {
myStack[++top] = item;
cout<<item<<endl;
return true;
}
}
//removes or pops elements out of the stack
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow!!";
return 0;
}
else {
int item = myStack[top--];
return item;
}
}
void Stack::viewStack()
{
if(top<0)
{
cout<<"Stack has no elements\n";
}
else
{
for(int i=top;i>=0;i--)
cout<<"| "<<myStack[i]<<" |\n------\n";
}
}
// main program to demonstrate stack functions
int main()
{
class Stack st;
int item,ch=0;
do
{
cout<<"1.PUSH\n";
cout<<"2.POP\n";
cout<<"3.VIEW STACK\n";
cout<<"4.Quit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"PUSH the Item into Stack\n";
cout<<"Enter the item:";
cin>>item;
st.push(item);
break;
case 2:
cout<<"POP the Item from Stack\n";
item=st.pop();
cout<<"Item "<<item<<" is deleted\n";
break;
case 3:
cout<<"STACK\n";
st.viewStack();
break;
case 4:
cout<<"Thank You";
break;
default:
cout<<"Please enter Correct choice\n";
break;
}
}while(ch!=4);
return 0;
}
OUTPUT
1.PUSH
2.POP
3.VIEW STACK
4.Quit
Enter your choice:1
PUSH the Item into Stack
Enter the item:30
30
1.PUSH
2.POP
3.VIEW STACK
4.Quit
Enter your choice:3
STACK
| 30 |
------
| 20 |
------
| 10 |
------
1.PUSH
2.POP
3.VIEW STACK
4.Quit
Enter your choice:2
POP the Item from Stack
Item 30 is deleted
1.PUSH
2.POP
3.VIEW STACK
4.Quit
Enter your choice:3
STACK
| 20 |
------
| 10 |
------
1.PUSH
2.POP
3.VIEW STACK
4.Quit
Enter your choice:4
Thank You
SOURCE CODE FOR TUBO C++(MS DOS)
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define MAX 1000
class Stack
{
public:
int top;
int myStack[MAX];
Stack() {
top = -1;
}
void push(int x);
int pop();
void viewStack();
};
//pushes element on to the stack
void Stack::push(int x)
{
if (top >= (MAX-1)) {
cout << "Stack Overflow!!!";
}
else {
myStack[++top] = x;
cout<<"Item "<<x<<"is pushed into stack"<<endl;
}
}
//removes or pops elements out of the stack
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow!!";
return 0;
}
else {
int x = myStack[top--];
return x;
}
}
void Stack::viewStack()
{
if(top<0)
{
cout<<"Stack has no elements\n";
}
else
{
for(int i=top;i>=0;i--)
cout<<"| "<<myStack[i]<<" |\n------\n";
}
}
// main program to demonstrate stack functions
int main()
{
class Stack st;
int x,ch=0;
clrscr();
do
{
cout<<"1.PUSH\n";
cout<<"2.POP\n";
cout<<"3.VIEW STACK\n";
cout<<"4.Quit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"PUSH the Item into Stack\n";
cout<<"Enter the item:";
cin>>x;
st.push(x);
break;
case 2:
cout<<"POP the Item from Stack\n";
x=st.pop();
cout<<"Item "<<x<<" is deleted\n";
break;
case 3:
cout<<"STACK\n";
st.viewStack();
break;
case 4:
cout<<"Thank You";
break;
default:
cout<<"Please enter Correct choice\n";
break;
}
}while(ch!=4);
getch();
return 0;
}
SAMPLE OUTPUT