FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
399 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k points)

A doubly linked list

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.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.

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...