0 like 0 dislike

0 like 0 dislike
by (3.3k points)

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);

int main(void)

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

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

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

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

return 0;

}

{

{

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.  */

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;

}

}

{  /* 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;

{

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;

{ /* The list is empty */

return;

}

}

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

{

struct Node *currentNode;

{

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);

currentNode->data = value;

currentNode->next = NULL;

currentNode->previous = NULL;

{

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 eﬃcient 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 speciﬁc element, like deleting elements.