-->





Translate

stack implementation in c++ using array source code

Leave a Comment
Stack in C++ program
This c++ program has 10 functions. Each function calls on menu based selection using switch statement (functions names may very in source code). Each function and whole code has proper commented to explain the code.  This program has been tested on Visual Studio IDE.

What is a stack?
Stack is know as LIFO(Last in First Out) functionality which means a thing or an object which has been added last time will out first. Like Placing books on one an other the last book we placed will be picked first to collect all books. To now more about stack in c++ read wiki .


List of Stack Functions


  1. void createStack();
  2. void menu();
  3. void display();
  4. void deleteStack();
  5. void clearStack();                     // declaration of functions 
  6. int pop();
  7. void push(int);
  8. void findelement();
  9. int isfull();
  10. int isempty();
  11. void find_by_position();
Program brief introduction


  • Every function will be called on menu based selection
  • An integer pointer has declared for dynamic memory allocation
  • Global variable size and top has declared to track the stack status in whole program

C++ Source Code


#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int top=-1;                         // -1 shows that stack is not created
int size=0;                         // initial size is zero
int *stk;             // initialization of dynamic array of typ int [works as stack]
/////////////////////////////////////////////////////////////////////////////////////
void createstk();
void menu();
void display();
void deletestk();
void clearstk();                     // declaration of functions 
int pop();
void push(int);
void findelement();
int isfull();
int isempty();
int checkIndex=0;
void find_by_position();
 /////////////////////////////////////////////////////////////////////////////////////
void menu()                      
{
     int option;
     system("pause");
     system("cls"); 
     while(option<1||option>10)                             // continue till we select option from 1-10
     {       
     system("cls"); 
     cout<<"press 0 to find element by position\n";
     cout<<"press 1 to create stack\n";
     cout<<"press 2 to display stack\n";
     cout<<"press 3 to clear stack\n";
     cout<<"press 4 to delete stack\n";                    
     cout<<"press 5 to find element in stack\n";        
     cout<<"press 6 to remove element from stack\n" ;
     cout<<"press 7 to add element in stack\n";
     cout<<"press 8 to check whether stack is full\n";
     cout<<"press 9 to check whether stack is empty\n";
     cout<<"press 10 to exit\n";
     cin>>option;
     
     
     switch(option)
     {
      case 0:
      find_by_position();
      break;

     case 1:
     createstk();
     break;
          
     case 2:
     display();
     break;
     
     case 3:
     clearstk();
     break;
          
     case 4:
     deletestk();
     break;    
     
     case 5:
     findelement();
     break;    
     
     case 6:
     {
     if(top<0)
     cout<<"stack not created\n";                  // check to see if stack is created or not
     if(isempty()==1)
     cout<<"stack is empty\n";                     // check to see if stack is empty 
     else 
     pop();                                        // pop element
     }
     menu();
     break;
     
     case 7:
     {
     int a;                 
     if(top<0)
     cout<<"stack not created\n";                   // check to see if stack is created or not
     else 
     if(isfull()==1 )
     cout<<"stack is full\n";                       //check to see if stack is full 
     else 
     cout<<"enter element to push on stack:";       // take element to push on stack if it is not full
     cin>>a;           
     push(a);                         
     }
     menu();
     break;
     
     
     case 8:
     {
     int a;      
     if(top<0)                                        //check to see if stack is created or not
     cout<<"stack not created\n";
     else
     isfull();                                         //calling isfull funtion
     if(a==1)
     cout<<"stack is full\n";                         //if isfull function returns 1 (true) then stack is full
     else
     cout<<"stack is not full\n";                     //else stack is not full
     }
     menu();
     break;
     
     
     case 9:
     {
     int a;
     if(top<0)                                     
     cout<<"stack not created\n";                    //check to see if stack is created or not
     else
     isempty();                                      //calling isempty function
     if(a==1)                                        //if isempty function returns 1(true) then stack is empty
     cout<<"stack is empty\n";
     else
     cout<<"stack is not empty\n";                  //else stack is not empty
     } 
     menu();
     break;
 
          
                       
     case 10:
     exit(0);                                       //calling default built in function to exit program
     break;
     
     
     default:
     cout<<"invalid option";
     break;
     }    
     }
     }
 ///////////////////////////////////////////////////////////////////////////////////////////    
 void createstk()        
 {
      cout<<"enter total size of stack:";
      cin>>size;     
   stk=new int[size];                                        // take total size of stack
     do
     {
           
                         
     cout<<"how many elements do you want to enter?\n";     //take current size of stack that we are going to use yet
     cin>>top;
                         
     }
     while(top<1||top>size);                               //prompt to enter current size till size entered is less then 1
                                                           // or greater then total size
                                                           
     cout<<"enter elements\n";                             
     for(int i=0;i<top;i++)                                //for loop from index 0 to current size (top)
     {
     cin>>stk[i];                                          //input value in stack
     
     }    
     menu();                                               //function call to enter main menu again
     } 
