We have discussed Circular Linked List Introduction and Applications, in the previous post on Circular Linked List. In this post, traversal operation is discussed.

In a conventional linked list, we traverse the list from the head node and stop the traversal when we reach NULL. In a circular linked list, we stop traversal when we reach the first node again. Following is C code for linked list traversal.
C
void printList( struct Node *first)
{
struct Node *temp = first;
if (first != NULL)
{
do
{
printf ( "%d " , temp->data);
temp = temp->next;
}
while (temp != first);
}
}
|
Java
static void printList(Node head)
{
Node temp = head;
if (head != null )
{
do
{
System.out.print(temp.data + " " );
temp = temp.next;
} while (temp != head);
}
}
|
C#
static void printList(Node head)
{
Node temp = head;
if (head != null ) {
do {
Console.Write(temp.data + " " );
temp = temp.next;
} while (temp != head);
}
}
|
Complete program to demonstrate traversal. Following are complete programs to demonstrate traversal of circular linked list.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node *next;
};
void push(Node **head_ref, int data)
{
Node *ptr1 = new Node();
Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL)
{
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void printList(Node *head)
{
Node *temp = head;
if (head != NULL)
{
do
{
cout << temp->data << " " ;
temp = temp->next;
}
while (temp != head);
}
}
int main()
{
Node *head = NULL;
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);
cout << "Contents of Circular Linked List\n " ;
printList(head);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void push( struct Node **head_ref, int data)
{
struct Node *ptr1 = ( struct Node *) malloc ( sizeof ( struct Node));
struct Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL)
{
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void printList( struct Node *head)
{
struct Node *temp = head;
if (head != NULL)
{
do
{
printf ( "%d " , temp->data);
temp = temp->next;
}
while (temp != head);
}
}
int main()
{
struct Node *head = NULL;
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);
printf ( "Contents of Circular Linked List\n " );
printList(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref,
int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null )
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static void printList(Node head)
{
Node temp = head;
if (head != null )
{
do
{
System.out.print(temp.data + " " );
temp = temp.next;
}
while (temp != head);
}
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 12 );
head = push(head, 56 );
head = push(head, 2 );
head = push(head, 11 );
System.out.println( "Contents of Circular " +
"Linked List:" );
printList(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
class CircularLinkedList:
def __init__( self ):
self .head = None
def push( self , data):
ptr1 = Node(data)
temp = self .head
ptr1. next = self .head
if self .head is not None :
while (temp. next ! = self .head):
temp = temp. next
temp. next = ptr1
else :
ptr1. next = ptr1
self .head = ptr1
def printList( self ):
temp = self .head
if self .head is not None :
while ( True ):
print "%d" % (temp.data),
temp = temp. next
if (temp = = self .head):
break
cllist = CircularLinkedList()
cllist.push( 12 )
cllist.push( 56 )
cllist.push( 2 )
cllist.push( 11 )
print "Contents of circular Linked List"
cllist.printList()
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null )
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static void printList(Node head)
{
Node temp = head;
if (head != null )
{
do
{
Console.Write(temp.data + " " );
temp = temp.next;
}
while (temp != head);
}
}
static public void Main(String []args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 56);
head = push(head, 2);
head = push(head, 11);
Console.WriteLine( "Contents of Circular " +
"Linked List:" );
printList(head);
}
}
|
Output:
Contents of Circular Linked List
11 2 56 12
You may like to see following posts on Circular Linked List
Split a Circular Linked List into two halves
Sorted insert for circular linked list
We will soon be discussing the implementation of insert delete operations for circular linked lists.
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.