In this doubly linked list, I will be making use of two pointers, startList and endList, where startList will point at the first node and endList will point at the last node. The startList pointer will help to traverse the list in FIFO order, while the endList pointer will help to traverse it in LIFO order. Follow these steps to create a doubly linked list and traverse it in either direction:
- Define a structure called node. To store content for the doubly linked list, define a data member in the node structure. Define two pointers called next and prev.
- A menu is displayed that shows four options: 1, to create a doubly linked list; 2, to display the elements of the list in LIFO order; 3, for displaying the elements in FIFO order; and 4, to quit. If the user enters 1, go to step 3. If the user enters 2, go to step 10. If the user enters 3, go to step 15. Finally, if the user enters 4, then it means they want to quit the program, so go to step 19.
- Initialize the startList pointer to NULL.
- Allocate memory for the new node.
- Ask the user for the value to be added to the doubly linked list. The value that's entered by the user is assigned to the data member of the node.
- Set the next and prev pointers of the node to NULL.
- If this node is the first node of the doubly linked list, set the startList pointer to point at the new node. If this node is not the first node, don't disturb the startList pointer and let it point at the node that it is currently pointing to.
- If this is the first node of the doubly linked list, set the endList pointer to at new node. If this is not the first node, perform the following steps:
- Set the next pointer of the new node to NULL.
- Set the prev pointer of the new node so that it points at the node pointed at by endList.
- Set the next pointer of endList so that it points at the new node.
- Set endList so that it points at the new node.
- Ask the user whether more elements have to be added to the doubly linked list. If the user wants to enter more, go to step 4; otherwise, display the menu by going to step 2.
- To display the linked list in LIFO order, let the temp pointer point at the node being pointed at by endList.
- Let step 12 and step 13 run until the temp pointer reaches NULL.
- Display the data member of the node being pointed at by temp.
- Set the temp pointer so that it points to where its prev pointer is pointing.
- The doubly linked list's content is displayed in LIFO order. Now, go to step 2 to display the menu again.
- Make the temp pointer point at the node being pointed at by the startList pointer.
- If the temp pointer is not NULL, display the data member of the node being pointed at by temp.
- Let the temp point at the node that its next pointer is pointing to.
- If temp has reached NULL, this means all of the nodes of the doubly linked list have been traversed. Now, you can display the menu by jumping to step 2. If temp has not reached NULL, then go to step 16 to display the rest of the elements of the doubly linked list.
- Exit the program.
The program for implementing a doubly or two-way linked list is as follows:
//doublylinkedlist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data;
struct node * next, * prev;
};
struct node * startList, * endList;
void createdoubly();
void list_lifo();
void list_fifo();
int main() {
int n = 0;
while (n != 4) {
printf("\n1. Creating a doubly linked list\n");
printf("2. Displaying elements in L.I.F.O. order\n");
printf("3. Displaying elements in F.I.F.O. order\n");
printf("4. Quit\n");
printf("Enter your choice 1/2/3/4: ");
scanf("%d", & n);
switch (n) {
case 1:
createdoubly();
break;
case 2:
list_lifo();
break;
case 3:
list_fifo();
break;
}
}
return 0;
}
void createdoubly() {
char k[10];
struct node * newNode;
startList = NULL;
strcpy(k, "yes");
while (strcmp(k, "yes") == 0 || strcmp(k, "Yes") == 0) {
if (startList == NULL) {
newNode = (struct node * ) malloc(sizeof(struct node));
printf("Enter the value to add: ");
scanf("%d", & newNode - > data);
newNode - > next = NULL;
newNode - > prev = NULL;
startList = newNode;
endList = startList;
} else {
newNode = (struct node * ) malloc(sizeof(struct node));
printf("Enter the value to add: ");
scanf("%d", & newNode - > data);
newNode - > next = NULL;
newNode - > prev = endList;
endList - > next = newNode;
endList = newNode;
}
printf("Want to add more yes/no? ");
scanf("%s", k);
}
printf("Doubly linked list is created\n");
}
void list_lifo() {
struct node * temp;
temp = endList;
if (temp != NULL) {
printf("The elements of the doubly linked list in L.I.F.O. order :\n");
while (temp != NULL) {
printf("%d\n", temp - > data);
temp = temp - > prev;
}
} else
printf("The doubly linked list is empty\n");
}
void list_fifo() {
struct node * temp;
temp = startList;
printf("The elements of the doubly linked list in F.I.F.O. order: \n");
while (temp != NULL) {
printf("%d\n", temp - > data);
temp = temp - > next;
}
}
Now, let's go behind the scenes so that we can understand the code.