Question: I need help comparing values in the function verify. The code is written in C and basically, I need to create an empty stack, fill

I need help comparing values in the function verify. The code is written in C and basically, I need to create an empty stack, fill it in with numbers then verify that the numbers filled in are the expected ones. The problem here is that in the function verify, I'm comparing doubles which is often a big no-no in C. How would I go about comparing the values in the stack with the expected values inside the array arr[] in the function verify?

#include #include #include #include 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;

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;

}

bool list_is_empty(const List *lst) { return lst == NULL; }

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

struct node *n = pos.node; pos.node = n->next; free(n); 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 <= 'Z'; ch++) { str[0] = ch; pos = list_insert(pos, str); pos = list_next(pos); } }

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

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!