0% found this document useful (0 votes)
3 views

SLL_ProgramFunctions

The document contains a C program that implements a singly linked list with various functionalities such as creating a list, adding and deleting nodes at different positions, sorting the list, finding the maximum value, and reversing the list. The program provides a menu-driven interface for users to interact with the linked list operations. It utilizes dynamic memory allocation for node creation and includes error handling for operations on an empty list.

Uploaded by

shaikrezwana8812
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SLL_ProgramFunctions

The document contains a C program that implements a singly linked list with various functionalities such as creating a list, adding and deleting nodes at different positions, sorting the list, finding the maximum value, and reversing the list. The program provides a menu-driven interface for users to interact with the linked list operations. It utilizes dynamic memory allocation for node creation and includes error handling for operations on an empty list.

Uploaded by

shaikrezwana8812
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <stdio.

h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
};

struct node *head = NULL;


struct node *tail = NULL;

void create() {
int value;
printf("Enter the value for the first node: ");
scanf("%d", &value);

struct node *newNode = (struct node *)malloc(sizeof(struct node));


newNode->data = value;
newNode->next = NULL;

head = newNode;
tail = newNode;

printf("List created with first node successfully.\n");


}

void addAtBeg() {
int value;
printf("Enter the value for the new node: ");
scanf("%d", &value);

struct node *newNode = (struct node *)malloc(sizeof(struct node));


newNode->data = value;
newNode->next = head;

head = newNode;

if (tail == NULL) {
tail = newNode;
}

printf("Node added at the beginning successfully.\n");


}

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);

struct node *temp = head;

while (temp != NULL) {


if (temp->data == target) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = temp->next;
temp->next = newNode;

if (temp == tail) {
tail = newNode;
}
printf("Node inserted after %d successfully.\n", target);
return;
}
temp = temp->next;
}

printf("Target node with value %d not found.\n", target);


}

void insertAtEnd() {
int value;
printf("Enter the value for the new node: ");
scanf("%d", &value);

struct node *newNode = (struct node *)malloc(sizeof(struct node));


newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}

printf("Node inserted at the end successfully.\n");


}

void deleteAtBeg() {
if (head == NULL) {
printf("List is empty! Nothing to delete.\n");
return;
}

struct node *temp = head;


head = head->next;
free(temp);

if (head == NULL) {
tail = NULL;
}

printf("Node deleted from the beginning successfully.\n");


}

void deleteAfterNode() {
int target;
printf("Enter the value of the target node: ");
scanf("%d", &target);

struct node *temp = head;

while (temp != NULL && temp->next != NULL) {


if (temp->data == target) {
struct node *nodeToDelete = temp->next;
temp->next = nodeToDelete->next;

// Update tail if the deleted node was the last node


if (nodeToDelete == tail) {
tail = temp;
}

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;
}

struct node *i, *j;


int temp;

for (i = head; i != NULL; i = i->next) {


for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}

printf("List sorted successfully.\n");


}

void findMax() {
if (head == NULL) {
printf("List is empty! No maximum value.\n");
return;
}

struct node *temp = head;


int max = temp->data;

while (temp != NULL) {


if (temp->data > max) {
max = temp->data;
}
temp = temp->next;
}

printf("The maximum value in the list is: %d\n", max);


}

void reverseTheList() {
if (head == NULL) {
printf("List is empty! Nothing to reverse.\n");
return;
}

struct node *prev = NULL, *current = head, *next = NULL;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}

tail = head;
head = prev;

printf("List reversed successfully.\n");


}
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");
}

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;
}

You might also like