0% found this document useful (0 votes)
18 views2 pages

8

This document contains a C program that implements a singly linked list with functionalities to insert nodes at a specific position, display the list, and free the allocated memory. The program includes a menu-driven interface for user interaction to perform these operations. It handles memory allocation and provides error messages for invalid operations.

Uploaded by

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

8

This document contains a C program that implements a singly linked list with functionalities to insert nodes at a specific position, display the list, and free the allocated memory. The program includes a menu-driven interface for user interaction to perform these operations. It handles memory allocation and provides error messages for invalid operations.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void insertAtPosition(struct Node** head, int data, int position) {


struct Node* newNode = createNode(data);
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
struct Node* current = *head;
for (int i = 0; i < position - 1 && current != NULL; i++) {
current = current->next;
}
if (current == NULL) {
printf("Position %d is out of bounds\n", position);
free(newNode);
return;
}
newNode->next = current->next;
current->next = newNode;
}

void display(struct Node* head) {


if (head == NULL) {
printf("Linked List is empty\n");
return;
}
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

void freeList(struct Node* head) {


while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
}
int main() {
struct Node* head = NULL;
int choice, data, position;

while (1) {
printf("\nMenu:\n");
printf("1. Insert at position\n");
printf("2. Display linked list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position to insert at: ");
scanf("%d", &position);
insertAtPosition(&head, data, position);
break;
case 2:
printf("Linked List:\n");
display(head);
break;
case 3:
freeList(head);
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}

You might also like