Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data structures C language questions: Make a doubly linked list and inplement th

ID: 3705895 • Letter: D

Question

Data structures C language questions:

Make a doubly linked list and inplement the following functions using the following libraries: stdio.h, assert.h, stdlib.h

Structs should be used as shown:

struct Node{

char* val;

struct Node* next;

struct Node* prev;

};

typdef struct{

struct Node* first;

struct Node* last

; } DBLinked;

Do what the following functions ask:

void initDBLinked(struct DBLinked** dbLinked) - initialize the doubly linked list to an empty list so calling, we should be able to add element to it by calling add(struct DBLinked* dbLinked, char* val) and remove element by calling remove(struct DBLinked* dbLinked)

void destroyDBLinked(DBLinked **dbLinked) - delete all nodes and any memory allocated

void merge(struct DBLinked * dbLinked ) - combines the two string in the front two nodes of the list and stores the new combined string in the first node and deletes the second node.

Explanation / Answer

#include #include struct node { int data; struct node * previous; struct node * next; }; struct linked_list { struct node * first; struct node * last; }; struct linked_list * init_list(struct node * node) { struct linked_list * ret = malloc(sizeof(struct linked_list)); ret->first = node; ret->last = node; return ret; } struct node * create_node(int data) { struct node * ret = malloc(sizeof(struct node)); ret->data = data; ret->previous = NULL; ret->next = NULL; return ret; } void destroy_list(struct linked_list * list) { struct node * node = list->first, *next = NULL; while(node != NULL) { next = node->next; free(node); node = next; } free(list); } void push_front(struct linked_list * list, struct node * node) { if(list->first != NULL) { node->next = list->first; list->first->previous = node; } else list->last = node; list->first = node; } void push_back(struct linked_list * list, struct node * node) { if(list->last != NULL) { node->previous = list->last; list->last->next = node; list->last = node; } else push_front(list, node); } void insert_after(struct node * node, struct node * to_add) { to_add->previous = node; to_add->next = node->next; node->next = to_add; } void remove_node(struct node * node) { node->previous->next = node->next; node->next->previous = node->previous; free(node); }