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

Stack As Array - Complete

This C++ program implements a stack using an array and defines functions for pushing, popping, checking if the stack is empty or full, and traversing/displaying the stack items. The main function contains a menu that allows the user to choose these stack operations and input items to push. It uses global variables to track the stack size and top index and calls the appropriate functions based on the user's selection.

Uploaded by

shatha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Stack As Array - Complete

This C++ program implements a stack using an array and defines functions for pushing, popping, checking if the stack is empty or full, and traversing/displaying the stack items. The main function contains a menu that allows the user to choose these stack operations and input items to push. It uses global variables to track the stack size and top index and calls the appropriate functions based on the user's selection.

Uploaded by

shatha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// A Program that exercise the operations on Stack Implementing Array

// i.e. (Push, Pop, Traverse)

#include <iostream>
#include <stdlib.h>
using namespace std;

#define STACKSIZE 10 // int const STACKSIZE = 10;


// global variable and array declaration
int Top=-1;
int Stack[STACKSIZE];

void Push(int); // functions prototyping


int Pop(void);
bool IsEmpty(void);
bool IsFull(void);
void Traverse(void);

int main( )
{ int item, choice;
while( 1 )
{
cout<< "\n\n\n\n\n";
cout<< " ******* STACK OPERATIONS ********* \n\n";
cout<< " 1- Push item \n 2- Pop Item \n";
cout<< " 3- Traverse / Display Stack Items \n 4- Exit.";
cout<< " \n\n\t Your choice ---> ";
cin>> choice;
switch(choice)
{ case 1: if(IsFull())cout<< "\n Stack Full/Overflow\n";
else
{ cout<< "\n Enter a number: "; cin>>item;
Push(item); }
break;
case 2: if(IsEmpty())cout<< "\n Stack is empty) \n";
else
{item=Pop();
cout<< "\n deleted from Stack = "<<item<<endl;}
break;
case 3: if(IsEmpty())cout<< "\n Stack is empty) \n";
else
{ cout<< "\n List of Item pushed on Stack:\n";
Traverse();
}
break;
case 4: exit(0);
default:
cout<< "\n\n\t Invalid Choice: \n";
} // end of switch block
} // end of while loop
} // end of of main() function

void Push(int item)


{ if (Top == STACKSIZE-1)
{
cout << "the stack is overflow " << endl;
}
else {
Top++;
Stack[Top]=item; }
}
int Pop( )
{ if (Top == -1 ) {
cout << " the stack is underflow " << endl ;
}
else {
return Stack[Top--]; }

}
bool IsEmpty( )
{ if(Top == -1 ) return true else return false;
}
bool IsFull( )
{ if(Top == STACKSIZE-1 ) return true else return false;
}
void Traverse( )
{ if (Top == -1 ) {
cout << " the stack is underflow " << endl ;
}
else {
for (int i = Top ; i>= 0 ;i-- ) {
cout << Stack [i] << endl ;
}
}
}

You might also like