#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
struct Node {
int data;
struct Node* next;
};
// Head pointer
struct Node* head = NULL;
// Function to create and insert a node at the end
void insertAtEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to insert at beginning
void insertAtBeginning(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}
// Function to insert at a given position
void insertAtPosition(int value, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
struct Node* temp = head;
for (int i = 1; i < position - 1; i++) {
if (temp == NULL) {
printf("Invalid position!\n");
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to delete at beginning
void deleteAtBeginning() {
if (head == NULL) {
printf("List is empty!\n");
return;
}
struct Node* temp = head;
head = head->next;
free(temp);
}
// Function to delete at end
void deleteAtEnd() {
if (head == NULL) {
printf("List is empty!\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;
}
// Function to delete at a given position
void deleteAtPosition(int position) {
if (head == NULL) {
printf("List is empty!\n");
return;
}
if (position == 1) {
struct Node* temp = head;
head = head->next;
free(temp);
return;
}
struct Node* temp = head;
for (int i = 1; i < position - 1; i++) {
if (temp == NULL) {
printf("Invalid position!\n");
return;
}
temp = temp->next;
}
if (temp->next == NULL) {
printf("Invalid position!\n");
return;
}
struct Node* deleteNode = temp->next;
temp->next = deleteNode->next;
free(deleteNode);
}
// Function to display the list
void display() {
if (head == NULL) {
printf("List is empty!\n");
return;
}
struct Node* temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function
int main() {
int choice, value, position;
while (1) {
printf("\n--- Singly Linked List Operations ---\n");
printf("1. Insert at End\n");
printf("2. Insert at Beginning\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 List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(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:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid Choice! Try again.\n");
}
}
return 0;
}
Comments
Post a Comment