Singly Linklist
Singly Linklist
Singly Linklist
//Roll_No-67
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
// Function prototypes
int main() {
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");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
scanf("%d", &value);
insertAtLast(&head, value);
break;
case 3:
scanf("%d", &value);
scanf("%d", &position);
break;
case 4:
deleteFromBeginning(&head);
break;
case 5:
deleteFromLast(&head);
break;
case 6:
scanf("%d", &position);
deleteAfterLocation(&head, position);
break;
case 7:
scanf("%d", &value);
if (searchElement(head, value))
else
break;
case 8:
displayList(head);
break;
case 9:
printf("Exiting program.\n");
break;
default:
return 0;
newNode->data = value;
newNode->next = *head;
*head = newNode;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
int count = 1;
newNode->data = value;
newNode->next = NULL;
if (position == 1) {
newNode->next = *head;
*head = newNode;
return;
temp = temp->next;
count++;
if (temp == NULL) {
} else {
newNode->next = temp->next;
temp->next = newNode;
if (*head == NULL) {
} else {
*head = (*head)->next;
free(temp);
printf("Node deleted from the beginning.\n");
if (*head == NULL) {
free(*head);
*head = NULL;
} else {
temp = temp->next;
free(temp->next);
temp->next = NULL;
if (*head == NULL) {
int count = 1;
temp = temp->next;
count++;
} else {
temp->next = temp->next->next;
free(nodeToDelete);
if (temp->data == key) {
temp = temp->next;
if (temp == NULL) {
printf("List is empty.\n");
} else {
temp = temp->next;
printf("\n");
Output:-
Insert at Beginning
2. Insert at Last
8. Show
9. Exit
1. Insert at Beginning
2. Insert at Last
8. Show
9. Exit
Insert at Beginning
2. Insert at Last
8. Show
9. Exit
Enter position: 6
1. Insert at Beginning
2. Insert at Last
8. Show
9. Exit
Enter your choice: 3
Enter position: 3
1. Insert at Beginning
2. Insert at Last
8. Show
9. Exit
1. Insert at Beginning
2. Insert at Last
8. Show
9. Exit