Javatpoint Logo
Javatpoint Logo

Q. Program to create and display a circular linked list.

Explanation

In this program, we will create a circular linked list and print all the nodes present in the list.

Circular Linked List:

The circular linked list is a kind of linked list. First thing first, the node is an element of the list, and it has two parts that are, data and next. Data represents the data stored in the node and next is the pointer that will point to next node. Head will point to the first element of the list, and tail will point to the last element in the list. In the simple linked list, all the nodes will point to their next element and tail will point to null.

The circular linked list is the collection of nodes in which tail node also point back to head node. The diagram shown below depicts a circular linked list. Node A represents head and node D represents tail. So, in this list, A is pointing to B, B is pointing to C and C is pointing to D but what makes it circular is that node D is pointing back to node A.

Program to create and display a circular linked list

Algorithm

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: add() and display() .
  3. add() will add the node to the list:
    1. It first checks whether the head is null, then it will insert the node as the head.
    2. Both head and tail will point to the newly added node.
    3. If the head is not null, the new node will be the new tail, and the new tail will point to the head as it is a circular linked list.
  4. display() will show all the nodes present in the list.
    1. Define a new node 'current' that will point to the head.
    2. Print current.data till current will points to head
    3. Current will point to the next node in the list in each iteration.

Solution

Python

Output:

Nodes of the circular linked list: 
1 2 3 4

C

Output:

Nodes of the circular linked list: 
1 2 3 4

JAVA

Output:

Nodes of the circular linked list: 
1 2 3 4

C#

Output:

Nodes of the circular linked list: 
1 2 3 4

PHP

Output:

Nodes of the circular linked list: 
1 2 3 4

Next Topic#




Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials