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.
/*
* C Program to Implement Doubly Linked List using Singly Linked List
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void move (struct node *);
void release(struct node **);
void display(struct node *);
int main()
{
struct node *p = NULL, *q = NULL;
int result, count;
printf("Enter data into the list\n");
create(&p);
printf("Displaying list:\n");
display(p);
move(p);
release (&p);
return 0;
}
void move(struct node *head)
{
struct node *p, *q;
int ch;
p = q = head;
printf("\nPointer at %d\n", head->num);
do
{
printf("Select option:\n1. Move front\n2. Move back\n3. Exit\nYour choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: if(q->next != NULL)
{
q = q->next;
printf("\nPointer at %d\n", q->num);
}
else
{
printf("\nPointer at last node %d. Cannot move ahead.\n", q->num);
}
break;
case 2: while (p->next != q)
{
p = p->next;
}
if (p == q)
{
printf("\nPointer at first node %d. Cannot move behind.\n", q->num);
}
else
{
q = p;
p = head;
printf("\nPointer at %d\n", q->num);
}
break;
case 3: return;
default: printf("\nInvalid choice entered. Try again\n");
}
} while (1);
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
void display(struct node *head)
{
while (head != NULL)
{
printf("%d\t", head->num);
head = head->next;
}
printf("\n");
}
void release(struct node **head)
{
struct node *temp;
while ((*head) != NULL)
{
temp = *head;
(*head) = (*head)->next;
free(temp);
}
}
$ 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Buy Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice Design & Analysis of Algorithms MCQ