C Program:Stack using Pointers






C program for implementing Stack operations(push/ pop) using pointers : A Stack can be defined as a list of items in which additions can be done from only one direction (usually known as top). Stacks can be implemented in two ways using C. First one is Array and second is Linked List(pointers). In a Stack a item which is usually pushed first is popped out at the last. That means Last in First Out[LIFO] or First in Last Out[FILO]. Stack has two functions, They are Push and Pop.

Push used to insert elements in a pointers list, whereas Pop used to remove elements from pointers list. We have declared two global variables stack and top and assigned max with a value of 50 entries. Stack variable is used to populate the elements of stack and top used to check status of the Stack (Empty/Full).

Push Function
1. We do insertions(push) if the Stack having some free space, so for doing insertions(push operation)
    we check push condition top==max that means if top of Stack is full we need to display Overflow(can't    
    push).
2. If the above condition fails, then insertions(push) can be done. So user will give an input which is  
    populated into stack and Top position of Stack is incremented. If we are inserting(push) an element for
    the first time it will be stored in position 0 as we have declared top=0.

Pop Function
1. We can remove(pop) an element/item form Stack if there are some elements in Stack. If there are no
    elements we cannot remove(pop), so for checking this we used condition top==-1, that means if the
    stack position = 1 then we will not be able to perform pop operation.
2. If the above condition fails then, We can perform Pop operation by decrementing the Top position of
    Stack.

Pointer Implementation of Stacks: The following code provides functions for implementation of stack operations using pointers.

PROGRAM:

//By Vikas
#include <stdio.h>
#include<stdlib.h>

int *ptr,top,size;
void create();
void push();
void pop();
void display();
void create()
{
    top=-1;
}
void push()
{
    int val;
    if(top==size-1)
    printf("\n stack is full");
    else
    {
        printf("\n Enter a element to push");
        scanf("%d",&val);
        top++;
        ptr[top]=val;
    }
}
void pop()
{
    if(top==-1)
    printf("\n Stack is Empty");
    else
    printf("\n The deleted element is %d",ptr[top]);
    top--;
}
void display()
{
    int i;
    if(top==-1)
    printf("\n Stack is empty");
    else
    {
    printf("\n Stack is ...\t");
    for(i=top;i>=0;--i)
    printf("%d\n",ptr[i]);
    }
}
void main()
{
    int ch;
    printf("\n Enter the size");
    scanf("%d",&size);
    ptr= (int *)malloc(sizeof(int) * size);

    while(1)
    {
        printf("\n1:Create\n2:Push\n3:Pop\n4:Display\n5:Exit");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:create();
            break;
            case 2:
            push();
            break;
            case 3:
            pop();
            break;
            case 4:
            display();
            break;
            case 5:
            exit(0);
            break;
            
        }
    }

}

Previous Post Next Post