The aim was to make a program that deleted the last occurrence of an element in a linked list Ex:-If the linked list is 23->45->23->24->23 then i look for the output 23->45->23->24 but I am getting several errors on debugging mainly on the parts temp->next!=NULL and t1->next=t->next,kindly point out the errors hindering the execution of program?
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insertLL(struct node *head,int data)
{
struct node *temp;
struct node *new=(struct node *)malloc(sizeof(struct node));
new->data=data;
if(head==NULL)
{
head=new;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
void delete(struct node *head,int data)
{
struct node *temp=head;
struct node *t;
struct node *t1;
while(temp->next!=NULL)
{
if(temp->next->data==data)
{
t1=temp;
}
if(temp->data==data)
{
t=temp;
}
temp=temp->next;
}
t1->next=t->next;
free(t);
}
void display(struct node *head)
{
struct node *temp1=head;
if (head==NULL)
{
printf("List is empty");
}
else
{
while(temp1->next!=NULL)
{
printf("%d",temp1->data);
printf("->");
temp1=temp1->next;
}
}
}
int main()
{
int u,k,l,n;
struct node *start;
while(1)
{
printf("Press 1 to insert element into the Linked List, press 2 to delete the last occurence of an element from a linked list, press 3 to display the linked list, press 0 to exit the program\n");
scanf("%d",&u);
switch(u)
{
case 1:
{
printf("Enter the data you want to insert\n");
scanf("%d",&l);
insertLL(start,l);
break;
}
case 2:
{
printf("Enter the element that you want to be deleted\n");
scanf("%d",&k);
delete(start,k);
break;
}
case 3:
{
display(start);
break;
}
default:
{
printf("Invalid input\n");
}
}
printf("Enter 1 to continue or 0 to break\n");
scanf("%d",&n);
if(n==1)
{
continue;
}
if(n==0)
{
break;
}
}
}