Question: I'm writing code for a queue data structure in C that uses a doubly linked list to fill in the letters from A-Z. The functions

I'm writing code for a queue data structure in C that uses a doubly linked list to fill in the letters from A-Z. The functions and their descriptions are included but it seems that I'm doing something wrong as I keep running into a segmentation fault... Feel free to fix those issues and any other issues you may run into. Just simply go through the functions and see if any might need any tweaking to have the program running. However, don't add/remove any functions nor change the parameters. This question has been posted earlier with my attempt at filling in those functions, but they seemed to be a bit too confusing so I decided to repost while emptying those functions to make it easier for the expert to solve.

HEADER FILE:

#ifndef QUEUE_H #define QUEUE_H

#include #include #include #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 the queue. typedef struct queue { List *list; } Queue;

// Create and return an empty queue. Queue *queue_create(void);

// Destroy the queue. void queue_destroy(Queue *q);

// Add a value to the tail of the queue. // The value is copied to dynamically allocated memory. void queue_enqueue(Queue *q, const char *value);

// Remove the value at the head of the queue. // The caller is responsible for deallocating the returned pointer. char *queue_dequeue(Queue *q);

// Check if the queue is empty. bool queue_is_empty(const Queue *q);

#endif /* QUEUE_H */

MAIN FILE: #Include #include "queue.h"

Queue *queue_create(void){ }

void queue_destroy(Queue *q){

}

void queue_enqueue(Queue *q, const char *value){

}

char *queue_dequeue(Queue *q){ }

// Check if the queue is empty. bool queue_is_empty(const Queue *q){

}

int main(void) { // Create an empty queue. Queue *q = queue_create();

// Add the values A, B, ..., Z to the queue. char str[] = "A"; for (char ch = 'A'; ch <= 'Z'; ch++) { str[0] = ch; queue_enqueue(q, str); }

// Verify the values in the queue. bool ok = true; for (char ch = 'A'; ch <= 'Z'; ch++) { str[0] = ch; char *str2 = queue_dequeue(q); if (strcmp(str, str2) != 0) { ok = false; } free(str2); } if (!queue_is_empty(q)) { ok = false; } printf("Test the functioning of the queue ... %s ", ok ? "PASS" : "FAIL");

// Clean up. queue_destroy(q);

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!