here is my example of double linked list

any comments or criticisms welcomeCode:#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next, *previous; }Node; typedef struct LinkedList { struct Node *head, *tail; }LinkedList; void createhead(LinkedList *list); int getdata(void); Node *getmemory(void); void appendlist(LinkedList *list); void prependlist(LinkedList *list); void printlist(const LinkedList list); void reverseprintlist(const LinkedList list); int countnodes(const LinkedList list); void deletelist(LinkedList *list); void removenode(LinkedList *list); Node *findnode(LinkedList **list); int main() { LinkedList list; createhead(&list); appendlist(&list); appendlist(&list); appendlist(&list); printlist(list); prependlist(&list); prependlist(&list); printlist(list); reverseprintlist(list); removenode(&list); prependlist(&list); appendlist(&list); printlist(list); deletelist(&list); return 0; } void createhead(LinkedList *list) { Node *tmp_node = NULL; list->head = NULL; list->tail = NULL; tmp_node = getmemory(); if (!tmp_node) { printf("error getting memory"); exit(EXIT_FAILURE); } tmp_node->data = getdata(); tmp_node->previous = NULL; tmp_node->next = NULL; list->head = tmp_node; list->tail = list->head; } int getdata(void) { int data; printf("Please enter a number: "); scanf(" %d", &data); return data; } Node *getmemory(void) { return malloc(sizeof(Node)); } void appendlist(LinkedList *list) { Node *tmp_node = NULL; tmp_node = getmemory(); if (!tmp_node) { printf("error getting memory"); return; } tmp_node->data = getdata(); tmp_node->next = NULL; tmp_node->previous = list->tail; list->tail->next = tmp_node; list->tail = tmp_node; if (!list->head->next) { list->head->next = tmp_node; } } void printlist(const LinkedList list) { int count = 1; Node *current_node = list.head; while (current_node) { printf("node %d's data is %d\n", count, current_node->data); count++; current_node = current_node->next; } } void deletelist(LinkedList *list) { int count = 1; Node *tmp_node = NULL; while (list->head) { tmp_node = list->head->next; free(list->head); printf("node %d freed\n", count); count++; list->head = tmp_node; } } void prependlist(LinkedList *list) { Node *tmp_node = NULL; tmp_node = getmemory(); if (!tmp_node) { printf("error getting memory"); return; } tmp_node->data = getdata(); tmp_node->next = list->head; list->head->previous = tmp_node; list->head = tmp_node; } void reverseprintlist(const LinkedList list) { int count = countnodes(list); Node *current_node = list.tail; printf("\nprinting list in reverse\n"); while (current_node) { printf("node %d's data is %d\n", count , current_node->data); count--; current_node = current_node->previous; } } int countnodes(const LinkedList list) { int count = 0; Node *current_node = list.head; while (current_node) { count++; current_node = current_node->next; } return count; } void removenode(LinkedList *list) { Node *tmp_node = NULL; tmp_node = findnode(&list); if (!tmp_node) //node not found { printf("Invalid data entered data not found\n"); return; } if (!tmp_node->previous) //node is list head { list->head = tmp_node->next; list->head->previous = NULL; tmp_node->next = NULL; free(tmp_node); return; } if (!tmp_node->next) //node is list tail { tmp_node->previous->next = NULL; list->tail = tmp_node->previous; tmp_node->previous = NULL; free(tmp_node); return; } tmp_node->previous->next = tmp_node->next; tmp_node->next->previous = tmp_node->previous; tmp_node->next = NULL; tmp_node->previous = NULL; free(tmp_node); } Node *findnode(LinkedList **list) { int data; Node *current_node = (*list)->head, *tmp_node = NULL; printf("please enter the data to delete: "); scanf(" %d", &data); while (current_node) { if (current_node->data == data) { tmp_node = current_node; return tmp_node; } current_node = current_node->next; } return tmp_node; }

coop