Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I wrote this program in C that creates a doubly linked list and fills it with letters from A-Z. Everything seems to look fine for

I wrote this program in C that creates a doubly linked list and fills it with letters from A-Z. Everything seems to look fine for me, but the verifying functions given to use by the teachers return false and I can't seem to figure out what's wrong with my code. Please have a look at it, point out what's wrong with any of my functions and provide a solution in addition to an explanation. Do not add/remove any functions or provide answers where you write your own code in Python or any other language (Yes, some have done that and they don't solve my issue at all).

HEADER FILE: #ifndef LIST_H #define LIST_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 /* LIST_H */

MAIN FILE: #include #include #include #include #include \"list.h\"

static bool verify_forwards(List *lst); static bool verify_backwards(List *lst);

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

static struct node* make_node(const char* value) { struct node* result = (struct node*)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) { if (lst != NULL) { free(lst); } }

bool list_is_empty(const List* lst) { if (lst == NULL || (lst->head.next == lst->head.prev && lst->head.prev == &lst->head)) return true;

return false; }

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) { struct node* after = pos.node->next;

pos.node = after; return pos; }

ListPos list_prev(ListPos pos) { struct node* before = pos.node->prev;

pos.node = before; return pos; }

ListPos list_insert(ListPos pos, const char* value) {

struct node *node = make_node(value);

struct node *before = pos.node->prev; struct node *after = pos.node;

node->next = after; after->prev = node;

node->prev = before; before->next = node;

pos.node = node; return pos; }

ListPos list_remove(ListPos pos) { struct node* before = pos.node->prev; struct node* after = pos.node->next;

before->next = after; after->prev = before;

pos.node = after;

return pos; }

const char* list_inspect(ListPos pos) { return pos.node->value;

}

// 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 => str[0] = ch; pos = list_insert(pos, str); pos = list_next(pos); } }

// Traverse and verify a list in the forward direction. static bool verify_forwards(List *lst) { char str[2] = \"A\"; char ch = 'A' - 1; bool correct = true; ListPos pos = list_first(lst); ListPos end = list_end(lst); while (!list_pos_equal(pos, end)) { ch += 1; str[0] = ch; const char *str2 = list_inspect(pos); if (strcmp(str, str2) != 0) { correct = false; } pos = list_next(pos); } if (ch != 'Z') { correct = false; } return correct; }

// Traverse and verify a list in the backward direction. static bool verify_backwards(List *lst) { char str[2] = \"Z\"; char ch = 'Z' + 1; bool correct = true; ListPos pos = list_end(lst); ListPos first = list_first(lst); while (!list_pos_equal(pos, first)) { pos = list_prev(pos); ch -= 1; str[0] = ch; const char *str2 = list_inspect(pos); if (strcmp(str, str2) != 0) { correct = false; } } if (ch != 'A') { correct = false; } return correct; }

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

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

// Add some values. add_values(lst);

// Verify the list forwards. bool forwards_ok = verify_forwards(lst); printf(\"Test traversal in forward direction ... %s \", forwards_ok ? \"PASS\" : \"FAIL\");

// Verify the list backwards. bool backwards_ok = verify_backwards(lst); printf(\"Test traversal in backward direction ... %s \", backwards_ok ? \"PASS\" : \"FAIL\");

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

// Verify that the list is empty. bool empty_ok = list_is_empty(lst); printf(\"Test removal of all elements ... %s \", empty_ok ? \"PASS\" : \"FAIL\");

// 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_2

Step: 3

blur-text-image_3

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Differentiate the function. r(z) = 2-8 - 21/2 r'(z) =

Answered: 1 week ago