Add

linear implementation of stack USING STACK STRUCTURE

Stack in Data Structure


Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). There are many real-life examples of a stack. Consider an example of plates stacked over one another in the canteen.

#include< stdio.h >

#define MAX 100

/* declaration*/

typedef struct

{

    int ele[MAX];

    int TOP;

}STACK;

STACK *s;

/* function: pushItem( ), to insert an item into stack. */

void pushItem(STACK *s,int ITEM)

{

    if(s->TOP==MAX-1)

    { printf("\nSTACK is FULL.\n"); return;   }

    s->ele[++s->TOP]=ITEM;

    printf("\nITEM inserted successfully.\n");

}

/* function: popItem( ), to delete an item from stack. */

int popItem(STACK *s)

{

    int itm;

    if(s->TOP==-1)   { printf("\nSTACK is empty.\n"); return; }

    itm=s->ele[s->TOP];

    s->TOP--;

    printf("\nItem removed : %d\n",itm);   

}

/* function: dispItem( ), to display stack elements. */

void dispItems(STACK *s)

{

    int i;

    if(s->TOP == -1)

    { printf("STACK IS EMPTY."); return; }

    for(i=s->TOP;i>=0;i--)

        printf("%d->",s->ele[i]);

    printf("\n");

}

/*** main function **/

void main()

{

    int num; char dummy;

    /** initialisation**/

    s=(STACK*)malloc(sizeof(STACK));

    s->TOP=-1;

    int choice=0;

again:

    /* display stack elements **/

    printf("\nSTACK ELEMENTS :"); dispItems(s);

    printf("\nSTACK OPTIONS \n0: Exit\n1: Add item\n2: Remove item \nEnter choice :::");

    scanf("%d",&choice);

    switch(choice)

    {

        case 0:

            exit(1);

        break;

        case 1:

            printf("\nEnter an item to insert:");

            scanf("%d",&num);

            pushItem(s,num);

        break;

        case 2:

            popItem(s);

        break;

        default:

            printf("\nAn Invalid Choice !!!");

        break;

    }

scanf("%c",&dummy);

goto again;

}

Output

    STACK ELEMENTS :44->33->22->11->

    STACK OPTIONS

    0: Exit

    1: Add item

    2: Remove item

    Enter choice :::1

    Enter an item to insert:55

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.