Stack using array

 

Stack using array






OURSHOPKEEPER












#include <iostream>
using namespace std;

void push();
void pop();
void peek();
void display();

#define SIZE 10

int stack[SIZE];
int top = -1;

void push()
{
   int value;
   cout << "Enter the element to insert: " << endl;
   cin >> value;
    if (top == SIZE - 1)
              cout<<"\nOverflow. Stack is Full"<<endl;
   else
   {
      top++;
      stack[top] = value;
      cout<<"\nInsertion was successful"<<endl;
   }
}
void pop()
{
   if (top == -1)
      cout<<"\nUnderflow. Stack is empty"<<endl;
   else
   {
      cout<<"\nDeleted : "<< stack[top]<<" ";
      top--;
   }
}

void peek()
{
   if (top == -1)
   {
      cout<<"\n The stack is empty"<<endl;
   }
   else
   cout<< stack[top];
}
void display()
{
   int i;
   cout << "Element of stack are:" << endl;
   for (int i = top; i>=0; i++)
   {
      cout << stack[i]<<" ";
   }
}
int main()
{
   int choice;
   while (1)
   {
      cout << "\n1.puch\n2.pop\n3.peek\n4.display\n5.exit" << endl;
      cin >> choice;
      switch (choice)
      {
      case 1:
         push();
         break;
      case 2:
         pop();
         break;
      case 3:
         peek();
         break;
      case 4:
         display();
         break;
         case 5:
         exit(0);
      default:
         cout << "invalid" << endl;
      }
   }
   return 0;
}





    


Click on the above button to download the code.



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post