Code.GeekInterview.com
 
Code Samples C
 

Double Linked List


Code ResourceAuthor: mano.mithun  

Difficulty Level: Intermediate

Published: 3rd Aug 2010   Read: 3971 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

This a sample program of double linked list! the advantage of double over single is that singly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer. but in a doubly linked list, the node pointer points to the both previous and the next node. singly linked list has two nodes doubly linked list has three nodes A doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.

 


Sample Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct dllist {
  5.  int number;
  6.  struct dllist *next;
  7.  struct dllist *prev;
  8. };
  9.  
  10. struct dllist *head, *tail;
  11.  
  12. void append_node(struct dllist *lnode);
  13. void insert_node(struct dllist *lnode, struct dllist *after);
  14. void remove_node(struct dllist *lnode);
  15.  
  16. int main(void) {
  17.  struct dllist *lnode;
  18.  int i = 0;
  19.  
  20.  /* add some numbers to the double linked list */
  21.  for(i = 0; i <= 5; i++) {
  22.   lnode = (struct dllist *)malloc(sizeof(struct dllist));
  23.   lnode->number = i;
  24.   append_node(lnode);
  25.  }
  26.  
  27.  /* print the dll list */
  28.  for(lnode = head; lnode != NULL; lnode = lnode->next) {
  29.   printf("%dn", lnode->number);
  30.  }
  31.  
  32.  /* destroy the dll list */
  33.  while(head != NULL)
  34.   remove_node(head);
  35.  
  36.  return 0;
  37. }
  38.  
  39. void append_node(struct dllist *lnode) {
  40.  if(head == NULL) {
  41.   head = lnode;
  42.   lnode->prev = NULL;
  43.  } else {
  44.   tail->next = lnode;
  45.   lnode->prev = tail;
  46.  }
  47.  
  48.  tail = lnode;
  49.  lnode->next = NULL;
  50. }
  51.  
  52. void insert_node(struct dllist *lnode, struct dllist *after) {
  53.  lnode->next = after->next;
  54.  lnode->prev = after;
  55.  
  56.  if(after->next != NULL)
  57.   after->next->prev = lnode;
  58.  else
  59.   tail = lnode;
  60.  
  61.  after->next = lnode;
  62. }
  63.  
  64. void remove_node(struct dllist *lnode) {
  65.  if(lnode->prev == NULL)
  66.   head = lnode->next;
  67.  else
  68.   lnode->prev->next = lnode->next;
  69.  
  70.  if(lnode->next == NULL)
  71.   tail = lnode->prev;
  72.  else
  73.   lnode->next->prev = lnode->prev;
  74. }
  75.  
Copyright GeekInterview.com


Next Article: Single Linked List


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

finding loop in a doubly link ist
Comment posted by: swati on 2011-01-01T09:29:25

Popular Coders

# Coder NameHits
1. kaivalya198932313
2. mano.mithun19249
3. deepu0817246
4. meefriend4ever7824
5. chandrikakr7816
6. shashivaishnav6687
7. sadasivathavamani6563
8. venkatakrishnansvpr6437
9. Jimmy Zorald6277
10. sripri5431

Active Coders

Refined Tags