/* Program for stack implementation using linked list */ pop() | display() function
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*top=NULL;
void push()
{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=top;
top=temp;
}
void pop()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstack is empty");
else
{
printf("\n%d is deleted from stack\n",top->info);
top=top->link;
free(temp);
}
}
void display()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstackis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*top=NULL;
void push()
{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=top;
top=temp;
}
void pop()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstack is empty");
else
{
printf("\n%d is deleted from stack\n",top->info);
top=top->link;
free(temp);
}
}
void display()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstackis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
0 comments: