Update DS/C/Lab/Week7/linkedlist.c

This commit is contained in:
Aadit Agrawal 2024-09-10 11:30:56 +05:30
parent dc0e7580d3
commit b883874dee

View File

@ -6,7 +6,56 @@ typedef struct Node
struct Node *next; struct Node *next;
} Node; } Node;
// case 3
Node *delEle(Node *head, int data)
{
if (head == NULL)
{
printf("Linked list is empty.\n");
return NULL;
}
Node *current = head;
Node *prev = NULL;
while (current != NULL && current->data != data)
{
prev = current;
current = current->next;
}
if (current == NULL)
{
printf("Element not found in the linked list.\n");
return head;
}
if (prev == NULL)
{
head = current->next;
}
else
{
prev->next = current->next;
}
free(current);
printf("Element deleted successfully.\n");
return head;
}
// case 4
void dispList(Node *head)
{
if (head == NULL)
{
printf("Linked list is empty.\n");
return;
}
Node *current = head;
printf("Linked list elements: ");
while (current != NULL)
{
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() int main()
{ {
@ -41,7 +90,7 @@ int main()
break; break;
case 4: case 4:
printf("List traversal"); printf("List traversal");
dispList(); dispList(head);
break; break;
case 5: case 5:
printf("The reversed link list is: "); printf("The reversed link list is: ");
@ -53,11 +102,12 @@ int main()
break; break;
case 7: case 7:
printf("The list after deleting every alternate node is:"); printf("The list after deleting every alternate node is:");
delAltNode();
break; break;
case 8: case 8:
printf("Enter the element you want to insert: "); printf("Enter the element you want to insert: ");
scanf("%d", &data); scanf("%d", &data);
enterSortList(); enterSortList(data);
break; break;
} }
} }