【问题标题】:Multiple Queues in CC中的多个队列
【发布时间】:2014-12-04 19:05:17
【问题描述】:

我有一个基本的队列设计,但我想要多个队列。现在看起来我需要另一个 queue.h 文件并用不同的名称替换 head 和 tail,但我确信有更好的方法吗?

queue.h *已编辑

#include<stdlib.h>  // malloc

struct Node {
    int data;
    struct Node* next;
};

struct Queue {
  struct Node *head, *tail;
};

struct Queue *QueueInit() {
    //allocate and initialize a queue
    struct Queue *thisQueue = malloc(sizeof *thisQueue);
    thisQueue->head = NULL;
    thisQueue->tail = NULL;
    return thisQueue;
}


void push(struct Queue *myQueue, int x) {
    struct Node *temp; 
    temp = malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = NULL;  
    if(myQueue->head == NULL && myQueue->tail == NULL) { //empty
        myQueue->head = myQueue->tail = temp;
        return;
    }
    myQueue->tail->next = temp;
    myQueue->tail = temp;
}

void pop(struct Queue *myQueue) {
    struct Node* temp = myQueue->head;
    if(myQueue->head == NULL) return;   //empty
    if(myQueue->head == myQueue->tail) {
        myQueue->head = myQueue->tail = NULL;
    }
    else {
        myQueue->head = myQueue->head->next;
    }
    free(temp);
}

我怎样才能像这样创建多个队列?

ma​​in.c

int main() {
    struct Node iceCreamLine;
    struct Node bathroomLine;

    iceCreamLine.push(13);
    bathroomLine.push(2);


    //It looks like I will have to use this syntax then instead?
    struct Queue *droneQueue;    //(THIS IS LINE 5)
    push(&droneQueue,1666);
    push(&droneQueue,100);

    printf("--> %d",&droneQueue->head->data);
    printf("--> %d",&droneQueue->head->next->data);

}

第一个 printf 有效,但第二个给我一个分段转储。这里还有警告

main.c:在函数“main”中: main.c:6:2: 警告:从不兼容的指针类型传递“push”的参数 1 [默认启用] 在 queue.c:2:0 包含的文件中: queue.h:21:6:注意:预期为“struct Queue *”,但参数的类型为“struct Queue **” main.c:7:2: 警告:从不兼容的指针类型传递“push”的参数 1 [默认启用] 在 queue.c:2:0 包含的文件中: queue.h:21:6:注意:预期为“struct Queue *”,但参数的类型为“struct Queue **” main.c:9:2:警告:格式“%d”需要“int”类型的参数,但参数 2 的类型为“int *”[-Wformat] main.c:10:2:警告:格式“%d”需要“int”类型的参数,但参数 2 的类型为“int *”[-Wformat]

【问题讨论】:

  • 要使推送符号iceCreamLine.push(13); 起作用,您需要在struct Node 结构中添加一个元素void (*push)(int);,并且您必须确保每个struct Node 都已正确初始化,因此push 元素指向正确的函数。
  • headtail 等创建具有不同名称的文件的新版本将是一场灾难。 +1 表示怀疑并在实施之前费心询问。

标签: c queue


【解决方案1】:
struct Queue {
  struct Node *head, *tail;
};

添加一个QueueInit函数来分配和初始化一个队列,返回一个指向struct Queue的指针。将指向struct Queue 的指针传递给pushpop,并去掉你的全局headtail

【讨论】:

  • 我在上面编辑了我的代码,但我不是来自 C 背景。头部和尾部会得到什么? push 和 pop 怎么知道 head 和 tail 是什么?谢谢
  • 根据您的其他代码,您可以将headtail 初始化为NULL。您可以将struct Queue * 添加到push 和pop 的参数列表中——void push(struct Queue *myQueue, int x)void pop(struct Queue *myQueue)。在pushpop 中,您将引用myQueue-&gt;headmyQueue-&gt;tail。此外,您的QueueInit 应该有一个return thisQueue;
  • 谢谢,我想我几乎可以正常工作了。我确实想要像 iceCreamLine.push(13); 这样的语法。而不是 push(&iceCreamLine,13),但这会做。我将代码编辑为大家所说的内容,但在我的第二个 printf 上收到警告和分段转储。我已经开始讨厌指针了!
  • 当您将droneQueue 声明为指针时,您不应该将它与&amp; 一起发送到push&amp;droneQueue 表示指向 droneQueue 的指针,因此正如您编写的那样,它是指向指针的指针。摆脱你所有的&amp;s 应该会有所帮助。另外,不要忘记初始化droneQueue,并为其分配QueueInit的返回值。
猜你喜欢
  • 2018-03-09
  • 2020-04-19
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 2015-06-10
相关资源
最近更新 更多