About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

Write a C program using pointers to implement a stack with all the operations.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
int size;
/* Defining the stack structure */
struct stack
{
    int arr[MAX];
    int top;
};
/* Initializing the stack(i.e., top=-1) */
void init_stk(struct stack *st)
{
    st->top = -1;
}
/* Entering the elements into stack */
void push (struct stack *st,int num)
{
    if(st->top == size-1)
        {
        printf("\nStack overflow(i.e., stack full).");
        return;
        }
    st->top++;
    st->arr[st->top] = num;
}
//Deleting an element from the stack.
int pop(struct stack *st)
{
    int num;
    if(st->top == -1)
        {
        printf("\nStack underflow(i.e., stack empty).");
        return NULL;
        }
    num = st->arr[st->top];
    st->top--;
    return num;
}
void display(struct stack *st)
{
    int i;
    for(i=st->top;i>=0;i--)
        printf("\n%d",st->arr[i]);
}
int main()
{
    int element,opt,val;
    struct stack ptr;
    init_stk(&ptr);
    printf("\nEnter Stack Size :");
    scanf("%d",&size);
    while(1)
    {
        printf("\n\n\tSTACK PRIMITIVE OPERATIONS");
        printf("\n1.PUSH");
        printf("\n2.POP");
        printf("\n3.DISPLAY");
        printf("\n4.QUIT");
        printf("\n");
        printf("\nEnter your option : ");
        scanf("%d",&opt);
        switch(opt)
        {
            case 1:
                printf("\nEnter the element into stack:");
                scanf("%d",&val);
                push(&ptr,val);
                break;
            case 2:
                element = pop(&ptr);
                printf("\nThe element popped from stack is : %d",element);
                break;
            case 3:
                printf("\nThe current stack elements are:");
                display(&ptr);
                break;
            case 4:
                exit(0);
            default:
                printf("\nEnter correct option!Try again.");
        }
    }
return(0);
}

Output :

Explanation of Program :

We are declaring the stack with MAX size specified by the preprocessor.
struct stack
{
    int arr[MAX];
    int top;
};
As soon as after defining the stack we are initializing the top location of stack to -1. We are passing the structure to the function using pointer thus we can see the “struct stack*” as data type in the function call.
void init_stk(struct stack *st)
{
    st->top = -1;
}
Whenever we are accessing the member of the structure using the structure pointer then we are using arrow operator. If we have to access the top element of stack then we are writing
st->top
Similarly if we have to access oth element of the stack array then we can write -
st->arr[0]
Since we have to access the topmost element we can write it as -
st->arr[st->top]

Procedure of Pushing :

  1. Check whether the size reached upto the maximum or not,
  2. Increment the top
  3. Insert the element.
Similar steps are depicted in the code -
void push (struct stack *st,int num)
{
    if(st->top == size-1)
        {
        printf("\nStack overflow(i.e., stack full).");
        return;
        }
    st->top++;
    st->arr[st->top] = num;
}

Procedure of Poping element :

  1. Check whether the size reached upto the minimum or not(underflow)
  2. Remove Element
  3. Decrement the top
int pop(struct stack *st)
{
    int num;
    if(st->top == -1)
        {
        printf("\nStack underflow(i.e., stack empty).");
        return NULL;
        }
    num = st->arr[st->top];
    st->top--;
    return num;
}

No comments: