#include <stdio.
h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtBeginning(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}
void insertAtEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
void insertAtPosition(int value, int position) {
if (position < 1) {
printf("Invalid position. Position should be 1 or greater.\n");
return;
}
if (position == 1) {
insertAtBeginning(value);
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
struct Node* temp = head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range.\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
void deleteAtBeginning() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
struct Node* temp = head;
head = head->next;
free(temp);
}
void deleteAtEnd() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
return;
}
struct Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
void deleteAtPosition(int position) {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
if (position < 1) {
printf("Invalid position. Position should be 1 or greater.\n");
return;
}
if (position == 1) {
deleteAtBeginning();
return;
}
struct Node* temp = head;
struct Node* prev = NULL;
for (int i = 1; i < position && temp != NULL; i++) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range.\n");
return;
}
prev->next = temp->next;
free(temp);
}
void display() {
struct Node* temp = head;
if (temp == NULL) {
printf("The list is empty.\n");
return;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int choice, value, position;
while (1) {
printf("\n1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Insert at position\n");
printf("4. Delete at beginning\n");
printf("5. Delete at end\n");
printf("6. Delete at position\n");
printf("7. Display\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value to insert: ");
scanf("%d", &value);
printf("Enter position: ");
scanf("%d", &position);
insertAtPosition(value, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteAtPosition(position);
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}