//Name: YASH DATTATRAY WAGHMARE
//Roll_No-67
//Pgm- Singly Linked list
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function prototypes
void insertAtBeginning(struct Node** head, int value);
void insertAtLast(struct Node** head, int value);
void insertAtRandomLocation(struct Node** head, int value, int position);
void deleteFromBeginning(struct Node** head);
void deleteFromLast(struct Node** head);
void deleteAfterLocation(struct Node** head, int position);
int searchElement(struct Node* head, int key);
void displayList(struct Node* head);
int main() {
struct Node* head = NULL;
int choice, value, position;
do {
printf("\n1. Insert at Beginning\n2. Insert at Last\n3. Insert at Random Location\n");
printf("4. Delete from Beginning\n5. Delete from Last\n6. Delete Node after specified Location\
n");
printf("7. Search for an Element\n8. Show\n9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter value to insert: ");
scanf("%d", &value);
insertAtLast(&head, value);
break;
case 3:
printf("Enter value to insert: ");
scanf("%d", &value);
printf("Enter position: ");
scanf("%d", &position);
insertAtRandomLocation(&head, value, position);
break;
case 4:
deleteFromBeginning(&head);
break;
case 5:
deleteFromLast(&head);
break;
case 6:
printf("Enter position to delete after: ");
scanf("%d", &position);
deleteAfterLocation(&head, position);
break;
case 7:
printf("Enter element to search: ");
scanf("%d", &value);
if (searchElement(head, value))
printf("Element found in the list.\n");
else
printf("Element not found in the list.\n");
break;
case 8:
displayList(head);
break;
case 9:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
} while (choice != 9);
return 0;
// Function to insert a node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
printf("Node inserted at the beginning.\n");
// Function to insert a node at the end of the linked list
void insertAtLast(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = *head;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
printf("Node inserted at the end.\n");
// Function to insert a node at a specified position in the linked list
void insertAtRandomLocation(struct Node** head, int value, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = *head;
int count = 1;
newNode->data = value;
newNode->next = NULL;
if (position == 1) {
newNode->next = *head;
*head = newNode;
printf("Node inserted at position %d.\n", position);
return;
while (count < position - 1 && temp != NULL) {
temp = temp->next;
count++;
if (temp == NULL) {
printf("Invalid position. Node not inserted.\n");
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Node inserted at position %d.\n", position);
// Function to delete a node from the beginning of the linked list
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty. Nothing to delete.\n");
} else {
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
printf("Node deleted from the beginning.\n");
// Function to delete a node from the end of the linked list
void deleteFromLast(struct Node** head) {
if (*head == NULL) {
printf("List is empty. Nothing to delete.\n");
} else if ((*head)->next == NULL) {
free(*head);
*head = NULL;
printf("Node deleted from the last.\n");
} else {
struct Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
free(temp->next);
temp->next = NULL;
printf("Node deleted from the last.\n");
// Function to delete a node after a specified location in the linked list
void deleteAfterLocation(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty. Nothing to delete.\n");
} else {
struct Node* temp = *head;
int count = 1;
while (count < position && temp != NULL) {
temp = temp->next;
count++;
if (temp == NULL || temp->next == NULL) {
printf("Invalid position. Node not deleted.\n");
} else {
struct Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
free(nodeToDelete);
printf("Node deleted after position %d.\n", position);
// Function to search for an element in the linked list
int searchElement(struct Node* head, int key) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key) {
return 1; // Element found
}
temp = temp->next;
return 0; // Element not found
// Function to display the elements of the linked list
void displayList(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
} else {
printf("Linked list elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
printf("\n");
Output:-
Insert at Beginning
2. Insert at Last
3. Insert at Random Location
4. Delete from Beginning
5. Delete from Last
6. Delete Node after specified Location
7. Search for an Element
8. Show
9. Exit
Enter your choice: 1
Enter value to insert: 10 Node inserted at the beginning.
1. Insert at Beginning
2. Insert at Last
3. Insert at Random Location
4. Delete from Beginning
5. Delete from Last
6. Delete Node after specified Location
7. Search for an Element
8. Show
9. Exit
Enter your choice: 2
Enter value to insert: 44 Node inserted at the end.
Insert at Beginning
2. Insert at Last
3. Insert at Random Location
4. Delete from Beginning
5. Delete from Last
6. Delete Node after specified Location
7. Search for an Element
8. Show
9. Exit
Enter your choice: 3
Enter value to insert: 77
Enter position: 6
Invalid position. Node not inserted.
1. Insert at Beginning
2. Insert at Last
3. Insert at Random Location
4. Delete from Beginning
5. Delete from Last
6. Delete Node after specified Location
7. Search for an Element
8. Show
9. Exit
Enter your choice: 3
Enter value to insert: 77
Enter position: 3
Node inserted at position 3.
1. Insert at Beginning
2. Insert at Last
3. Insert at Random Location
4. Delete from Beginning
5. Delete from Last
6. Delete Node after specified Location
7. Search for an Element
8. Show
9. Exit
Enter your choice: 7
Enter element to search: 10 Element found in the list.
1. Insert at Beginning
2. Insert at Last
3. Insert at Random Location
4. Delete from Beginning
5. Delete from Last
6. Delete Node after specified Location
7. Search for an Element
8. Show
9. Exit
Enter your choice: 8
Linked list elements: 10 44 77