Sunday, 13 May 2018

Stack Representation using Linked List

stack is  basically a program which works on the concept of LIFO i.e, last in first out which means last element will be deleted first.
The is all about the implementation of stack in linked list.

/*All the Pre Processors*/
#include<stdio.h>
#include<conio.h>
#include<stdio.h>

#define MAX 25

struct stack                           /*Structure of Stack*/
{
    int stack[MAX];
    int top;
};

typedef struct stack NODE;

void push(NODE *);              /*Function to push an element in Stack*/
void pop(NODE *);                /*Function to delete or pop an element*/

void main()
{
    NODE *ps;
    int i,p;
    clrscr();
    printf("\nEnter no. of element you want to push:- ");
    scanf("%d",&i);
    while(i>0)
    {
        push(ps);
        i--;
    }
    printf("\nDo you want to pop element(1-->Yes and 0-->No):- ");
    scanf("%d",&p);
    if(p==1)
    {
        pop(ps);
    }
    getch();
}

void push(NODE *pu)
{
    int item;
    if(pu->top==MAX-1)
    {
        printf("\nSTACK IS FULL");
    }
    else
    {
        printf("\nEnter value:- ");
        scanf("%d",&item);
        pu->stack[++pu->top]=item;
    }
}

void pop(NODE *po)
{
    int item;
    if(po->top==-1)
    {
        printf("\nStack is Empty");
    }
    else
    {
        item=po->stack[po->top--];
        printf("\nElement Deleted :- %d",item);
    }
}


No comments:

Post a Comment