Simulation of Stack using an Array in C

Below is the program for simulation of stack to understand push and pop operation in stack using an array in c. We will understand program step by step.

First we have declared variable stack, and functions init(), empty(), full(), pop(), push(), print(). Below is the detailed understanding of functions. Also take a note that, MAX size of an array mentioned here is 6.

void init(stack *) : init function initialise the stack to top -1.

int empty(stack *) : empty function checks if stack is empty or not. Return type of function is integer. Returns 1 if empty, else 0.

int full(stack *) : full function checks if stack is full or not. Return type of function is integer. Returns 1 if stack is full, else 0.
int

int pop(stack *) : pop function removes value from stack and return the stack position. Return type of function is integer.

void push(stack *, int) : push function accepts 2 parameters. First of type stack and second is of type integer. Functionally it pushes value in stack and increment position of it by 1. Function doesn't return anything, return type of it is void.

void print(stack *) : print function prints all the stack values. Return type of it is void.

void main() :
-  First it declares the stack and other variables, and clears the screen using clrscr() function.
- Next, it calls init() function to initialise the stack to -1.
- Next, It asks the user to enter their choice by giving 4 options.
  1. Push 2. Pop 3. Print 4. Quit
- Next, Based on user input it calls the Switch Cases declared. For example, if user enters 1 then it will ask user to enter a number to be stored in stack. Which will be stored in stack by calling function push(stack *, int).

#include<stdio.h>
#include<conio.h>
#define MAX 6
typedef struct stack
{
         int data[MAX];
         int top;
}stack;

void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
void print(stack *);

void main()
{
    stack s;
    int x,op;
    init(&s);
    clrscr();
    do
    {
         printf("\n\n1)Push\n2)Pop\n3)Print\n4)Quit");
         printf("\nEnter Your choice: ");
         scanf("%d",&op);
       
         switch(op)
         {
               case 1:printf("\n enter a number :");
                    scanf("%d",&x);
                    if(!full(&s))
                         push(&s,x);
                    else
                         printf("\nStack is full......");
                    break;
               case 2:
                    if(!empty(&s))
                    {
                         x=pop(&s);
                         printf("\npopped value= %d",x);
                    }
                    else
                         printf("\nStack is empty.....");
                    break;
               case 3:
                    print(&s);
                    break;
         }
    }while(op!=4);
}

void init(stack *s)
{
       s->top=-1;
}

int empty(stack *s)
{
       if(s->top==-1)
             return(1);
       return(0);
}

int full(stack *s)
{
       if(s->top==MAX-1)
             return(1);
       return(0);
}

void push(stack *s,int x)
{
       s->top=s->top+1;
       s->data[s->top]=x;
}

int pop(stack *s)
{
       int x;
       x=s->data[s->top];
       s->top=s->top-1;
       return(x);
}

void print(stack *s)
{
       int i;
       printf("\n");

       for(i=s->top;i>=0;i--)
            printf("%d  ",s->data[i]);
}

Post a Comment

0 Comments