Update DS/C/Lab/Week7/linkedlist.c
This commit is contained in:
parent
dc0e7580d3
commit
b883874dee
@ -6,7 +6,56 @@ typedef struct Node
|
||||
struct Node *next;
|
||||
} 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()
|
||||
{
|
||||
@ -41,7 +90,7 @@ int main()
|
||||
break;
|
||||
case 4:
|
||||
printf("List traversal");
|
||||
dispList();
|
||||
dispList(head);
|
||||
break;
|
||||
case 5:
|
||||
printf("The reversed link list is: ");
|
||||
@ -53,11 +102,12 @@ int main()
|
||||
break;
|
||||
case 7:
|
||||
printf("The list after deleting every alternate node is:");
|
||||
delAltNode();
|
||||
break;
|
||||
case 8:
|
||||
printf("Enter the element you want to insert: ");
|
||||
scanf("%d", &data);
|
||||
enterSortList();
|
||||
enterSortList(data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user