Circular Linked List | Set 2 (Traversal)

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

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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.

```/* Function to traverse a given Circular linked list and print nodes */
void printList(struct Node *first)
{
struct Node *temp = first;

// If linked list is not empty
if (first != NULL)
{
// Keep printing nodes till we reach the first node again
do
{
printf("%d ", temp->data);
temp = temp->next;
}
while (temp != first);
}
}
```

Complete C program to demonstrate traversal. Following is complete C program to demonstrate traversal of circular linked list.

C/C++

```#include<stdio.h>
#include<stdlib.h>

/* structure for a node */
struct Node
{
int data;
struct Node *next;
};

/* Function to insert a node at the begining of a Circular
void push(struct Node **head_ref, int data)
{
struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node));
ptr1->data = data;

/* If linked list is not NULL then set the next of last node */
{
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */

}

/* Function to print nodes in a given Circular linked list */
{
{
do
{
printf("%d ", temp->data);
temp = temp->next;
}
}
}

/* Driver program to test above functions */
int main()
{
/* Initialize lists as empty */

/* Created linked list will be 11->2->56->12 */

printf("Contents of Circular Linked List\n ");

return 0;
}
```

Python

```
# Python program to demonstrate circular linked list traversal

# Structure for a Node
class Node:

# Constructor to create  a new node
def __init__(self, data):
self.data = data
self.next = None

# Constructor to create a empty circular linked list
def __init__(self):

# Function to insert a node at the beginning of a
def push(self, data):
ptr1 = Node(data)

# If linked list is not None then set the next of
# last node
temp = temp.next
temp.next = ptr1

else:
ptr1.next = ptr1 # For the first node

# Function to print nodes in a given circular linked list
def printList(self):
while(True):
print "%d" %(temp.data),
temp = temp.next
break

# Driver program to test above function

# Initialize list as empty

# Created linked list will be 11->2->56->12
cllist.push(12)
cllist.push(56)
cllist.push(2)
cllist.push(11)

print "Contents of circular Linked List"
cllist.printList()

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

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 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

GATE CS Corner    Company Wise Coding Practice

1.6 Average Difficulty : 1.6/5.0
Based on 31 vote(s)