Thursday, February 16, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 1 - C++ Program to implement stack data structure

 Bharathiar University

Programming in C++ Lab
C++ Program 1
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 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





Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...