Question: I need help creating a queue that uses a doubly-linked list in C and fill the queue with the letters A-Z. The program utilizes a
I need help creating a queue that uses a doubly-linked list in C and fill the queue with the letters A-Z. The program utilizes a header and main file even though it's a small program, which should help make it easier to use regardless. Feel free to fill in the functions but don't add/remove any functions nor change anything in the structs.
HEADER FILE:
#ifndef QUEUE_H #define QUEUE_H
#include
// The type for a list. typedef struct list { struct node head; } List;
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 "queue.h"
Queue *queue_create(void){
}
void queue_destroy(Queue *q){
}
void queue_enqueue(Queue *q, const char *value){
}
char *queue_dequeue(Queue *q){
}
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
Get step-by-step solutions from verified subject matter experts
