Linked list implementation of stack
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct stack
{
int info;
struct stack *next;
};
void push(struct stack **p,int *i);
void pop(struct stack **p);
void display(struct stack **p);
void main()
{
int ch,item,data;
struct stack *s;
s='\0';
clrscr();
menu:
//clrscr();
printf("\n1.Push\n2.pop\n3.Display\n4.exit");
printf("\nSelect the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nInput the item for push:");
scanf("%d",&item);
push(&s,&item);
goto menu;
case 2:
pop(&s);
getch();
goto menu;
case 3:
display(&s);
getch();
goto menu;
case 4:
exit(0);
default:
printf("\nOut of menu!!");
goto menu;
}
}
void push(struct stack **p,int *i)
{
struct stack *temp,*r;
temp=*p;
r=(struct stack *)malloc(sizeof(struct stack));
r->info=*i;
if(*p=='\0')
{
*p=r;
(*p)->next='\0';
}
else
{
r->next=temp;
*p=r;
}
}
void pop(struct stack **p)
{
struct stack *temp;
if(*p=='\0')
printf("\nStack is empty.");
else
{
printf("\nDeleted data is =%d",(*p)->info);
temp=*p;
*p=(*p)->next;
free(temp);
}
}
void display(struct stack **p)
{
struct stack *temp;
temp=*p;
if(temp=='\0')
printf("\nStack is empty.");
while(temp!='\0')
{
printf("%d\n",temp->info);
temp=temp->next;
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct stack
{
int info;
struct stack *next;
};
void push(struct stack **p,int *i);
void pop(struct stack **p);
void display(struct stack **p);
void main()
{
int ch,item,data;
struct stack *s;
s='\0';
clrscr();
menu:
//clrscr();
printf("\n1.Push\n2.pop\n3.Display\n4.exit");
printf("\nSelect the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nInput the item for push:");
scanf("%d",&item);
push(&s,&item);
goto menu;
case 2:
pop(&s);
getch();
goto menu;
case 3:
display(&s);
getch();
goto menu;
case 4:
exit(0);
default:
printf("\nOut of menu!!");
goto menu;
}
}
void push(struct stack **p,int *i)
{
struct stack *temp,*r;
temp=*p;
r=(struct stack *)malloc(sizeof(struct stack));
r->info=*i;
if(*p=='\0')
{
*p=r;
(*p)->next='\0';
}
else
{
r->next=temp;
*p=r;
}
}
void pop(struct stack **p)
{
struct stack *temp;
if(*p=='\0')
printf("\nStack is empty.");
else
{
printf("\nDeleted data is =%d",(*p)->info);
temp=*p;
*p=(*p)->next;
free(temp);
}
}
void display(struct stack **p)
{
struct stack *temp;
temp=*p;
if(temp=='\0')
printf("\nStack is empty.");
while(temp!='\0')
{
printf("%d\n",temp->info);
temp=temp->next;
}
}
No comments:
Post a Comment