What is the Stack – Its Structure,Explanation and program- LIFO

By | July 19, 2015

read what is the Stack?What is the use of Stack in C/C++?What is the LIFO?

hello friends today i will continue my programming articles.What is the Stack in C/C++ and What is the Structure of Stack? A brif introduction about hte Stack and A program of Stack with brif Explanation.also learn error in c.

STACK:

Stack is the non-premitive liner Data Structure.It is also an ADT(abstract data type).Adstract data type means that it is not a Structure Syntax. It is just hte imagnery idea to use in programing. The idea is that the insertion and delection of data fro one end Thats way it is called LIFo(Last IN First Out).

For Example Stack of book, Stack of Cloths etc…

what is Stack 2[howpk.com]

what is Stack 2[howpk.com]

Stack[howpk.com]

Stack[howpk.com]

 Fundamental learning…what is the stack..

The two Funtions that is use in the Consept of Stack is as follws…

  1. Push

  2. pop

Push:

The push that is use to push the data into to the Stack. As we know that the Stack is LIFO thats way Stak is use the indicator to indicate the top value.when the value is entered in the Stack then the indicator has been incresed.

Note:-

Every Element will be indicated by an indicator called top.Top will tell the last element position in Stack.

Pop:

The pop function is use to delete the data into the Stack.In real that is not delect the data that is only used for decresing the top value.

Stack Fun[howpk.com]

Stack Fun[howpk.com]

The Program and Algorithm off Stack are as follow….what is the stack

#include<iostream.h>                                                                                  /* Header

#include<conio.h>                                                                                                    Fils*/

class Stack                                                                                                        //Starting Class name Stack

{        private:                                                                                                   // The private part of Class that cannot access outside the class

.         int arr[5];                                                                                              //Declartion Array named as arr[5]

.         int top;                                                                                                   // Declartion of variable name as “top”

///////////////////////////////////////////////

.       public:                                                                                                     //The public part that is access any where in the program.

.       Stack()                                                                                                   //Constractor with out parameters

.       {        top=-1;                                                                                         //Assining the value of top by -1 or set :top=-1;

.       }

////////////////////////////////////////////                                    /* Defination of Push Function

.       void push()                                                                                            Push Function that is use to insert the data*/

.       {        int v;                                                                                           //Declartion a variable

.       .        if(top==4)                                                                                /*taking condition on top If(top==4)   then

.       .       {     cout<<“Stack is full”<<endl;                                         // output (read or Print)“Stack is full”

.       .       }                                                                                                   [End of If then]

.       .      else                                                                                               //else then

.       .      {    cout<<“Enter the data”;                                                    //Output(read or Print)“Enter the data”

.       .      .     cin>>v;                                                                                //Write or scan the variable (v)

.        .     .     arr[++top]=v;                                                                    //Assining the value to array and Set :arr[++top]=v;

.        .     .     cout<<top+1;                                                                    /*Output(read or print)top+1

.        .     .     cout<<“\tData pushed successfully”<<endl;            and also “Data Pushed Successfully” or \t=use for Spaces*/

.       .      }                                                                                               [End of If else then]

}

///////////////////////////////////////////////////           /* Defination of Pop Function

.        void pop()                                                                         Pop Function is used for Deleting data or Decresing the value of Top*/

.       {      if(top==-1)                                                                           // Conditional work on top like   if(top==-1) then

.       .      {    cout<<“Stack is empty”<<endl;                                 //Output(read and print)“Stack is empty”

.       .      }                                                                                             [End of if then]

.       .     else                                                                                            //Else    then

.       .     {     cout<<“Data is Deleted Successfully”<<endl;          //Output Work again here

.       .      .     top–;                                                                                  //Decresing work

.       .      }                                                                                             [End of If Else Function]
///////////////////////////////////////////////////////        /* Defination of show Function

.       void show()                                                                             Show Function is use to Show all the data*/

.       {      int q=top;                                                                            //Set :Q=top and also the Declarction of Q

.       .      while(q>-1)                                                                        // while (q>-1) repeart next two steps

.       .      {      cout<<q+1<<“\t”<<arr[q]<<endl;                     //Output

.       .       .      q–;                                                                             //Decresing statement  or Set :q=q-1;

.       .       }                                                                                         [End of loop]
}
};
//////////////////////////////////////////////////////////////

void main()                                                                            // void main(){where the Compile start to read}

{      clrscr();

.       int x;                                                                                     // Declartion of (X) as the data type off int;

.      char e,d;                                                                              //Declartion of (E,D) as the data type off char;

.     Stack s;                                                                                  //object off Stack named as (S)

.     do                                                                                                 //do repeat all step to while(d==’y’)

.     {    cout<<“Press ‘I’ if You want to Enter the data”<<endl;               /*

.     .    cout<<“Press ‘D’if You want to delect the data”<<endl;                    Output

.     .    cout<<“Press ‘S’ if YOu want to show the data”<<endl;                                Statements*/

.     .    cin>>d;                                                                                                    //write and scan (D)

.     .    switch (d)                                                                                      //switch(d) then  

.     .    {      case ‘i’:                                                                                 //case ‘i’:       evaluate  (d==’i’) then

.     .     .     {     s.push();                                                                     //Calling push Function by using object (S)

.     .     .     .      break;

.     .     .     }                                                                                           [End of Case ‘i’:]

.     .     .     case ‘d’:                                                                                 //case ‘d’:      evaluate (d==’d’) then

.     .     .    {     s.pop();                                                                          //calling pop Function by using object (S)

.     .     .     .     break;

.     .     .     }                                                                                               [End of Case ‘d’:]

.     .     .     case ‘s’:                                                                                     //case ‘s’:    evaluate(d==’s’) then

.     .     .    {    s.show();                                                                          //calling show Function by using the Object (S)

.     .     .     .    break;

.     .     .     }                                                                                                  [End of Case ‘s’:]

.     .     }                                                                                                         [End of switch]

.     .     cout<<“press ‘Y’ if You want to Continou”<<endl;             //write and print this Statement

.     .     cin>>e;                                                                                         //read and scan e;

.     }while(e==’y’);                                                                                    [End of do-while loop]
}                                                                                                                          Exit;

 

i hope this is helpful to you.

what is the stack

 

Author: Awais Shafique

Hello My name is Awias.I am Student of BSCS in GCUF. I am the Senior Contributor of this website.