Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Deletion of head and tail element logic in a linked list using C language.
In C programming, deleting head and tail elements from a linked list are fundamental operations that require careful pointer manipulation to maintain list integrity. A linked list is a dynamic data structure where each node contains data and a pointer to the next node.
Syntax
// Delete head node void deleteHead(); // Delete tail node void deleteTail();
Node Structure
First, let's define the basic node structure for our linked list −
Example 1: Delete Head Element
Deleting the head element involves updating the head pointer to the next node and freeing the memory −
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* head = NULL;
void insertAtHead(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}
void deleteHead() {
if (head == NULL) {
printf("List is empty<br>");
return;
}
Node* temp = head;
int deletedValue = head->data;
head = head->next;
printf("Deleted head element: %d<br>", deletedValue);
free(temp);
}
void displayList() {
Node* temp = head;
printf("List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("<br>");
}
int main() {
insertAtHead(30);
insertAtHead(20);
insertAtHead(10);
printf("Original ");
displayList();
deleteHead();
printf("After deleting head ");
displayList();
return 0;
}
Original List: 10 20 30 Deleted head element: 10 After deleting head List: 20 30
Example 2: Delete Tail Element
Deleting the tail element requires traversing to the second-last node to update its next pointer −
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* head = NULL;
void insertAtTail(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
void deleteTail() {
if (head == NULL) {
printf("List is empty<br>");
return;
}
// If only one node exists
if (head->next == NULL) {
printf("Deleted tail element: %d<br>", head->data);
free(head);
head = NULL;
return;
}
// Traverse to second last node
Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
printf("Deleted tail element: %d<br>", temp->next->data);
free(temp->next);
temp->next = NULL;
}
void displayList() {
Node* temp = head;
printf("List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("<br>");
}
int main() {
insertAtTail(10);
insertAtTail(20);
insertAtTail(30);
printf("Original ");
displayList();
deleteTail();
printf("After deleting tail ");
displayList();
return 0;
}
Original List: 10 20 30 Deleted tail element: 30 After deleting tail List: 10 20
Key Points
-
Memory Management: Always use
free()to deallocate memory after deletion - Empty List Check: Check if the list is empty before attempting deletion
- Single Node Case: Handle the special case when deleting from a list with only one node
- Pointer Updates: Properly update head/tail pointers to maintain list integrity
Conclusion
Deleting head and tail elements in linked lists requires careful pointer manipulation and memory management. Head deletion is O(1) while tail deletion is O(n) due to traversal requirements in singly linked lists.
