Algorithm TraverseLinkedList
Input: head (reference to the head node of the linked list)
Output: None (processes or prints the data of each node)
1. Start with the head node.
2. If head is NULL:
Exit the algorithm. (The list is empty.)
3. Set current_node ← head
4. While current_node is not NULL:
a. Access or process data of current_node
b. Move to the next node: current_node ← current_node.next
5. End While
6. End Algorithm
Algorithm InsertNode
Input: head (reference to the head node of the linked list), data (value to insert), position
(index to insert at)
Output: Updated linked list
1. Create a new node:
new_node.data ← data
new_node.next ← NULL
2. If position = 0 (Insert at the beginning):
new_node.next ← head
head ← new_node
Exit
3. Set current_node ← head
4. Traverse to the node before the desired position:
For i from 0 to position - 1:
If current_node = NULL:
Exit (Position is invalid)
current_node ← current_node.next
5. Insert the new node:
new_node.next ← current_node.next
current_node.next ← new_node
6. End Algorithm
Algorithm DeleteNode
Input: head (reference to the head node of the linked list), position (index to delete)
Output: Updated linked list
1. If head = NULL:
Exit (The list is empty)
2. If position = 0 (Delete the first node):
temp ← head
head ← head.next
Free temp
Exit
3. Set current_node ← head
4. Traverse to the node before the desired position:
For i from 0 to position - 1:
If current_node = NULL or current_node.next = NULL:
Exit (Position is invalid)
current_node ← current_node.next
5. Delete the node:
temp ← current_node.next
current_node.next ← temp.next
Free temp
6. End Algorithm
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a new node at a specific position
void insertNode(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 0) { // Insert at the beginning
newNode->next = *head;
*head = newNode;
return;
}
struct Node* current = *head;
for (int i = 0; i < position - 1 && current != NULL; i++) {
current = current->next;
}
if (current == NULL) {
printf("Invalid position\n");
free(newNode);
return;
}
newNode->next = current->next;
current->next = newNode;
}
// Function to delete a node at a specific position
void deleteNode(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp;
if (position == 0) { // Delete the first node
temp = *head;
*head = (*head)->next;
free(temp);
return;
}
struct Node* current = *head;
for (int i = 0; i < position - 1 && current != NULL; i++) {
current = current->next;
}
if (current == NULL || current->next == NULL) {
printf("Invalid position\n");
return;
}
temp = current->next;
current->next = temp->next;
free(temp);
}
// Function to traverse and print the linked list
void traverseLinkedList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
// Insert nodes
insertNode(&head, 1, 0);
insertNode(&head, 2, 1);
insertNode(&head, 3, 2);
insertNode(&head, 4, 1); // Insert 4 at position 1
printf("Linked List after insertion: ");
traverseLinkedList(head);
// Delete nodes
deleteNode(&head, 0); // Delete the first node
deleteNode(&head, 2); // Delete the node at position 2
printf("Linked List after deletion: ");
traverseLinkedList(head);
// Free remaining nodes
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}