簡體   English   中英

如何在沒有前、后或計數變量的 C 中實現隊列?

[英]How do I implement a queue in C without front, rear or count variables?

我被分配了在 C 中實現隊列的任務,但所有教程和視頻都使用前、后和計數變量,而我們的任務要求我們不這樣做。 我將如何完成這個程序? 在實現這個數據結構時,我的腦海里應該有什么?

對於上下文,以下是我們允許使用的結構:

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

typedef struct queue
{
    List *list;
} Queue;

我希望這對你有用。 我沒有過多測試這段代碼的可靠性,但是它通過了標准的 valgrind memory 測試,所以應該沒問題。 如果您發現一些錯誤,請告訴我。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define IS_NULL(ptr) ((ptr) == NULL)

#define READY 1

struct node
{
    struct node *next;
    struct node *prev;
    char *value;
};

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

typedef struct queue
{
    List *list;
} Queue;


int enqueue (Queue queue, char *key);
char *dequeue (Queue queue);
int queue_init (Queue * queue);
void dispose(Queue queue);

static struct node * new_node(char *key, struct node *next, struct node *prev);

int enqueue (Queue queue, char *key){
    char *new_string;
    struct node *head, *new, *prev;
    if (IS_NULL(queue.list) || IS_NULL(key) || IS_NULL((new_string = strdup(key)))){
        return !READY;
    }
    head = &queue.list->head;
    if (IS_NULL(head)){
        free(new_string);
        return !READY;
    }
    new = new_node(new_string, NULL, head->prev);
    if (IS_NULL(new)){
        free(new_string);
        return !READY;
    }
    if (IS_NULL(head->next)){
        head->next = new;
    } else {
        head->prev->next = new;
    }
    head->prev = new;
    return READY;
}

char *dequeue (Queue queue){
    char *result;
    struct node *head, *selected;
    if (IS_NULL(queue.list)){
        return NULL;
    }
    head = &queue.list->head;
    selected = head->next;
    head->next = selected->next;
    result = selected->value;
    free(selected);
    return result;
}

int queue_init (Queue * queue){
    if (IS_NULL(queue) || IS_NULL(queue->list = calloc(1, sizeof(List)))){
        return !READY;
    }
    return READY;
}

void dispose(Queue queue){
    if (IS_NULL(queue.list)){
        return;
    }
    struct node *ptr = queue.list->head.next, *ptr_1;
    while (ptr != NULL){
        free(ptr->value);
        ptr_1 = ptr;
        ptr = ptr->next;
        free(ptr_1);
    }
    free(queue.list);
}

static struct node * new_node(char *key, struct node *next, struct node *prev){
    struct node *new;
    if (IS_NULL((new = malloc(sizeof(struct node))))){
        return NULL;
    }
    new->value = key;
    new->next = next;
    new->prev = prev;
    return new;
}

void queue_print(Queue queue){
    char *string = dequeue(queue);
    if (IS_NULL(string)) return;
    fprintf(stdout, "\n%s", string);
    free(string);
}

int main(){
    Queue my_queue;
    if (queue_init(&my_queue) != READY){
        exit(EXIT_FAILURE);
    }
    enqueue(my_queue, "test1");
    enqueue(my_queue, "test2");
    queue_print(my_queue);
    enqueue(my_queue, "test3");
    enqueue(my_queue, "test4");
    queue_print(my_queue);
    queue_print(my_queue);
    dispose(my_queue);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM