Thursday, June 27, 2013

Write a C program function to perform stack operation.



#define N 50
struct stack {
                   int top;
                   int S[N];
                   };
typedef struct stack mystack;
void makeempty (mystack *pt)    //create an empty stack
{ pt->top=-1; }
int isfull (mystack *pt)      //check whether the stack is full or not
{
if(pt->top==N-1)
return(1);
else
return 0;
}
int isempty (mystack *pt)    //check whether the stack is empty or not
{
if(pt->top==-1)
return(1);
else
return 0;
}
void push (mystack *pt, int item)   //add the item in the stack
{
if(isfull(pt))
printf(“THE STACK IS FULL”);
else
{pt->top++;  //increment the top
pt->S[pt->top]=item;   //add the item
}
}
int pop (mystack *pt)     //delete an item from the stack
{
int temp;
if(isempty(pt))
printf(“THE STACK IS EMPTY”);
else
{temp= pt->S[pt->top];   //store top element in auxiliary variable
pt->top--;            //decrement ‘top’
return (temp);  
}
}
void display (mystack *pt)          //display all the item of the stack
{
int i;
if(isempty(pt))
printf(“THE STACK IS EMPTY”);
else
{for(i=pt->top; i>=0; i--)
printf(“%d\t”,pt->S[i]);
}
}
Void count (mystack *pt)            //count the number of item in the stack
{
int i, cont=0;
if(isempty(pt))
printf(“THE STACK IS EMPTY”);
else
{
for(i=pt->top; i>=0; i--)
++cont;
return (cont);
}
}

No comments:

Post a Comment