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

SLL Algorithm

The document outlines algorithms for creating and manipulating a linked list, including operations such as insertion at the beginning, end, and after a specific node, as well as deletion from various positions. It also describes methods for sorting the list, finding the maximum value, reversing the list, and displaying its contents. Each operation includes step-by-step instructions for implementation.

Uploaded by

shaikrezwana8812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SLL Algorithm

The document outlines algorithms for creating and manipulating a linked list, including operations such as insertion at the beginning, end, and after a specific node, as well as deletion from various positions. It also describes methods for sorting the list, finding the maximum value, reversing the list, and displaying its contents. Each operation includes step-by-step instructions for implementation.

Uploaded by

shaikrezwana8812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Create the List

Algorithm:

1. Prompt the user to enter a value for the first node.


2. Create a new node (newNode) and allocate memory.
3. Assign the input value to newNode->data.
4. Set newNode->next = NULL.
5. Set head = newNode and tail = newNode.
6. Print "List created with first node successfully."

2. Insert at the Beginning


Steps:
1. Input the value for the new node.
2. Create a new node dynamically using malloc.
3. Assign value to the data field of the new node.
4. Set the next pointer of the new node to point to the current head.
5. Update the head to point to the new node.
6. If the list was previously empty (i.e., tail == NULL), update tail to point to the new
node.
7. End.

3. Insert at the End


Steps:
1. Input the value for the new node.
2. Create a new node dynamically using malloc.
3. Assign value to the data field of the new node.
4. Set the next pointer of the new node to NULL (as it will be the last node).
5. If the list is empty (i.e., head == NULL):
 Update both head and tail to point to the new node.
6. Otherwise:
 Update the next pointer of the current tail to point to the new node.
 Update tail to point to the new node.
7. End.
4. Insert After a Specific Node
Steps:
1. Input the value for the new node.
2. Input the position (1-based) or specificNode (reference to the target node) where the
new node should be inserted.
3. Create a new node dynamically using malloc.
4. Assign value to the data field of the new node.
5. Traverse the list to find the target node:
Use a loop to move through the nodes until the target node is reached.
6. If the target node is found:
 Set the next pointer of the new node to point to the node currently after
the target node.
 Update the next pointer of the target node to point to the new node.
 If the target node was the tail, update tail to point to the new node.
7. If the target node is not found, print an error message and free the new node.
8. End.

5. Delete at Beginning
Algorithm:
1. Check if the list is empty:
 If head == NULL, print "List is empty" and terminate the operation.
2. Store the current head node in a temporary pointer (temp).
3. Update head to point to the next node (head->next).
4. Free the memory allocated for the node stored in temp.
5. If the list becomes empty (head == NULL), update tail to NULL.
6. Print "Node deleted from the beginning."

6. Delete at End
Algorithm:
1. Check if the list is empty:
 If head == NULL, print "List is empty" and terminate the operation.
2. If the list has only one node:
 Free the head node.
 Set head and tail to NULL.
 Print "Node deleted from the end."
 Exit.
3. If the list has more than one node:
 Traverse the list to find the second-last node (temp).
 While temp->next != tail, move temp to temp->next.
 Free the memory for the tail node.
 Update tail to point to temp.
 Set tail->next to NULL.
4. Print "Node deleted from the end."

7. Delete After a Specific Node


Algorithm:
1. Check if the list is empty:
 If head == NULL, print "List is empty" and terminate the operation.
2. Prompt the user for the value of the node after which deletion is to occur.
3. Search for the target node (targetNode) in the list:
 Start from head and traverse while targetNode->data != value.
 If the node is not found (targetNode == NULL), print "Node not found" and
exit.
4. Check if targetNode->next exists:
 If targetNode->next == NULL, print "No node exists after the specified node"
and exit.
5. Store the node to be deleted (deleteNode = targetNode->next).
6. Update targetNode->next to skip the node to be deleted (targetNode->next =
deleteNode->next).
7. If deleteNode was the tail, update tail to targetNode.
8. Free the memory allocated for deleteNode.
9. Print "Node deleted after the specified node."
8. Sort the List

Algorithm:

1. Check if the list is empty or has only one node:


 If head == NULL or head->next == NULL, print "List is empty or has only
one node, no need to sort." and terminate.
2. Use Bubble Sort to sort the nodes:
 Initialize two pointers, i and j.
 For each node i in the list:
 For each node j after i:
 If i->data > j->data, swap the data of the two nodes.
3. Print "List sorted successfully."

9. Find the Maximum Value

Algorithm:

1. Check if the list is empty:


 If head == NULL, print "List is empty! No maximum value." and terminate.
2. Initialize a variable max with the value of the first node (head->data).
3. Traverse the list using a pointer (temp):
 For each node, compare temp->data with max.
 If temp->data > max, update max to temp->data.
4. Print "The maximum value in the list is: max."

10. Reverse the List

Algorithm:

1. Check if the list is empty:


 If head == NULL, print "List is empty! Nothing to reverse." and terminate.
2. Initialize three pointers:
 prev = NULL, current = head, and next = NULL.
3. Traverse the list and reverse the links:
 For each node:
 Store the next node in next (next = current->next).
 Reverse the current node’s link (current->next = prev).
 Move prev to current and current to next.
4. Update head and tail:
 Set head = prev and tail = the original head.
5. Print "List reversed successfully."

11. Display the List


Algorithm:
1. Check if the list is empty:
 If head == NULL, print "List is empty!" and terminate.
2. Initialize a pointer (temp = head).
3. Traverse the list:
 For each node:
 Print temp->data followed by ->.
 Move temp to temp->next.
4. Print "NULL" to indicate the end of the list.

You might also like