Program
- Create Dynamic Stack(using Linked list)
// Stack using linked list
#include<stdio.h>
#include<conio.h>
struct link
{
int no;
struct link *next;
};
struct link *start,*new1;
struct link * push(struct link *);
struct link * pop(struct link *);
void peep(struct link *);
struct link * update(struct link
*);
void display(struct link *);
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n\t==================");
printf("\n\t\t
Menu");
printf("\n\t==================");
printf("\n
1. Push");
printf("\n
2. Pop");
printf("\n
3. Peep");
printf("\n
4. Update");
printf("\n
5. Display");
printf("\n
6. Exit");
printf("\nEnter
Your Choise : ");
scanf("%d",&choice);
switch(choice)
{
case
1:
{
printf("\n
Push Operation Selected");
start=push(start);
printf("\n
Stack after push");
display(start);
break;
}
case
2:
{
printf("\n
POP Operation Selected");
start=pop(start);
printf("\n
srack after pop");
display(start);
break;
}
case
3:
{
printf("\n
Peep Operation Selected");
peep(start);
break;
}
case
4:
{
printf("\n
Update Operation Selected");
start=update(start);
printf("\n
Stack after update");
display(start);
break;
}
case
5:
{
printf("\n
Display Selected");
display(start);
break;
}
case
6:
{
exit(0);
}
}
}
getch();
}
struct link *push(struct link *s)
{
new1=(struct link *)malloc(sizeof(struct
link));
printf("\n Enter data to push :
");
scanf("%d",&new1->no);
new1->next=s;
s=new1;
return(s);
}
struct link *pop(struct link *s)
{
struct link *temp;
if(s==NULL)
{
printf("\n stack is underflow");
}
else
{
temp=s->next;
printf("\n popped element =
%d",s->no);
free(s);
s=temp;
}
return(s);
}
void display(struct link *s)
{
if(s==NULL)
{
printf("\n stack is
underflow");
return;
}
else
{
while(s)
{
printf("\n
%d",s->no);
s=s->next;
}
}
}
void peep(struct link *s)
{
int
pos,count=1,flag=0;
printf("\n
Enter position to peep data : ");
scanf("%d",&pos);
while(s)
{
if(count== pos)
{
printf("\n peeped element :
%d",s->no);
flag=1;
break;
}
s=s->next;
count++;
}
if(flag==0)
{
printf("\n position not found");
}
}
struct link * update(struct link
*s)
{
int
pos,count=1,flag=0;
struct
link *temp;
temp=s;
printf("\n
Enter position to peep data : ");
scanf("%d",&pos);
while(s)
{
if(count== pos)
{
printf("\n search successful old
value = %d",s->no);
printf("\n Enter new value :
");
scanf("%d",&s->no);
flag=1;
}
s=s->next;
count++;
}
if(flag==0)
{
printf("\n position not found");
}
s=temp;
return(s);
}
Posted By ;Ruchita Pandya
No comments:
Post a Comment