Exforsys

Home arrow Technical Training arrow C Tutorials

C Circular Linked Lists

Page 1 of 2
Author:      Published on: 26th Jun 2011

In this tutorial you will learn about C Programming - What is Circular Linked List, Adding Nodes to Circular Linked Lists, Traversing a Circularly Linked List and working with Circularly Linked List Example.

Circular linked lists are usually used for a queue or stack type situation, or for implementing a round robin algorithm in system level programming. You could also use one for a multiplayer game where you were keeping track of player's turns where it just repeats until the game ends.

They are exactly the same as a singly linked list, but instead of setting the next pointer in the last node of the list to NULL, you set it to point to head. Hence the name circular. Instead of a head pointer, they commonly use a tail pointer instead to keep track of the last node.

Ads

A cingularly linked list node looks exactly the same as a linear singly linked list. Here's the node for the example programming example.

Sample Code
  1. struct cllnode {
  2.           char name[4];
  3.           struct cllnode *next;
  4.         };
Copyright exforsys.com


It should be familiar from the previously introduced linear linked list, just a data portion and the next pointer.

Adding Nodes to Circular Linked Lists

Adding nodes and creating new circularly linked lists is very similar to the linear ones we learned previously. Let's look at our example to illustrate.

Sample Code
  1. void addNode(struct cllnode *newnode) {
  2.           //List is empty. Create new list.                                            
  3.           if (tail == NULL) {
  4.  
  5.             tail = newnode;
  6.             newnode->next = tail;
  7.  
  8.           } else {
  9.             //List isn't empty so add at the tail. Remember "first in first out."      
  10.             newnode->next = tail->next;
  11.             tail->next = newnode;
  12.             tail = newnode;
  13.           }
  14.         }
Copyright exforsys.com


That addNode function is all it takes to handle both the empty and non-empty singularly linked list cases when adding new nodes. You add at the back of the list in this linked list case and that's one of the reasons these are used for modeling queues and FIFO stacks.

Just as in the singularly linked list case you first check if the list pointer (no matter what you are calling it) is NULL. Then set the tail equal to the new node being added and make its pointer point back at itself, thus completing the circle.

When there are nodes in the list, you first set the new node's next pointer to tail->next because you are adding it just after tail, not at tail. You may be able to see why we use tail pointers rather than head pointers for circularly linked lists. The head would be 2 pointers away from where we perform insertions.

After that, you set tail's next pointer to the new node and set tail equal to the new node. Viola, we have inserted a new node at the end of the list, and tail is now pointing at it.

Traversing a Circularly Linked List

Traversing a circularly linked list is a little different than the regular type, due to the fact that there is no NULL termination to tell you when to stop. However, we can just use the tail pointer for this purpose as well.

As we see in this snippet of our example program, it's not any more difficult to traverse, it's just different

Sample Code
  1. if (tail != NULL) {
  2.             current = tail->next;
  3.  
  4.             while (current != tail) {
  5.  
  6.                 ...
  7.  
  8.         current = current->next;
  9.             }
  10.  
  11.             current = tail;
  12.  
  13.             ...
  14.           }
Copyright exforsys.com


We begin with the familiar check for the list's existence. If we have a list, we set the current pointer to the node after tail. From here we can just go through the nodes until we return to tail, so we use a while loop to do that. The traversal is the same as before in that you just set the current pointer to the next one at the end of the wile loop.

Ads

Now if you want to do something when you get to the tail you must do it after the while loop traversal, because our condition stopped the traversal at the node just before tail and started just after it. As you can see we had to add an addition print out block after the while loop to be able to see the last node's information printed out.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments