Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help creating a doubly linked list in C. The program is supposed to create a doubly-linked list, fill it with different letters as

I need help creating a doubly linked list in C. The program is supposed to create a doubly-linked list, fill it with different letters as shown, then verify the values, clear the list and delete it. Feel free to edit any function except for: list_insert, list_first and list_end.

IMPORTANT INFO: I do not need new functions, new libraries that do stuff that the current libraries I have included can't do, all I need is for someone to take a look at my code, find where I'm going wrong and provide a fix with explanations attached. Some of the functions I had cleared because my attempt at them made it very confusing for the expert to try and fix so just started from scratch. I have posted a similar question before and all I got was an entirely new code that does not resemble the one I wrote in the slightest, so please go through my functions, fix the errors and explain to me what I did wrong. Please do not rewrite my entire code with your own functions.

HEADER FILE: #ifndef NEW_H #define NEW_H

#include

// The type for a node in the list. struct node { struct node *next; struct node *prev; char *value; };

// The type for a list. typedef struct list { struct node head; } List;

// The type for a list position. typedef struct list_pos { struct node *node; } ListPos;

// Create and return an empty list. List *list_create(void);

// Deallocate the list (and all of its values, if any). void list_destroy(List *lst);

// Check if the list is empty. bool list_is_empty(const List *lst);

// Get the position of the first element. ListPos list_first(List *lst);

// Get the position after(!) the last element. ListPos list_end(List *lst);

// Check equality between two positions. bool list_pos_equal(ListPos p1, ListPos p2);

// Forward to the next position. ListPos list_next(ListPos pos);

// Backward to the previous position. ListPos list_prev(ListPos pos);

// Insert the value before the position and return the position of the new element. ListPos list_insert(ListPos pos, const char *value);

// Remove the value at the position and return the position of the next element. ListPos list_remove(ListPos pos);

// Get the value at the position. const char *list_inspect(ListPos pos);

#endif /* NEW_H */

MAIN FILE: #include #include #include #include #include "new.h"

static char *clone_string(const char *in) { size_t len = strlen(in); char *out = calloc(len + 1, sizeof(char)); strcpy(out, in); return out; }

static struct node *make_node(const char *value) { struct node *result = malloc(sizeof(struct node)); result->value = strdup(value); result -> next = NULL; result -> prev = NULL; return result; }

List *list_create(void) { List *lst = (List*)malloc(sizeof(struct list)); if(lst == NULL) { printf("No more memory! "); return 0; } lst->head.next = lst->head.prev = &lst->head; return lst;

}

void list_destroy(List *lst) { struct node *q; struct node *p = lst->head.next; while (p) { q = p; free(p); p = q; } }

bool list_is_empty(const List *lst) {

}

ListPos list_first(List *lst) { ListPos pos = { .node = lst->head.next }; return pos; }

ListPos list_end(List *lst) { ListPos pos = { .node = &lst->head }; return pos; }

bool list_pos_equal(ListPos p1, ListPos p2) { int count = 0; if(p1.node == p2.node) { return count; } count++; p1.node = p1.node -> next;

return -1; }

ListPos list_next(ListPos pos) { ListPos next; next.node = pos.node->next; return next; }

ListPos list_prev(ListPos pos) { ListPos prev; prev.node = pos.node->prev; return prev; }

ListPos list_insert(ListPos pos, const char *value) { // Create a new node. struct node *node = make_node(value);

// Find nodes before and after (may be the same node: the head of the list). struct node *before = pos.node->prev; struct node *after = pos.node;

// Link to node after. node->next = after; after->prev = node;

// Link to node before. node->prev = before; before->next = node;

// Return the position of the new element. pos.node = node; return pos; }

ListPos list_remove(ListPos pos) {

}

// Populate the list with A, B, ..., Z. static void add_values(List *lst) { char str[2] = "A"; ListPos pos = list_first(lst); for (char ch = 'A'; ch <= 'Z'; ch++) { str[0] = ch; pos = list_insert(pos, str); pos = list_next(pos); } } const char *list_inspect(ListPos pos) { return pos.node -> value;

}

// Remove all the added values from the list. static void remove_values(List *lst) { ListPos pos = list_first(lst); for (char ch = 'A'; ch <= 'Z' && !list_is_empty(lst); ch++) { pos = list_remove(pos); } }

// Test program. int main(void) { // Create an empty list. List *lst = list_create();

// Add some values. add_values(lst);

// Remove all added values. remove_values(lst);

// Clean up allocated resources. list_destroy(lst);

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions

Question

Without using a calculator calculate 13 2 11 2

Answered: 1 week ago

Question

Q.1. Taxonomic classification of peafowl, Tiger and cow ?

Answered: 1 week ago

Question

Q .1. Different ways of testing the present adulterants ?

Answered: 1 week ago

Question

Q.1. Health issues caused by adulteration data ?

Answered: 1 week ago