#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void create();
void delete();
void search();
void insert();
void display();
void make();
void addbeg();
void addend();
void addafternode();
main()
{
int i;
while(1)
{
printf("\npress 1 to create\npress 2 to delete\npress 3 to search\npress 4 to insert\npress 5 to display\n");
scanf("%d",&i);
switch(i)
{
case 1:
{
create();
break;
}
case 2:
{
delete();
break;
}
case 3:
{
search();
break;
}
case 4:
{
insert();
break;
}
case 5:
{
display();
break;
}
default :
{
printf("invalid input");
}
}
}
getch();
}
void create()
{
int i,n;
printf("enter the size of list\n");
scanf("%d",&n);
printf("enter the number\n");
for(i=0;i<n;i++)
{
make();
}
}
void make()
{
struct node *new,*temp;
new=(struct node *)malloc(sizeof (struct node));
scanf("%d",&new->data);
new->next=NULL;
if(head==NULL)
{
head=new;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
void display()
{
struct node *temp;
temp=head;
printf("\nnumbers are");
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
void delete()
{
int num;
struct node *temp,*q;
printf("enter the number to delete\n");
scanf("%d",&num);
temp=head;
if(temp->data!=num)
{
while(temp->next!=NULL)
{
if(temp->next->data==num)
{
q=temp->next;
temp->next=temp->next->next;
free(q);
break;
}
temp=temp->next;
}
if(temp->next==NULL)
{
printf("delete not possible\n");
}
}
else
{
head=temp->next;
free(temp);
}
}
void search()
{
int num;
struct node *temp;
printf("enter number to search\n");
scanf("%d",&num);
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
printf("number present\n");
break;
}
temp=temp->next;
}
if(temp==NULL)
{
printf("not present\n");
}
}
void insert()
{
int i;
printf("press 1 to add at begenning\npress 2 to add at end\npress 3 to add after any node\n");
scanf("%d",&i);
switch(i)
{
case 1:
{
addbeg();
break;
}
case 2:
{
addend();
break;
}
case 3:
{
addafternode();
break;
}
default :
{
printf("invalid input");
}
}
}
void addbeg()
{
struct node *new;
new=(struct node *)malloc(sizeof (struct node));
printf("enter number to insert\n");
scanf("%d",&new->data);
new->next=head;
head=new;
}
void addend()
{
struct node *new,*temp;
new=(struct node *)malloc(sizeof (struct node));
printf("enter number to insert\n");
scanf("%d",&new->data);
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
new->next=NULL;
}
void addafternode()
{
int num;
struct node *temp,*new;
new=(struct node *)malloc(sizeof (struct node));
printf("enter the number to insert\n");
scanf("%d",&new->data);
printf("enter the number after which to insert\n");
scanf("%d",&num);
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
break;
}
temp=temp->next;
}
new->next=temp->next;
temp->next=new;
}