0% found this document useful (0 votes)
6 views

linked list

The document provides a C implementation of a linked list, including functions to create nodes, traverse the list, insert nodes at various positions, delete nodes, and search for values. It also includes a main function that presents a menu for users to perform linked list operations interactively. The program handles user input and manages memory allocation for the linked list nodes.
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)
6 views

linked list

The document provides a C implementation of a linked list, including functions to create nodes, traverse the list, insert nodes at various positions, delete nodes, and search for values. It also includes a main function that presents a menu for users to perform linked list operations interactively. The program handles user input and manages memory allocation for the linked list nodes.
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/ 8

Ques: Implementation of 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:

You might also like