
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete all Non-Prime Nodes from a Singly Linked List in C++
In this tutorial, we are going to learn how to delete all prime nodes from a singly linked list.
Let's see the steps to solve the problem.
Write struct with data and next pointer.
Write a function to insert the node into the singly linked list.
Initialize the singly linked list with dummy data.
Iterate over the singly linked list. Find whether the current node data is prime or not.
If the current data is not prime, then delete the node.
Write a function to delete the node. Consider the following three cases while deleting the node.
If the node is head node, then move the head to next node.
If the node is middle node, then link the next node to the previous node
If the node is end node, then remove the previous node link.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node* next; }; void insertNode(Node** head_ref, int new_data) { Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } bool isPrime(int n) { if (n <= 1) { return false; } if (n <= 3) { return true; } if (n % 2 == 0 || n % 3 == 0) { return false; } for (int i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } void deleteNonPrimeNodes(Node** head_ref) { Node* ptr = *head_ref; while (ptr != NULL && !isPrime(ptr->data)) { Node *temp = ptr; ptr = ptr->next; delete(temp); } *head_ref = ptr; if (ptr == NULL) { return; } Node *curr = ptr->next; while (curr != NULL) { if (!isPrime(curr->data)) { ptr->next = curr->next; delete(curr); curr = ptr->next; } else { ptr = curr; curr = curr->next; } } } void printLinkedList(Node* head) { while (head != NULL) { cout << head->data << " -> "; head = head->next; } } int main() { Node* head = NULL; insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); insertNode(&head, 4); insertNode(&head, 5); insertNode(&head, 6); cout << "Linked List before deletion:" << endl; printLinkedList(head); deleteNonPrimeNodes(&head); cout << "\nLinked List after deletion:" << endl; printLinkedList(head); }
Output
If you run the above code, then you will get the following result.
Linked List before deletion: 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> Linked List after deletion: 5 -> 3 -> 2 ->
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Delete all Prime Nodes from a Singly Linked List in C++
- Delete all Prime Nodes from a Doubly Linked List in C++
- Product of all prime nodes in a Singly Linked List in C++
- Delete all the even nodes from a Doubly Linked List in C++
- Product of the nodes of a Singly Linked List
- Delete a tail node from the given singly Linked List using C++
- Program to delete n nodes after m nodes from a linked list in Python
- Reverse Alternate K Nodes in a Singly Linked List in C++
- Delete alternate nodes of a Linked List in C++
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Delete N nodes after M nodes of a linked list in C++?
- Sum of the nodes of a Singly Linked List in C Program
- Find the common nodes in two singly linked list in C++
- Delete all the nodes from a doubly linked list that are smaller than a given value in C++
- Delete all the nodes from the doubly linked list that are greater than a given value in C++
