In previous post Stacks in programming and Basic Exploits : Stack Operations, we explained the functioning of stacks. Later our users ask for its code in C Programming. In this post we will write a C Program to Implement Stacks using structures.
Program is successfully compiled and run using gcc under linux environment.

Program to Implement stacks in C/C++ using structures

#include<iostream>
using namespace std;
#define STACKSIZE 5

struct bufferstack
{
int stk[STACKSIZE];
int top; // We will use it as pointer to top of the stack
} pointer; // Here pointer is struct variable, you can see here how to implement structure in C

void push(); // To push elements in stack
int  pop();  // To Pop elements in stack
void display();

int main()
{
int c;
pointer.top=-1; //Set pointer to -1
int x=1;
while(x)  //While loop to keep program in loop
  {
cout<<"\n Pointer is at : \t"<<pointer.top+1<<endl; //this line is to test, the position of pointer
cout<<"\n Please enter your choice :"<<endl<<"1: Push"<<endl<<"2: Pop"<<endl<<"3: Display"<<endl<<"4: Exit"<<endl;
cin>>c;
    switch(c)
    {

    case 1:
        push();
        break;
    case 2:
        pop();
        break;
    case 3:
        display();
        break;
    case 4:
        return 0;
    }
cout<<" \n Do you want to cotin....? press 1 or 0"<<endl;
cin>>x;
  }
}

void push()
{
    int num;
    if(pointer.top==(STACKSIZE-1))
    {
        cout<<"\n Sorry You can't push any element into stack .... ,Stack is full";
    }
    else
    {
    cout<<"\n Enter the number to push into stack";
    cin>>num;
    pointer.top+=1;
    pointer.stk[pointer.top]= num;
    
    }
}

int pop()
{
    int num;
    if(pointer.top==-1)
    {
    cout<<"\nstack is empty "<<endl;
    }
    else
    {
    num=pointer.top;
    cout<<"\n Poped number is : "<<pointer.stk[num];
    pointer.stk[num]=0; //To delete top element from stack
    pointer.top-=1; 
    }
    return num;
    
}

void display()
{
    if(pointer.top==-1)
    {
    cout<<"\n Stack is empty"<<endl;
    }
    else
    {
    for(int i=pointer.top;i>=0;i--)
        {
        cout<<"\n"<<i<<":"<<pointer.stk[i]<<endl;
        }
    cout<<pointer.top;
    }
}
    

Output of above program:

root@kali:~/Desktop/prgrm# g++ stack.cpp -o stack
root@kali:~/Desktop/prgrm# ./stack

 Pointer is at :     0

 Please enter your choice :
1: Push
2: Pop
3: Display
4: Exit
1

 Enter the number to push into stack: 3
 
 Do you want to cotin....? press 1 or 0
1

 Pointer is at :     1

 Please enter your choice :
1: Push
2: Pop
3: Display
4: Exit
2

 Poped number is : 3 
 Do you want to cotin....? press 1 or 0

 

Let’s discuss the above program to implement stacks in C/C++ Programming:

Here we use three functions void push(), int pop(); void display() and a structure named bufferstack . We set a pointer at top as discussed here .

Push Function

It’s simple to understand the functionality of push function. First we check if stack is full by using condition top== STACKSIZE-1.  If it is not full and there is space to push more elements then we will set-top position of stack to number.

Pop Function

Pop function checks if stack is empty then print stack is empty else it will decrease the position of pointer, pointing to top to -1. Which virtually reduce the size of stack. While if you check it using cout<<[<pointer.stk[position] , it will show the element at that position. If you want to delete that element permanantly or defence your code you must set the deleting element to NULL at the time you pops that element.

Please feel free to ask any question in comments.

Previous                   Index             Next

Have something to Program to Implement stacks?? Please share in comments.

Follow us on Facebook, Google Plus and Twitter.

2 COMMENTS

Comments are closed.