#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node{
int info;
struct node *link;
}*start;
void addfirst()
{
struct node *ptr;
int n;
ptr=start;
struct node *nw;
nw=(node *)malloc(sizeof(node));
printf("Enter item to insert");
scanf("%d",&nw->info);
if(nw==NULL)
{
printf("Overflow");
exit(0);
}
if(start==NULL)
{
nw->link=NULL;
start=nw;
}
else
{
nw->link=start;
start=nw;
}
}
void addlast()
{
int n;
struct node *nw,*ptr;
nw=(node *)malloc(sizeof(node));
if(nw==NULL)
{
printf("Overflow");
exit(0);
}
printf("Enter data to enter");
scanf("%d",&nw->info);
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
nw->link=NULL;
ptr->link=nw;
}
void addafter()
{
int n,pos;
struct node *nw,*ptr;
nw=(node *)malloc(sizeof(node));
if(nw==NULL)
{
printf("overflow");
exit(0);
}
printf("Enter data to insert");
scanf("%d",&nw->info);
printf("\n\nEnter node no afterwhich you want to insert node");
scanf("%d",&pos);
ptr=start;
for(n=1;n<pos;n++)
ptr=ptr->link;
nw->link=ptr->link;
ptr->link=nw;
}
void deletenode()
{
int n;
printf("Enter position of the node you want to delete\n\n");
scanf("%d",&n);
struct node *ptr;
ptr=start;
for(int i=1;i<n-1;i++)
ptr=ptr->link;
if(i==1)
{
start=start->link;
}
else
{
ptr->link=ptr->link->link;
}
}
void display()
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf(" ->%d",ptr->info);
ptr=ptr->link;
}
}
void main()
{
int n;
start=0;
do
{
printf("\n\n1.ADDFIRST\n\n2.ADDAFTER\n\n3.ADDLAST\n\n4.DISPLAY\n\n5.DELETE\n\n6.EXIT\n\n");
scanf("%d",&n);
if(n==1)
{
addfirst();
}
if(n==2)
{
addafter();
}
if(n==3)
{
addlast();
}
if(n==4)
{
display();
}
if(n==5)
{
deletenode();
}
}while(n!=6);
}
output
1. ADDFIRST
2.ADDAFTER
2.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
1
Enter item to insert 5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
3
Enter data to enter
5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
4
->3->5
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node{
int info;
struct node *link;
}*start;
void addfirst()
{
struct node *ptr;
int n;
ptr=start;
struct node *nw;
nw=(node *)malloc(sizeof(node));
printf("Enter item to insert");
scanf("%d",&nw->info);
if(nw==NULL)
{
printf("Overflow");
exit(0);
}
if(start==NULL)
{
nw->link=NULL;
start=nw;
}
else
{
nw->link=start;
start=nw;
}
}
void addlast()
{
int n;
struct node *nw,*ptr;
nw=(node *)malloc(sizeof(node));
if(nw==NULL)
{
printf("Overflow");
exit(0);
}
printf("Enter data to enter");
scanf("%d",&nw->info);
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
nw->link=NULL;
ptr->link=nw;
}
void addafter()
{
int n,pos;
struct node *nw,*ptr;
nw=(node *)malloc(sizeof(node));
if(nw==NULL)
{
printf("overflow");
exit(0);
}
printf("Enter data to insert");
scanf("%d",&nw->info);
printf("\n\nEnter node no afterwhich you want to insert node");
scanf("%d",&pos);
ptr=start;
for(n=1;n<pos;n++)
ptr=ptr->link;
nw->link=ptr->link;
ptr->link=nw;
}
void deletenode()
{
int n;
printf("Enter position of the node you want to delete\n\n");
scanf("%d",&n);
struct node *ptr;
ptr=start;
for(int i=1;i<n-1;i++)
ptr=ptr->link;
if(i==1)
{
start=start->link;
}
else
{
ptr->link=ptr->link->link;
}
}
void display()
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf(" ->%d",ptr->info);
ptr=ptr->link;
}
}
void main()
{
int n;
start=0;
do
{
printf("\n\n1.ADDFIRST\n\n2.ADDAFTER\n\n3.ADDLAST\n\n4.DISPLAY\n\n5.DELETE\n\n6.EXIT\n\n");
scanf("%d",&n);
if(n==1)
{
addfirst();
}
if(n==2)
{
addafter();
}
if(n==3)
{
addlast();
}
if(n==4)
{
display();
}
if(n==5)
{
deletenode();
}
}while(n!=6);
}
output
1. ADDFIRST
2.ADDAFTER
2.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
1
Enter item to insert 5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
3
Enter data to enter
5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
4
->3->5
No comments:
Post a Comment