You are on page 1of 3

TO SHOW THE IMPLEMENTATION OF STACKS

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

void display() //FUNCTION TO DISPLAY THE STACK


{
struct node *trav;
trav= top;
if(trav==NULL)
printf(“\n LIST IS EMPTY.”);
while(trav!=NULL)
{
printf(“\n%t”,trav->info);
trav=trav->next;
}
}

void insert_beg(int i) //FUNCTION TO PUSH ELEMENT IN THE STACK


{
struct node *temp;
if(top ==NULL)
{
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next=NULL;
top =temp;
}
else
{
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next= top;
top=temp;
}
}

void delete_beg() //FUNCTION TO POP FROM A STACK


{
struct node *ptr;
ptr= top;
if(ptr==NULL)
printf(“\n LIST IS EMPTY.”);
else
{
top = top ->next;
printf(“\n ELEMENT DELETED IS-:”,ptr->info);
}
free(ptr);
}

void main()
{
top =NULL;
int q,a,b,c,d;
start:
clrscr();
printf(“\n TO PUSH IN STACK PRESS 1.”);
printf(“\n TO POP IN STACK PRESS 2.”);
printf(“\n TO DISPLAY STACK PRESS 3.”);
printf(“\n ENTER YOUR CHOICE.-:”);
scanf(“%d,&q);
switch(q)
{
case 1:

printf(“\n ENTER THE NUMBER-:”);


scanf(“%d”,&a);
insert_beg(a);
break;
case 2:
delete_beg();
break;
case 3:
display();
break;

default:
printf(“\n WRONG CHOICE ENTERED.”);
};
printf(“\n WISH TO CONTINUE(1/0).”);
scanf(“%d”,&d);
if(d==1)
goto start;
getch();
}

OUTPUT
TO PUSH IN STACK PRESS 1.
TO POP IN STACK PRESS 2.
TO DISPLAY STACK PRESS 3.
ENTER YOUR CHOICE.-:1
ENTER THE NUMBER-:21
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:1
ENTER THE NUMBER-:22
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:1
ENTER THE NUMBER-:23
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:1
ENTER THE NUMBER-:24
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:2
DELETED ELEMENT IS 24
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:3
23 22 21
WISH TO CONTINUE(1/0)….

You might also like