0% found this document useful (0 votes)
5 views4 pages

Dsa LL

This C program implements a singly linked list with various functionalities such as inserting and deleting nodes at different positions, traversing the list, and counting the nodes. It initializes the list with predefined values and provides a menu-driven interface for user interaction. The program manages memory allocation and deallocation to prevent memory leaks.

Uploaded by

yashkataria2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Dsa LL

This C program implements a singly linked list with various functionalities such as inserting and deleting nodes at different positions, traversing the list, and counting the nodes. It initializes the list with predefined values and provides a menu-driven interface for user interaction. The program manages memory allocation and deallocation to prevent memory leaks.

Uploaded by

yashkataria2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node *temp, *newNode;
int choice, value, pos, i;
int values[] = {12, 34, 56, 76, 89, 90};
for (i = 0; i < 6; i++) {
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = values[i];
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
do {
printf("\nMenu:\n");
printf("1. Insert at the end\n");
printf("2. Insert at the beginning\n");
printf("3. Insert at specific position\n");
printf("4. Delete from the end\n");
printf("5. Delete from the beginning\n");
printf("6. Delete from specific position\n");
printf("7. Traverse the list\n");
printf("8. Count the nodes\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at the end: ");
scanf("%d", &value);
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
printf("Node inserted at the end.\n");
break;
case 2:
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Node inserted at the beginning.\n");
break;
case 3:
printf("Enter position to insert at: ");
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (pos == 1) {
newNode->next = head;
head = newNode;
} else {
temp = head;
for (i = 1; i < pos - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position!\n");
free(newNode);
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Node inserted at position %d.\n",
pos);
}
}
break;
case 4:
if (head == NULL) {
printf("List is empty!\n");
} else {
temp = head;
while (temp->next->next != NULL) {
temp = temp->next;}
free(temp->next);
temp->next = NULL;
printf("Node deleted from the end.\n");
}
break;
case 5:
if (head == NULL) {
printf("List is empty!\n");
} else {
temp = head;
head = head->next;
free(temp);
printf("Node deleted from the beginning.\n");
}
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &pos);
if (head == NULL) {
printf("List is empty!\n");
} else if (pos == 1) {
temp = head;
head = head->next;
free(temp);
printf("Node deleted from position 1.\n");
} else {
struct Node* prev = head;
temp = head->next;
for (i = 2; i < pos && temp != NULL; i++) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position!\n");
} else {
prev->next = temp->next;
free(temp);
printf("Node deleted from position %d.\n",
pos);
}
}
break;
case 7:
if (head == NULL) {
printf("List is empty!\n");
} else {
temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
break;
case 8:
temp = head;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}
printf("Total nodes: %d\n", count);
break;
case 9:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 9);
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
return 0;
}

You might also like