linked list
linked list
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node* createNode(int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void traverse(Node *head) {
Node *temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* insertAtBeginning(Node *head, int data) {
Node *newNode = createNode(data);
newNode->next = head;
return newNode;
}
Node* insertAtEnd(Node *head, int data) {
Node *newNode = createNode(data);
if (head == NULL) return newNode;
Node *temp = head;
while (temp->next != NULL) temp = temp->next;
temp->next = newNode;
return head;
}
Node* insertAtMiddle(Node *head, int data, int position) {
if (position == 0) return insertAtBeginning(head, data);
Node *newNode = createNode(data);
Node *temp = head;
int count = 0;
while (temp != NULL && count < position - 1) {
temp = temp->next;
count++;
}
if (temp == NULL) {
printf("\nInvalid position!\n");
free(newNode);
return head;
}
newNode->next = temp->next;
temp->next = newNode;
return head;
}
Node* deleteNode(Node *head, int key) {
if (head == NULL) return NULL;
if (head->data == key) {
Node *temp = head;
head = head->next;
free(temp);
return head;
}
Node *temp = head;
while (temp->next != NULL && temp->next->data != key) temp = temp->next;
if (temp->next != NULL) {
Node *toDelete = temp->next;
temp->next = temp->next->next;
free(toDelete);
}
return head;
}
int search(Node *head, int key) {
Node *temp = head;
while (temp != NULL) {
if (temp->data == key) return 1;
temp = temp->next;
}
return 0;
}
int main() {
Node *head = NULL;
int choice, data, key, position;
do {
printf("\n--- Linked List Operations ---");
printf("\n1. Create/Insert at End");
printf("\n2. Traverse");
printf("\n3. Insertion");
printf("\n4. Delete");
printf("\n5. Search");
printf("\n6. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the data to insert: ");
scanf("%d", &data);
head = insertAtEnd(head, data);
break;
case 2:
traverse(head);
break;
case 3:
do {
printf("\n--- Insertion Sub-Menu ---");
printf("\n1. Insert at Beginning");
printf("\n2. Insert at End");
printf("\n3. Insert at Middle");
printf("\n4. Go Back");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the data to insert at the beginning: ");
scanf("%d", &data);
head = insertAtBeginning(head, data);
break;
case 2:
printf("\nEnter the data to insert at the end: ");
scanf("%d", &data);
head = insertAtEnd(head, data);
break;
case 3:
printf("\nEnter the data to insert: ");
scanf("%d", &data);
printf("\nEnter the position to insert (0-based index): ");
scanf("%d", &position);
head = insertAtMiddle(head, data, position);
break;
case 4:
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
} while (choice != 4);
break;
case 4:
printf("\nEnter the data to delete: ");
scanf("%d", &key);
head = deleteNode(head, key);
break;
case 5:
printf("\nEnter the data to search: ");
scanf("%d", &key);
if (search(head, key))
printf("\n%d found in the list.\n", key);
else
printf("\n%d not found in the list.\n", key);
break;
case 6:
printf("\nExiting... Thank you for using the Linked List program!\n");
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
} while (choice != 6);
return 0;
}
OUTPUT: