Program to Print Alternate Nodes of Linked List

  • Write a C program to print alternate nodes of given linked list.
  • Function to print alternate nodes of a linked list.

Given a singly linked list, we have to print all alternate nodes of linked list starting from head node. Check the sample example given below for clarification

Input Linked List
1-->2-->3-->4-->5-->6-->7
Alternate Nodes 
1 3 5 7

Algorithm to print alternate nodes of linked list
  • Initialize an integer variable "counter" with 0. We will use this variable to track the sequence number of any node from head node. Head node's sequence number is 0.
  • Using a while loop, we will traverse linked list from head node till (head != NULL) and increment "counter" variable.
  • For every node we will check if value of counter is even(counter%2 == 0) then print current node otherwise continue.

In this program, we will write a user defined function "printAlternateNodes" which implements above mentioned algorithm. printAlternateNodes function takes a pointer to the head node of a linked list and print alternate nodes on screen.

void printAlternateNodes(struct node *head) {
    int counter = 0;
    printf("\nPrinting Alernate nodes of Linked List\n");
    while(head != NULL) {
        if (counter%2 == 0) {
           printf(" %d ", head->data);
        }
        counter++;
        head = head->next;
    }
}

C program to print alternate nodes of a singly linked list

#include <stdio.h>
#include <stdlib.h>
 
/* A structure of linked list node */
struct node {
  int data;
  struct node *next;
} *head;

void initialize(){
    head = NULL;
}

/* 
Given a Inserts a node in front of a singly linked list. 
*/
void insert(int num) {
    /* Create a new Linked List node */
    struct node* newNode = (struct node*) malloc(sizeof(struct node));
    newNode->data  = num;
    /* Next pointer of new node will point to head node of linked list  */
    newNode->next = head;
    /* make new node as new head of linked list */
    head = newNode;
    printf("Inserted Element : %d\n", num);
}

/* It Prints alternate nodes of a Linked List */
void printAlternateNodes(struct node *head) {
    int counter = 0;
    printf("\nPrinting Alernate nodes of Linked List\n");
    while(head != NULL) {
        if (counter%2 == 0) {
           printf(" %d ", head->data);
        }
        counter++;
        head = head->next;
    }
}

/*
Prints a linked list from head node till tail node 
*/
void printLinkedList(struct node *nodePtr) {
  while (nodePtr != NULL) {
     printf("%d", nodePtr->data);
     nodePtr = nodePtr->next;
     if(nodePtr != NULL)
         printf("-->");
  }
}
 
int main() {
    initialize();
    /* Creating a linked List*/
    insert(7);  
    insert(6); 
    insert(5); 
    insert(4);
    insert(3);
    insert(2);
    insert(1);
    
    printf("\nLinked List\n");
    printLinkedList(head);

    printAlternateNodes(head);
    
    return 0;
}
Output
Inserted Element : 7
Inserted Element : 6
Inserted Element : 5
Inserted Element : 4
Inserted Element : 3
Inserted Element : 2
Inserted Element : 1

Linked List
1-->2-->3-->4-->5-->6-->7
Printing Alernate nodes of Linked List
 1  3  5  7