C++ program to delete alternate nodes of a Linked List

C++ program to delete alternate nodes of a linked list

How to delete alternate nodes of a Singly Linked List in C++?

Deleting alternate nodes of the linked list means to keep one node and delete another for example if the data in the list is 14,7,6,20 and 56 it should return 14,6 and 56 after deleting alternate node 7 and 20. In this article, we will learn how to code a C++ program to delete alternate nodes of a linked list.

 

Steps to write a C++ program to delete alternate nodes of a linked list

If we want to delete alternate nodes of a linked list, following steps are followed.

  1. Create linked list using the set of code defined below.
  2. Initialize the variables.
  3. Create a function named make() to create all the nodes of linked list.
  4. Now we need a display function that will be help us to show list before and after deletion.
  5. Create that display function.
  6. Create a function to delete alternate nodes named alternateDel().
  7. Take two pointers prev and alt_node
  8. Initially prev will points to head and alt_node will points to second node .
  9. Make the link of prev to point to link of alt_node and free alt_node.
  10. Traverse prev to next node.
  11. Traverse alt_node to next node of prev.
  12. Repeat step 9,10, and 11 until prev and alt_node becomes null.
  13. Print list after deleting alternate nodes.

Syntax

class Node  

    int data; 
    Node *next; 
}; 
C++ program to delete Alternate nodes in Linked List(1)

Algorithm to delete alternate nodes of a linked list

Following algorithm is used to delete alternate nodes of a linked list.

  1. IF STNODE==NULL
  2. EXIT
  3. WHILE (PREV!=NULL&&ALT_NODE!=NULL)
  4. PREV->NEXTPTR=ALT_NODE->NEXTPTR
  5. FREE(ALT_NODE)
  6. PREV=PREV->NEXTPTR
  7. IF(PREV!=nULL)
  8. ALT_NODE=PREV->NEXTPTR
  9. PRINT

Program to delete alternate nodes of a linked list in C++

Run
#include<iostream>
using namespace std;

struct node
{
  int num;
  node *nextptr;
} *stnode;			//node declared

void make (int n);
void alternateDel (node * stnode);
void display ();


int main ()				//main method
{
  int n, num;

  cout << "Enter the number of nodes: ";
  cin >> n;
  make (n);
  cout << "\nLinked list data: \n";
  display ();
  cout << "\nAfter deleting alternate node:\n";
  alternateDel (stnode);
  display ();
  return 0;
}

void make (int n)			//function to create linked list.
{
  struct node *frntNode, *tmp;
  int num, i;

  stnode = (struct node *) malloc (sizeof (struct node));
  if (stnode == NULL)
    {
      cout << " Memory can not be allocated";
    }
  else
    {

      cout << "Enter the data for node 1: ";
      cin >> num;
      stnode->num = num;
      stnode->nextptr = NULL;	//Links the address field to NULL
      tmp = stnode;

      for (i = 2; i <= n; i++)
	{
	  frntNode = (struct node *) malloc (sizeof (struct node));


	  if (frntNode == NULL)	//If frntnode is null no memory cannot be alloted
	    {
	      cout << "Memory can not be allocated";
	      break;
	    }
	  else
	    {
	      cout << "Enter the data for node " << i << ": ";	// Entering data in nodes.
	      cin >> num;
	      frntNode->num = num;
	      frntNode->nextptr = NULL;
	      tmp->nextptr = frntNode;
	      tmp = tmp->nextptr;
	    }
	}
    }
}

void display ()			//function to display linked list
{
  struct node *tmp;
  if (stnode == NULL)
    {
      cout << "List is empty";
    }
  else
    {
      tmp = stnode;
      cout << "Linked List\t";
      while (tmp != NULL)
	{
	  cout << tmp->num << "\t";
	  tmp = tmp->nextptr;
	}
    }
}

void alternateDel (node * stnode)	//function to delete alternate nodes.
{
  if (stnode == NULL)
    return;


  node *prev = stnode;
  node *alt_node = stnode->nextptr;

  while (prev != NULL && alt_node != NULL)
    {

      prev->nextptr = alt_node->nextptr;

      free (alt_node);

      prev = prev->nextptr;
      if (prev != NULL)
	alt_node = prev->nextptr;
    }
}
Output: 
Enter the number of nodes: 5
Enter the data for node 1: 14
Enter the data for node 2: 7
Enter the data for node 3: 6
Enter the data for node 4: 20
Enter the data for node 5: 56

Linked list data: 
Linked List	14	7	6	20	56	
After deleting alternate node:
Linked List	14	6	56

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java