Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

28
Feb

Circular Doubly Linked List With Out Using Special Head or Tail Node

Related Blog Items

Circular double linked list can be traversed clockwise or anticlockwise. In this list all nodes are linked as shown in the below figure. The only difference between double linked list and circular double linked list is last node points to first node in the list, first node points to last node instead of pointing to NULLs.

 

circular double linked list

here is the c structure for a node in a circular double linked list and it is same as double linked list.

struct Node
{
  void* info; //data
  Node* next; //used to point to next node
  Node* prev; //used to point to previous node 
};

and in this implementation as well (earlier post double linked list also doen’t maintain special nodes), we are not using any special nodes(head or tail), so while operating on the list one must be careful to maintain logical head or tail according to the need.

here is the code for inserting, deleting nodes

3ba20300b4ee03b02abc542cdfda5e37001

the below routine prints circular linked list, we can give any node in the list as a starting point

//given any node in list, prints entire list
int printlist(Node *h)
{
  Node* t = h;
 
  do
  {
    printf(" %d <->", t->info);
    t = t->next;
  }while(t && (t != h));
 
  printf(" NULL\n");
  return 0;
}

searching a circular double linked list not different from double linked list just the difference is sentinels (nodes whose addresses are NULL) are not present in circular double linked list unless it is broken.

//given any node in list, search entire list
Node *searchlist(Node *h, void *info)
{
  Node* after = h;
 
  //search all nodes after n (if any)
  do
  {
    if (after->info == info)
    {
      return after;
    }
    after = after->next;
  }while(after && (after != h));
 
 
  return NULL;
}

detecting a broken link in circular double linked list…

int detectlist(Node *n)
{
  int brokenlinks = 0;
  Node *tmp = n;
  Node* after = n->next;
  Node* before = n->prev;
 
  //detect abnormalities in the list (broken links) after n (if any)
  do
  {
    if ((after->prev != tmp) || (tmp->next != after))
    {
      brokenlinks++;
    }
 
    after = after->next;
    tmp = tmp->next;
  }while(after && tmp && (after != n->next) && (tmp != n));
 
  return brokenlinks;
}

download the above program [circular double linked list.c

let me know your comments and suggestions…

Popularity: 67%

You need to log on to convert this article into PDF


Related Blog Items

1 Comment

  • Jamal  said:

    i really got good help from it.
    Any person accessing this web site must atleast read the program


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image