C 循环链表中的队列

C 循环链表中的队列,c,linked-list,queue,C,Linked List,Queue,您好,我正在用C语言进行广度优先遍历,因此我需要实现一个队列,我想用双指针和循环链表来实现它。我知道这一定不是最简单的,但我想学更多 这是我的结构: struct queue { void *val; struct queue *next; }; 这是我的推送: void queue_push(struct queue **q, void *p) { struct queue *tmp = malloc(sizeof(struct queue));

您好,我正在用C语言进行广度优先遍历,因此我需要实现一个队列,我想用双指针和循环链表来实现它。我知道这一定不是最简单的,但我想学更多

这是我的结构:

struct queue
{
    void         *val;
    struct queue *next;
};
这是我的推送:

void queue_push(struct queue **q, void *p) {
    struct queue *tmp = malloc(sizeof(struct queue));
    tmp->val = p;
    if(q)
    {
            tmp->next = (*q)->next;
            (*q)->next = tmp;
    }
    else
    {
            tmp->next = tmp;
    }
}
如果我在每一行进行printf,它会告诉我“(*q)->next=tmp;”处的“非法指令(内核转储)”,我不知道我出了什么问题

编辑:我必须保持这个结构不变

void tree_breadth_print(struct tree *t)
{
    struct queue **q = malloc(sizeof(struct queue));
    struct tree *tmp = malloc(sizeof(struct queue));

    if(t == NULL)
    {
            printf("Tree is empty\n");
            return;
    }
    queue_push(q, t);

    while(!queue_is_empty(*q))
    {
            tmp = queue_pop(q);
            printf("%d", tmp->key);

            if(tmp->left != NULL)
                    queue_push(q, tmp->left);
                    printf("tmp->left->key = %d \n", tmp->left->key);
            if(tmp->right != NULL)
                    queue_push(q, tmp->right);
    }
    free(q);
    printf("\n");
}
树结构很简单:

struct tree {
    int           key;
    struct tree  *left, *right;
};

您可以在程序中使用此结构。 我将稍微更改您使用的名称: 考虑以下结构:

struct Node{
    int val;//supposing the type is int
    struct Node *next;
    struct Node *prev;
};typedef struct Node Node;
//
//
//
typedef struct queue{
    Node *head;
    Node *tail;
}//got a doubly queue
现在推送一个元素:

void queue_push(queue *q,int value)
{
    Node *n=(Node*)malloc(sizeof(Node));
    n->val=value;
    n->prev=NULL;
    if(q->head==NULL && q->tail==NULL)
    {
        n->next=NULL;
        q->head=n
        q->tail=n;
    }
    else
    {
        n->next=q->head;
        n->head->prev=n;
        q->head=n;
    }
}

我现在不知道我是否帮助你学习了一些新的东西,但不管怎样,它都不介意尝试一下,我认为它会起作用。

你可以在你的程序中遵循这个结构。 我将稍微更改您使用的名称: 考虑以下结构:

struct Node{
    int val;//supposing the type is int
    struct Node *next;
    struct Node *prev;
};typedef struct Node Node;
//
//
//
typedef struct queue{
    Node *head;
    Node *tail;
}//got a doubly queue
现在推送一个元素:

void queue_push(queue *q,int value)
{
    Node *n=(Node*)malloc(sizeof(Node));
    n->val=value;
    n->prev=NULL;
    if(q->head==NULL && q->tail==NULL)
    {
        n->next=NULL;
        q->head=n
        q->tail=n;
    }
    else
    {
        n->next=q->head;
        n->head->prev=n;
        q->head=n;
    }
}

我现在不知道我是否帮助你学习了一些新的东西,但不管怎样,它都不介意尝试一下,我认为它会起作用。

以下是我问题的答案:

void queue_push(struct queue **q, void *p) {
    struct queue *tmp = malloc(sizeof(struct queue));
    tmp->val = p;
    if(*q)
    {
        tmp->next = (*q)->next;
        (*q)->next = tmp;
    }
    else
        tmp->next = tmp;
    *q = tmp;
}
如果有人感兴趣,我也会给你排队,请随意使用

void* queue_pop(struct queue **q) {
    struct queue *tmp = (*q)->next;
    void *x = tmp->val;
    if(tmp == tmp->next)
        *q = NULL;
    else
        (*q)->next = tmp->next;
    free(tmp);
    return x;
}

以下是我问题的答案:

void queue_push(struct queue **q, void *p) {
    struct queue *tmp = malloc(sizeof(struct queue));
    tmp->val = p;
    if(*q)
    {
        tmp->next = (*q)->next;
        (*q)->next = tmp;
    }
    else
        tmp->next = tmp;
    *q = tmp;
}
如果有人感兴趣,我也会给你排队,请随意使用

void* queue_pop(struct queue **q) {
    struct queue *tmp = (*q)->next;
    void *x = tmp->val;
    if(tmp == tmp->next)
        *q = NULL;
    else
        (*q)->next = tmp->next;
    free(tmp);
    return x;
}

您应该向我们展示更多的代码,以便我们了解如何调用
queue\u push
函数,但这里有一些一般的想法:结构的更好名称应该是
queue\u node
,因为它不代表整个队列,因此您应该定义一个单独的类,其中包含指向队列中第一个/最后一个节点的指针,你应该把它传递给
queue\u push
函数。我已经用调用queue\u push的函数编辑了这个问题,我不会改变队列的结构或它的工作方式,我希望这样做,而不是其他方式。因为我使用了一个带有一个指针的简单队列和简单列表(不是圆形列表),所以您应该向我们展示更多的代码,这样我们就可以看到您是如何调用
Queue\u push
函数的,但是这里有一些一般的想法:对于您的结构来说,更好的名称应该是
Queue\u node
,因为它并不代表整个队列,为此,您应该定义一个单独的类,其中包含指向队列中第一个/最后一个节点的指针,并将其传递给
queue\u push
函数。我已经用调用queue\u push的函数编辑了这个问题,我不会更改队列的结构或其工作方式,我希望以这种方式完成,而不是其他方式。因为我使用了一个简单的队列,一个指针和一个简单的列表(不是圆形列表)