Linked Stack to implement Push, Pop, Peep & Update operations.
CODING:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int no;
struct stack *next;
};
struct stack *top=NULL;
struct stack *push(struct stack *,int);
struct stack *pop(struct stack *);
struct stack *update(struct stack *);
struct stack *display(struct stack *);
int peep(struct stack *);
void main()
{
int val,ch;
clrscr();
do
{
printf("\n\nS E L E C T O P E R A T I O N");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEEP");
printf("\n4.UPDATE");
printf("\n5.DISPLAY");
printf("\n6.EXIT");
printf("\nEnter The option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the number to be pushed on to the stack:");
scanf("%d",&val);
top=push(top,val);
break;
case 2:
top=pop(top);
break;
case 3:
val=peep(top);
printf("\nThe value stored at the top of the stack:%d",val);
break;
case 4:
top=update(top);
break;
case 5:
top=display(top);
break;
case 6:
printf("PRESS ENTER..");
break;
default:
printf("INVALID ENTER,TRY AGAIN.");
}
}while(ch!=6);
getch();
}
struct stack *push(struct stack *top,int val)
{
struct stack *new_stack;
new_stack=(struct stack *)malloc(sizeof(struct stack *));
new_stack->no=val;
if(top==NULL)
{
top=new_stack;
top->next=NULL;
}
else
{
new_stack->next=top;
top=new_stack;
}
return top;
}
struct stack *pop(struct stack *top)
{
struct stack *new_stack;
new_stack=top;
if(top==NULL)
printf("STACK UNDERFLOW");
else
{
top=top->next;
printf("\nThe value being deleted is:%d",new_stack->no);
free(new_stack);
}
return top;
}
int peep(struct stack *top)
{
if(top->no==0)
{
printf("Stack is Empty.");
return 0;
}
else
return top->no;
}
struct stack *display(struct stack *top)
{
struct stack *new_stack;
new_stack=top;
if(top==NULL)
printf("STACK EMPTY");
else
{
printf("STACK");
while(new_stack!=NULL)
{
printf("\n%d",new_stack->no);
new_stack=new_stack->next;
}
}
return top;
}
struct stack *update(struct stack *top)
{
int temp,flag=0;
struct stack *up;
up=top;
if(top==NULL)
{
printf("STACK IS EMPTY");
flag=1;
}
else
{
printf("Enter the number you want to update:");
scanf("%d",&temp);
do
{
if(up->no==temp)
{
temp=up->no;
printf("\nEnter the number:");
scanf("%d",&up->no);
printf("%d is update.",temp);
flag=1;
}
else
up=up->next;
}
while(up!=NULL);
}
if(flag==0)
printf("%d is not in Stack",temp);
return top;
}
CODING:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int no;
struct stack *next;
};
struct stack *top=NULL;
struct stack *push(struct stack *,int);
struct stack *pop(struct stack *);
struct stack *update(struct stack *);
struct stack *display(struct stack *);
int peep(struct stack *);
void main()
{
int val,ch;
clrscr();
do
{
printf("\n\nS E L E C T O P E R A T I O N");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEEP");
printf("\n4.UPDATE");
printf("\n5.DISPLAY");
printf("\n6.EXIT");
printf("\nEnter The option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the number to be pushed on to the stack:");
scanf("%d",&val);
top=push(top,val);
break;
case 2:
top=pop(top);
break;
case 3:
val=peep(top);
printf("\nThe value stored at the top of the stack:%d",val);
break;
case 4:
top=update(top);
break;
case 5:
top=display(top);
break;
case 6:
printf("PRESS ENTER..");
break;
default:
printf("INVALID ENTER,TRY AGAIN.");
}
}while(ch!=6);
getch();
}
struct stack *push(struct stack *top,int val)
{
struct stack *new_stack;
new_stack=(struct stack *)malloc(sizeof(struct stack *));
new_stack->no=val;
if(top==NULL)
{
top=new_stack;
top->next=NULL;
}
else
{
new_stack->next=top;
top=new_stack;
}
return top;
}
struct stack *pop(struct stack *top)
{
struct stack *new_stack;
new_stack=top;
if(top==NULL)
printf("STACK UNDERFLOW");
else
{
top=top->next;
printf("\nThe value being deleted is:%d",new_stack->no);
free(new_stack);
}
return top;
}
int peep(struct stack *top)
{
if(top->no==0)
{
printf("Stack is Empty.");
return 0;
}
else
return top->no;
}
struct stack *display(struct stack *top)
{
struct stack *new_stack;
new_stack=top;
if(top==NULL)
printf("STACK EMPTY");
else
{
printf("STACK");
while(new_stack!=NULL)
{
printf("\n%d",new_stack->no);
new_stack=new_stack->next;
}
}
return top;
}
struct stack *update(struct stack *top)
{
int temp,flag=0;
struct stack *up;
up=top;
if(top==NULL)
{
printf("STACK IS EMPTY");
flag=1;
}
else
{
printf("Enter the number you want to update:");
scanf("%d",&temp);
do
{
if(up->no==temp)
{
temp=up->no;
printf("\nEnter the number:");
scanf("%d",&up->no);
printf("%d is update.",temp);
flag=1;
}
else
up=up->next;
}
while(up!=NULL);
}
if(flag==0)
printf("%d is not in Stack",temp);
return top;
}
Comments
Post a Comment