///////////////////////////////////////////////////////////////////////////////////////////
void display()
{
     if(top<0)
     cout<<"stack not created\n";                         //check to see if stack is created
     else
     cout<<"total number of elements in stack are :"<<top;       //displays total number of elements in stack
                                                                //by diplaying current value of top
     
     cout<<"\nand are as follows\n";                           
                                                                                      
                                                                                      
     for(int i=0;i<top;i++)                                 //loop from 0 index to top
     cout<<stk[i]<<endl;                                    //displays value in stack 
     getch();
     menu();                                              //function call to enter main menu again
}

////////////////////////////////////////////////////////////////////////////////////////////
void clearstk()
{
     if(top<0)
     cout<<"stack not created\n";                          //check to see if stack is created or not
     else
     top=0;                                               //if stack is created then make top=0 and clear it
     cout<<"stack is cleared\n";
     menu();
}


////////////////////////////////////////////////////////////////////////////////////////////////
void  deletestk()
{
      if(top<0)
      cout<<"stack not created\n";                       //check to see if stack is created or not
      else
      top=-1;
      cout<<"stack is deleted\n";                        //if stack is created then make top=-1 and delete it
      menu();
}
 ///////////////////////////////////////////////////////////////////////////////////////////////
void findelement()
{
     int item,temp=0;                                 //initialize temp variable from 0 + intializing variable to enter item
                                                        //to be found + intitailaizing counter variable 'i'
     if(top<0)
     cout<<"stack not created\n";                      //check to see if stack is created or not
     else
     {
     
     cout<<"enter item you want to find\n";
     cin>>item;                                        //input item to be found in stack
     for(int m=0;m<top;m++)                                //loop starts from zero index and continue upto top
     {
             if(stk[m]==item)                          //compare each content of stack with value entere
    {
    cout<<"item found at "<<m<<endl;
     temp=1;
             }
                                                //if item found then change value of temp to false the condition
             
             
     
     }
                                                     //control comes out of loop
    if(temp==0)
    {
    
     cout<<"item not found\n";                       //if temp!=1 displays that item not found
}
}
  menu();
     
  }
 /////////////////////////////////////////////////////////////////////////////////////////
 void push(int i)                                   //take arguement
 {
  
    
     stk[top]=i;                                   //places arguement at top
     top++;                                        //increment top to show next available position
  }
 //////////////////////////////////////////////////////////////////////////////////////// 
  int pop()
  {
    
     int i=stk[top];                               //remove element from top
     top--;                                        //decrements top 
  return i;
  }  
 /////////////////////////////////////////////////////////////////////////////////// 
  int isfull()
  {
    
     if( top==size)                              //check if top is equal to total size
     return 1 ;                                  //return true i-e stack is full
     else                                       
     return 0;                                   //else return false tnat stack is not full
     }
 /////////////////////////////////////////////////////////////////////////////////    
     int isempty()
     {
         if(top==0)                             //check if top is zero
         return 1;                              //return true that stack is empty
         else
         return 0;                              //else return false that stack is not empty
     } 
/////////////////////////////////////////////////////////////////////////////////////////   
void find_by_position()
{
     int pos;
     
     if(top<0)
     cout<<"stack not created\n";
     else
     if(top==0)
     cout<<"stack is empty\n";
     else
     {
     do
     {    
     cout<<"enter position to find its content\n";
     cin>>pos;
     }while(pos<0||pos>size); 
     for(int i=0;i<size;i++)
     {
             if(i==pos)
             {
                       cout<<"item at position "<<i<<" is "<<stk[i]<<endl;
                       
              }   //if closed
     }   //for closed
            
     }  //else closed
     menu();
     } //function closed
int main()
     {
         cout<<"welcome to stack application\n";
         menu();                                //funtion call to main menu
         getch();
         }


Program Sample output

create, enter size, select number of elements to enter then enter elements
stack c++ using array output program
Program output
Push element to stack


Output video

Download Source code from Google Drive

Note: It is recommended to dry run all the code for better understanding. There is always more way to code a program. Try to improve it by adding more functions and validations. Hope this mini c++ project help the beginners.

See also: Queue Implementation Using Array C++ Source Code
More C++ projects can be found here

Simple C++ mini projects for beginners 

0 comments:

Post a Comment