Single linked list basic Operation using Typedef
#include <stdio.h>
#include <stdlib.h>
// Define a new type 'Node' for the struct
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *head = NULL;
// Function to create a new node
Node* createNode(int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Insert at the beginning
void insertAtBeginning(int data) {
Node *newNode = createNode(data);
newNode->next = head;
head = newNode;
}
// Insert at the end
void insertAtEnd(int data) {
Node *newNode = createNode(data);
if (head == NULL) {
head = newNode;
return;
}
Node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
// Insert at a specific position
void insertAtPosition(int data, int position) {
Node *newNode = createNode(data);
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
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);
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
// Delete from the beginning
void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node *temp = head;
head = head->next;
free(temp);
}
// Delete from the end
void deleteFromEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
return;
}
Node *temp = head;
while (temp->next->next != NULL)
temp = temp->next;
free(temp->next);
temp->next = NULL;
}
// Delete from a specific position
void deleteFromPosition(int position) {
if (head == NULL) {
printf("List is empty\n");
return;
}
if (position == 1) {
Node *temp = head;
head = head->next;
free(temp);
return;
}
Node *temp = head;
for (int i = 1; i < position - 1 && temp->next != NULL; i++)
temp = temp->next;
if (temp->next == NULL) {
printf("Position out of range\n");
} else {
Node *toDelete = temp->next;
temp->next = toDelete->next;
free(toDelete);
}
}
// Display the linked list
void displayList() {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node *temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Search for an element in the linked list
void searchElement(int data) {
Node *temp = head;
int position = 1;
while (temp != NULL) {
if (temp->data == data) {
printf("Element %d found at position %d\n", data, position);
return;
}
temp = temp->next;
position++;
}
printf("Element %d not found in the list\n", data);
}
int main() {
int choice, data, position;
while (1) {
printf("\n--- Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display List\n");
printf("8. Search Element\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at beginning: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter data to insert at end: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter data and position to insert: ");
scanf("%d %d", &data, &position);
insertAtPosition(data, position);
break;
case 4:
deleteFromBeginning();
break;
case 5:
deleteFromEnd();
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteFromPosition(position);
break;
case 7:
displayList();
break;
case 8:
printf("Enter element to search: ");
scanf("%d", &data);
searchElement(data);
break;
case 9:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}