C Program to Implement Doubly Linked List using Singly Linked List

«
»
This C Program implements doubly linked list using singly linked list. It makes use of 2 pointers, one points at the current node, other points at the head. When user requests to move back, the pointer from head travels to a previous node of the current pointer. The pointer to previous node is the resulting node.

Here is a source code of the C program to implement doubly linked list using singly linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement Doubly Linked List using Singly Linked List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. void create(struct node **);
  14. void move (struct node *);
  15. void release(struct node **);
  16. void display(struct node *);
  17.  
  18. int main()
  19. {
  20.     struct node *p = NULL, *q = NULL;
  21.     int result, count;
  22.  
  23.     printf("Enter data into the list\n");
  24.     create(&p);
  25.     printf("Displaying list:\n");
  26.     display(p);
  27.     move(p);
  28.     release (&p);
  29.  
  30.     return 0;
  31. }
  32.  
  33. void move(struct node *head)
  34. {
  35.     struct node *p, *q;
  36.     int ch;
  37.  
  38.     p = q = head;
  39.     printf("\nPointer at %d\n", head->num);
  40.     do
  41.     {
  42.         printf("Select option:\n1. Move front\n2. Move back\n3. Exit\nYour choice: ");
  43.         scanf("%d", &ch);
  44.         switch(ch)
  45.         {
  46.         case 1: if(q->next != NULL)
  47.                 {
  48.                     q = q->next;
  49.                     printf("\nPointer at %d\n", q->num);
  50.                 }
  51.                 else
  52.                 {
  53.                     printf("\nPointer at last node %d. Cannot move ahead.\n", q->num);
  54.                 }
  55.                 break;
  56.         case 2: while (p->next != q)
  57.                 {
  58.                     p = p->next;
  59.                 }
  60.                 if (p == q)
  61.                 {
  62.                     printf("\nPointer at first node %d. Cannot move behind.\n", q->num);
  63.                 }
  64.                 else
  65.                 {
  66.                     q = p;
  67.                     p = head;
  68.                     printf("\nPointer at %d\n", q->num);
  69.                 }
  70.                 break;
  71.         case 3: return;
  72.         default: printf("\nInvalid choice entered. Try again\n");
  73.         }
  74.     } while (1);
  75. }
  76.  
  77. void create(struct node **head)
  78. {
  79.     int c, ch;
  80.     struct node *temp, *rear;
  81.  
  82.     do
  83.     {
  84.         printf("Enter number: ");
  85.         scanf("%d", &c);
  86.         temp = (struct node *)malloc(sizeof(struct node));
  87.         temp->num = c;
  88.         temp->next = NULL;
  89.         if (*head == NULL)
  90.         {
  91.             *head = temp;
  92.         }
  93.         else
  94.         {
  95.             rear->next = temp;
  96.         }
  97.         rear = temp;
  98.         printf("Do you wish to continue [1/0]: ");
  99.         scanf("%d", &ch);
  100.     } while (ch != 0);
  101.     printf("\n");
  102. }
  103.  
  104. void display(struct node *head)
  105. {
  106.     while (head != NULL)
  107.     {
  108.         printf("%d\t", head->num);
  109.         head = head->next;
  110.     }
  111.     printf("\n");
  112. }
  113.  
  114. void release(struct node **head)
  115. {
  116.     struct node *temp;
  117.     while ((*head) != NULL)
  118.     {
  119.         temp = *head;
  120.         (*head) = (*head)->next;
  121.         free(temp);
  122.     }
  123. }

advertisement
$ cc singledouble.c 
$ ./a.out
Enter data into the list
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 1
Enter number: 8
Do you wish to continue [1/0]: 1 
Enter number: 10
Do you wish to continue [1/0]: 0
 
Displaying list:
2	4	6	8	10	
 
Pointer at 2
Select option:
1. Move front
2. Move back
3. Exit
Your choice: 1
 
Pointer at 4
Select option:
1. Move front
2. Move back
3. Exit
Your choice: 1
 
Pointer at 6
Select option:
1. Move front
2. Move back
3. Exit
Your choice: 2
 
Pointer at 4
Select option:
1. Move front
2. Move back
3. Exit
Your choice: 2
 
Pointer at 2
Select option:
1. Move front
2. Move back
3. Exit
Your choice: 3

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter