SLL_ProgramFunctions
SLL_ProgramFunctions
h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void create() {
int value;
printf("Enter the value for the first node: ");
scanf("%d", &value);
head = newNode;
tail = newNode;
void addAtBeg() {
int value;
printf("Enter the value for the new node: ");
scanf("%d", &value);
head = newNode;
if (tail == NULL) {
tail = newNode;
}
void insertAfterNode() {
int target, value;
printf("Enter the value of the target node: ");
scanf("%d", &target);
printf("Enter the value for the new node: ");
scanf("%d", &value);
if (temp == tail) {
tail = newNode;
}
printf("Node inserted after %d successfully.\n", target);
return;
}
temp = temp->next;
}
void insertAtEnd() {
int value;
printf("Enter the value for the new node: ");
scanf("%d", &value);
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
void deleteAtBeg() {
if (head == NULL) {
printf("List is empty! Nothing to delete.\n");
return;
}
if (head == NULL) {
tail = NULL;
}
void deleteAfterNode() {
int target;
printf("Enter the value of the target node: ");
scanf("%d", &target);
free(nodeToDelete);
printf("Node deleted after %d successfully.\n", target);
return;
}
temp = temp->next;
}
printf("No node found after the target node with value %d.\n", target);
}
void deleteAtEnd() {
if (head == NULL) {
printf("List is empty! Nothing to delete.\n");
return;
}
if (head == tail) {
free(head);
head = tail = NULL;
} else {
struct node *temp = head;
while (temp->next != tail) {
temp = temp->next;
}
free(tail);
tail = temp;
tail->next = NULL;
}
printf("Node deleted from the end successfully.\n");
}
void sortList() {
if (head == NULL || head->next == NULL) {
printf("List is empty or has only one node, no need to sort.\n");
return;
}
void findMax() {
if (head == NULL) {
printf("List is empty! No maximum value.\n");
return;
}
void reverseTheList() {
if (head == NULL) {
printf("List is empty! Nothing to reverse.\n");
return;
}
tail = head;
head = prev;
int main() {
int choice;
while (1) {
printf("\nMenu:\n");
printf("1. Create List\n2. Insert at Beginning\n3. Insert at End\n4. Insert
After Specified Node\n5. Delete at Beginning\n6. Delete at End\n7. Delete After
Specified Node\n8. Display List\n9. Sort the List\n10. Find the Max element\n11.
Reverse the List\n12. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
create();
break;
case 2:
addAtBeg();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAfterNode();
break;
case 5:
deleteAtBeg();
break;
case 6:
deleteAtEnd();
break;
case 7:
deleteAfterNode();
break;
case 8:
display();
break;
case 9:
sortList();
break;
case 10:
findMax();
break;
case 11:
reverseTheList();
break;
case 12:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}