Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (3.3k points)

A doubly linked list

1 Answer

0 like 0 dislike
by (3.3k points)
 
Best answer
An example of code showing how nodes can be inserted at a doubly linked list, how the list can easily be reversed, and how it can be printed in reverse.

#include <stdio.h>

#include <stdlib.h>

/* This data is not always stored in a structure, but it is sometimes for ease of use */

struct Node

{

 /* Sometimes a key is also stored and used in the functions */  

int data;  

struct Node* next;  

struct Node* previous;

};

void insert_at_beginning(struct Node **pheadNode, int value);

void insert_at_end(struct Node **pheadNode, int value);

void print_list(struct Node *headNode);

void print_list_backwards(struct Node *headNode);

void free_list(struct Node *headNode);

int main(void)

{  /* Sometimes in a doubly linked list the last node is also stored */  

struct Node *head = NULL;

printf("Insert a node at the beginning of the list.\n");  

insert_at_beginning(&head, 5);  

print_list(head);

printf("Insert a node at the beginning, and then print the list backwards\n");  

insert_at_beginning(&head, 10);  

print_list_backwards(head);

printf("Insert a node at the end, and then print the list forwards.\n");

insert_at_end(&head, 15);  

print_list(head);

free_list(head);

return 0;

}

void print_list_backwards(struct Node *headNode)

{  

if (NULL == headNode)  

{    

return;  

}  /*  Iterate through the list, and once we get to the end, iterate backwards to print  out the items in reverse order (this is done with the pointer to the previous node).  This can be done even more easily if a pointer to the last node is stored.  */  

struct Node *i = headNode;  

while (i->next != NULL)

{

i = i->next;                /* Move to the end of the list */  

}

while (i != NULL)

{    

printf("Value: %d\n", i->data);    

i = i->previous;  

}

}

void print_list(struct Node *headNode)

{  /* Iterate through the list and print out the data member of each node */  

struct Node *i;  

for (i = headNode; i != NULL; i = i->next)

{    

printf("Value: %d\n", i->data);  

}

}

void insert_at_beginning(struct Node **pheadNode, int value)

{  

struct Node *currentNode;

if (NULL == pheadNode)  

{    

return;  }  /*  This is done similarly to how we insert a node at the beginning of a singly linked  list, instead we set the previous member of the structure as well  */  

currentNode = malloc(sizeof *currentNode);

currentNode->next = NULL;  

currentNode->previous = NULL;  

currentNode->data = value;

if (*pheadNode == NULL)

{ /* The list is empty */    

*pheadNode = currentNode;    

return;  

}

currentNode->next = *pheadNode;  

(*pheadNode)->previous = currentNode;  

*pheadNode = currentNode;

}

void insert_at_end(struct Node **pheadNode, int value)

{  

struct Node *currentNode;

if (NULL == pheadNode)  

{    

return;  

}

  /*  This can, again be done easily by being able to have the previous element.  It  would also be even more useful to have a pointer to the last node, which is commonly  used.  */

currentNode = malloc(sizeof *currentNode);  

struct Node *i = *pheadNode;

currentNode->data = value;

currentNode->next = NULL;  

currentNode->previous = NULL;

if (*pheadNode == NULL)

{    

*pheadNode = currentNode;    

return;  

}

while (i->next != NULL)

{ /* Go to the end of the list */    

i = i->next;  

}

i->next = currentNode;  

currentNode->previous = i;

}

void free_list(struct Node *node)

{  

while (node != NULL)

{    

struct Node *next = node->next;    

free(node);    

node = next;  

}

}

Note that sometimes, storing a pointer to the last node is useful (it is more efficient to simply be able to jump straight to the end of the list than to need to iterate through to the end):

struct Node *lastNode = NULL;

In which case, updating it upon changes to the list is needed.

Sometimes, a key is also used to identify elements. It is simply a member of the Node structure:

struct Node

{  

int data;  

int key;  

struct Node* next;  

struct Node* previous;

};

The key is then used when any tasks are performed on a specific element, like deleting elements.

Winter 10 Days boot-camp classes(7 HRS Daily) will start from 5, 20 & 27 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

Goeduhub Important Lists Our Youtube Channels (For free E-learning)
About Us List of IITs Goeduhub Technologies
Contact Us List of NITs AI and Big Data-HADOOP E-Learning Series
  List of PSUs Smart Learning PLC-SCADA, IoT and Raspberry-PI
  List of Exams After Graduation  
...