#include <stdio.h>
struct list
{
int data;
struct list *next;
};
typedef struct list node;
int print(node *head)
{
node *start=head;
printf("
Your Linked List:
");
while(start!=NULL)
{
printf("%d ",start->data);
start= start->next;
}
printf("
");
}
int delete_node(node *head,int n)
{
node *start = head;
node *temp;
while(start!=NULL)
{
if(start->next->data == n) ///not the best way to delete
{
start->next = start->next->next;
break;
}
start = start->next;
}
print(head);
}
int main()
{
node *head,*start,*last,*temp;
int flag,n;
start=NULL;
do
{
printf("Please Enter 1 for Insert Value: ");
scanf("%d",&flag);
if(flag!=1)
{
break;
}
temp = (node *)malloc(sizeof(node));
printf("Enter The Value: ");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
{
start=temp;
head=temp;
}
else
{
last=start;
while(last->next!=NULL)
{
last= last->next;
}
last->next =temp;
}
}
while(flag==1);
printf("Please Enter Which Element You Want To Delete: ");
scanf("%d",&n);
delete_node(head,n);
return 0;
}
Output
Please Enter 1 for Insert Value: 1
Enter The Value: 8
Please Enter 1 for Insert Value: 1
Enter The Value: 5
Please Enter 1 for Insert Value: 1
Enter The Value: 9
Please Enter 1 for Insert Value: 1
Enter The Value: 0
Please Enter 1 for Insert Value: 0
Please Enter Which Element You Want To Delete: 9
Your Linked List:
8 5 0
Add your comment