Stack Program in C

What is Stack ?

A stack is ordered collection of element in which insertion & deletions are allowed only at one end called top of stack. Stack is a linear data structure.

The elements which is inserted first in stack is removed at last from the stack. It is named stack as it behaves like a real-world stack, for example - pile of plates etc.

A real-world stack allows operation at one end only. This feature makes it LIFO  data structure. LIFO stands for last in first out.

In stack ,insertion operation is called PUSH operation and deletion operation is called POP operation. 

Stack Animated Diagram :



Basic Operations in Stack :


init() : We will initialize top to -1, since the first array index is 0.

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

push() : This PUSH operation adds a new element at the top of stack.

void push(stack *ps,int num)
{
    ps->data[++ps->top]=num;
}

pop()   : Removes the topmost element from the stack.

int pop()
{
    int num;
    num=top->info;
    top=top->next;
    printf("Popped value:%d\n",num);
}

IsEmpty() : The operation returns TRUE if the stack is empty & false otherwise. Popping from an empty stack causes an Underflow.

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

Isfull() 
:This operation returns TRUE if  the stack is full & false otherwise. Pushing into full stack causes overflow.

int isfull(stack *ps)
{
    if(ps->top==20-1)
        return 1;
    return 0;
}

peek()  :This function allows the topmost element in the stack to be displayed without  popping the element. Hence top is not decremented here.

int peek(stack *ps)
{
return ps->data[ps->top];
}

Which Condition should check before pushing elements in stack?

Before pushing an element into stack one should check weather stack is not full only when stack is static(stack is implemented using array) . If stack is not full we can push elements into stack ,otherwise pushing element in full stack causes Overflow.
If stack is implemented using linked list we will not check Isfull condition before pushing.

Implementation of stacks :

1.Static Implementation Using Array :

#include<stdio.h>
#define max 20
typedef struct stack
{
int data[max];
int top;
}stack;  // creating stack

void push(stack *ps,int num)
{
ps->data[++ps->top]=num; //pushing element in stack
}

int pop(stack *ps)
{
int num;
num=ps->data[ps->top--]; //popping element from stack and decrementing top

printf("Popped value:%d\n",num); // printing popped element
}

int isempty(stack *ps)
{
if(ps->top==-1)
return 1;
return 0;
}    //return 1 if stack is empty else return 0

int isfull(stack *ps)
{
if(ps->top==max-1)
return 1;
return 0;
}  //return 1 if stack is full else return 0

int init(stack *ps)
{
    ps->top=-1; //initialize top=-1
}

int main()    //main function
{
stack s;
init(&s);
int choice,num,i;
do
{
printf("1.Push\n2.pop\n3.Exit\n\n");
printf("Enter: ");
scanf("%d",&choice);
switch(choice)
{
case 1:if(!isfull(&s))
{
   printf("Enter value to push:");
   scanf("%d",&num);
   push(&s,num);}
   else
   printf("Overflow\n");
   break;
case 2:if(!isempty(&s))
   pop(&s);
   else
   printf("Underflow\n");
   break;
}
}
while(choice!=3);
}

Output :

1.Push
2.pop
3.Exit

Enter: 1
Enter value to push:23
1.Push
2.pop
3.Exit

Enter: 2
Popped value:23
1.Push
2.pop
3.Exit

Enter: 3

Process returned 0 (0x0)   execution time : 6.395 s
Press any key to continue.

2.Dynamic Implementation of static :

#include<stdio.h>
typedef struct node
{
    int info;
    struct node *next;
} node;
node *top;

void push(int num)
{
    node *newnode;
    newnode=(node*)malloc(sizeof(node));
    newnode->info=num;
    newnode->next=NULL;

    newnode->next=top;
    top=newnode;
}
int pop()
{
    int num;
    num=top->info;
    top=top->next;
    printf("Popped value:%d\n",num);
}
int isempty()
{
    return top==NULL;
}
int init()
{
    top=NULL;
}
int peek()
{
  return top->info;
}

int main()
{
    init();
    int choice,num;
    do
    {
        printf("1.Push\n2.Pop\n3.Peek\n4.Exit\n");
        printf("Enter: ");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            printf("Enter value to push:");
            scanf("%d",&num);
            push(num);
            break;
        case 2:
            if(!isempty())
                pop();
            else
                printf("Underflow\n");
            break;
        case 3:
            if(!isempty())
                printf("Top-> %d\n",peek());
            else
                printf("Underflow\n");
        }
    }
    while(choice!=4);
}

Output :

1.Push
2.Pop
3.Peek
4.Exit
Enter: 1
Enter value to push:90
1.Push
2.Pop
3.Peek
4.Exit
Enter: 1
Enter value to push:44
1.Push
2.Pop
3.Peek
4.Exit
Enter: 2
Popped value:44
1.Push
2.Pop
3.Peek
4.Exit
Enter: 2
Popped value:90
1.Push
2.Pop
3.Peek
4.Exit
Enter: 1
Enter value to push:23
1.Push
2.Pop
3.Peek
4.Exit
Enter: 3
Top-> 23
1.Push
2.Pop
3.Peek
4.Exit
Enter: 2
Popped value:23
1.Push
2.Pop
3.Peek
4.Exit
Enter: 4

Process returned 0 (0x0)   execution time : 34.561 s
Press any key to continue.


Post a Comment

1 Comments

Thanks,To visit this blog.