Below given a code for singly linked list menu driven using c langulage
create()
display()
insert any ,first, last
delete any, first, last
download here code
Download Button
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
int i,val;
struct node *head,*newnode,*ptr,*tmp;
void new_node()
{
newnode=(struct node*)malloc(sizeof(struct node *));
printf("\n Enter value :");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
}
void create(struct node *head)
{
int n;
ptr=head;
printf("\nHow many vlues are you insert");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new_node();
ptr->next=newnode;
ptr=newnode;
}
}
void display(struct node*head)
{
struct node *ptr;
ptr=head->next;
printf("\n------------");
if(ptr==NULL)
{printf("\nThe list is empty");}
else
{
while(ptr!=NULL)
{ printf("\n%d",ptr->data);
ptr=ptr->next;
}
}
printf("\n--------------");
}
void insert_any(struct node *head)
{
int no;
printf("Enter the node number after want to insert the value :");
scanf("%d",&no);
new_node();
ptr=head;
for(i=1;i<no;i++)
{
ptr=ptr->next;
}
newnode->next=ptr->next;
ptr->next=newnode;
}
void insert_first(struct node*head)
{
new_node();
newnode->next=head->next;
head->next=newnode;
}
void insert_last(struct node *head)
{
new_node();
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=newnode;
}
struct node* delete_any(struct node *head)
{
int key;
struct node *ptr, *prev;
printf("\n Enter the item which you want to delete: ");
scanf("%d",&key);
ptr=head->next;
prev=head;
while(ptr!=NULL && ptr->data!=key)
{
prev =ptr;
ptr=ptr->next;
}
prev->next=ptr->next;
return ptr;
}
struct node* delete_last(struct node* head)
{
struct node *ptr,*prev;
ptr=head->next;
prev=head;
while(ptr->next!=NULL)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=NULL;
return ptr;
}
struct node *delete_first(struct node *head)
{
struct node *ptr ,*prev;
prev=head;
ptr=head->next;
if(ptr->next==NULL)
{
printf("\n list is empty");
}
else
{
prev->next=ptr->next;
}
return ptr;
}
void main()
{
int ch1;
head=(struct node *)malloc(sizeof(struct node*));
head->data=NULL;
head->next=NULL;
do
{
printf("\n1. create");
printf("\n2. display");
printf("\n3. insert Any");
printf("\n4. insert first");
printf("\n5. insert last");
printf("\n6. delete any");
printf("\n7. delete first");
printf("\n8. delete last");
printf("\n9. Exit");
printf("\n\n Enter your choice :");
scanf("%d",&ch1);
switch(ch1)
{
case 1:create(head);
break;
case 2:printf("\n The content of LL is :");
display(head) ;
break;
case 3:insert_any(head);
break;
case 4:insert_first(head);
break;
case 5:insert_last(head);
break;
case 6:tmp=delete_any(head);
printf("\n-----------");
if(tmp!=NULL)
{printf("\n The deleted item is:- %d",tmp->data); }
else
{
printf("\n The list is empty or not found");
}
printf("\n------------");
break;
case 7:tmp=delete_first(head);
printf("\n-----------");
if(tmp!=NULL)
{printf("\n The deleted item is:- %d",tmp->data); }
else
{
printf("\n The list is empty or not found");
}
printf("\n------------");
break;
case 8:tmp=delete_last(head);
printf("\n-----------");
if(tmp!=NULL)
{printf("\n The deleted item is:- %d",tmp->data); }
else
{printf("\n The list is empty or not found"); }
printf("\n------------");
break;
case 9:printf("By By.......");
exit(1);
}
}
while(ch1!=9);
getch();
}