0% found this document useful (0 votes)
48 views6 pages

Linkedlist

This C program implements a linked list with operations like insertion, deletion, reversal, and printing. It defines a Node struct with data and next pointer fields. Functions are created to insert nodes at the beginning, end, or middle of the list, delete from the beginning, end, or middle, reverse the list, and print the list. The main function uses a menu to allow the user to test each operation on the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views6 pages

Linkedlist

This C program implements a linked list with operations like insertion, deletion, reversal, and printing. It defines a Node struct with data and next pointer fields. Functions are created to insert nodes at the beginning, end, or middle of the list, delete from the beginning, end, or middle, reverse the list, and print the list. The main function uses a menu to allow the user to test each operation on the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME – DAKSH PATEL

REG NO - 23MCA1025

Implimentation of linked list using all operations

#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));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void insertAtBeginning(struct Node** head) {


int data;
printf("Enter value to insert: ");
scanf("%d", &data);
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

void insertAtEnd(struct Node** head) {


int data;
printf("Enter value to insert: ");
scanf("%d", &data);
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}

void insertAtMiddle(struct Node** head) {


int data, position;
printf("Enter value to insert: ");
scanf("%d", &data);
printf("Enter position to insert: ");
scanf("%d", &position);

if (position <= 0) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
return;
}

struct Node* newNode = createNode(data);


struct Node* current = *head;
int count = 0;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Invalid position\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}

void deleteFromBeginning(struct Node** head) {


if (*head == NULL) {
printf("Linked list is empty\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}

void deleteFromEnd(struct Node** head) {


if (*head == NULL) {
printf("Linked list is empty\n");
return;
}
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* current = *head;
while (current->next->next != NULL) {
current = current->next;
}
free(current->next);
current->next = NULL;
}

void deleteFromMiddle(struct Node** head) {


if (*head == NULL) {
printf("Linked list is empty\n");
return;
}
int position;
printf("Enter position to delete: ");
scanf("%d", &position);

if (position <= 0) {
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
return;
}

struct Node* current = *head;


struct Node* prev = NULL;
int count = 0;
while (current != NULL && count < position) {
prev = current;
current = current->next;
count++;
}
if (current == NULL) {
printf("Invalid position\n");
return;
}
prev->next = current->next;
free(current);
}

void reverseLinkedList(struct Node** head) {


struct Node* prev = NULL;
struct Node* current = *head;
struct Node* nextNode;
while (current != NULL) {
nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
}
*head = prev;
}

void printLinkedList(struct Node* head) {


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

int main() {
struct Node* head = NULL;
int choice;

while (1) {
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Insert at middle\n");
printf("4. Delete from beginning\n");
printf("5. Delete from end\n");
printf("6. Delete from middle\n");
printf("7. Reverse linked list\n");
printf("8. Print linked list\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 1) {
insertAtBeginning(&head);
} else if (choice == 2) {
insertAtEnd(&head);
} else if (choice == 3) {
insertAtMiddle(&head);
} else if (choice == 4) {
deleteFromBeginning(&head);
} else if (choice == 5) {
deleteFromEnd(&head);
} else if (choice == 6) {
deleteFromMiddle(&head);
} else if (choice == 7) {
reverseLinkedList(&head);
printf("Linked list reversed\n");
} else if (choice == 8) {
printf("Linked list: ");
printLinkedList(head);
} else if (choice == 9) {
exit(0);
} else {
printf("Invalid choice\n");
}
}

return 0;
}

You might also like