Saturday, August 21, 2010

Multiple Stacks Implementation

GXTechno Tags: , , , ,

#include<stdio.h>
#include<conio.h>

//Check whether stack is empty
int isempty(int *top,int *base,int stk)
{
    if((top[stk-1]==-1)||(top[stk-1]==base[stk-1]))//condition if the stack is empty
      return(1);
    else
      return(0);
}

//Check whether stack is full
int isfull(int *top,int *base,int stk,int total)
{
    if((top[stk-1]==total-1)||(top[stk-1]==base[stk]))//if the stack is full
      return(1);
    else
      return(0);
}

//Push operation(adds element) in stack
void push(int *array,int *top,int *base,int stk,int total,int info)
{
    if(isfull(top,base,stk,total))//check if stack is full
    {
      puts("\n\t*****Stack is full*****\n");
      return;
    }
    top[stk-1]+=1;//first increement the top pointer befor addition

    array[top[stk-1]]=info;//then push the value on to the stack
}

//Pop operation(deletes element) in stack
int pop(int *array,int *top,int *base,int stk)
{
    int temp;
    if(isempty(top,base,stk))//check if stack is empty
    {
      puts("\n\t*****Stack is empty*****\n");
      return(NULL);
    }
    temp=array[top[stk-1]];//first extract the value from the stack

    top[stk-1]-=1;//then decreement the pointer

    return(temp);//free the memory
}

//Display stack
void display(int *array,int *top,int *base,int stk)

No comments:

Post a Comment

Search This Blog