Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views2 pages

C Algorithm For Deletion in Specified Location

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

C Algorithm For Deletion in Specified Location

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

#include <stdio.

h>
#include <stdlib.h>

// Define the structure of a node


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

// Function to delete a node at a specific position in the linked list


void deleteAtPosition(struct Node** head, int position) {
// If the linked list is empty
if (*head == NULL) {
printf("The linked list is empty. No node to delete.\n");
return;
}

// Store the head node


struct Node* temp = *head;

// If the head needs to be removed (position 0)


if (position == 0) {
*head = temp->next; // Change the head to the next node
free(temp); // Free the old head node
printf("Node deleted at position %d.\n", position);
return;
}

// Traverse to the node just before the node to be deleted


for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}

// If the position is greater than the number of nodes


if (temp == NULL || temp->next == NULL) {
printf("Position out of range. No node to delete.\n");
return;
}

// Node temp->next is the node to be deleted


struct Node* nodeToDelete = temp->next;

// Unlink the node from the list


temp->next = nodeToDelete->next;

// Free memory of the node to be deleted


free(nodeToDelete);

printf("Node deleted at position %d.\n", position);


}

// Function to print the linked list


void printList(struct Node* head) {
if (head == NULL) {
printf("The linked list is empty.\n");
return;
}
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to add a node at the end of the list


void append(struct Node** head, int newData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

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

// If the linked list is empty, make the new node the head
if (*head == NULL) {
*head = newNode;
return;
}

// Traverse to the last node


while (last->next != NULL) {
last = last->next;
}

// Change the next of last node


last->next = newNode;
}

int main() {
struct Node* head = NULL;

// Adding elements to the linked list


append(&head, 10);
append(&head, 20);
append(&head, 30);
append(&head, 40);
append(&head, 50);

printf("Linked list before deletion: ");


printList(head);

// Deleting the node at position 2 (0-based index)


deleteAtPosition(&head, 2);

printf("Linked list after deletion: ");


printList(head);

return 0;
}

You might also like