gpt4 book ai didi

c - 尝试在 C 中创建队列时出错

转载 作者:行者123 更新时间:2023-11-30 19:10:38 24 4
gpt4 key购买 nike

我正在编写一个简短的程序来创建带有时间戳的节点的队列,并最终将分成线程。我陷入了编写队列部分并尝试打印出值的困境。编译器在不同的行给了我 3 个错误。

  1. 第 16 行和第 29 行“错误:二进制 == 的操作数无效(具有‘node’和‘void *’)”

  2. 第 35、38 和 57 行上的“错误:从类型‘struct node *’分配给类型‘node’时出现不兼容的类型”。

  3. 第 45 行出现“错误:无效初始化程序”。

我不明白为什么会出现这些错误,也不知道如何修复它们。这是代码:

  1 #include<stdio.h>
2 #include<time.h>
3 #include<pthread.h>
4
5 typedef struct node{
6 int fd;
7 time_t seconds;
8 struct node *next;
9 }node;
10
11 void dequeue(node);
12 void enqueue(node, int);
13
14 void dequeue(node head){
15
16 if(head == NULL)
17 return;
18
19 head.next = head.next->next;
20 return;
21 }
22
23 void enqueue(node head, int fd){
24
25 node curr;
26 curr.fd = fd;
27 curr.seconds = 0;
28
29 if(head == NULL){
30 head = curr;
31 return;
32 }
33
34 while(head.next != NULL){
35 head = head.next;
36 }
37
38 head.next = curr;
39 return;
40 }
41
42 int main(){
43 node head = NULL;
44 int i;
45 printf("head ");
46
47 for(i = 0; i < 5; i++){
48 enqueue(head, i);
49 }
50
51 printf("head");
52 node tmp;
53
54 for(i = 0; i < 5; i++){
55 printf("-> node %d with value %d", i, tmp.fd);
56 tmp = tmp.next;
57 }
58
59 return 0;
60 }

最佳答案

您的代码中有一堆 bug 和错误。我尝试修复您的代码,同时尝试坚持您正在解决的问题。

始终包含 stdlib.h,因为它会抑制某些指针算术警告。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

结构体的类型定义是一种不好的做法,并且使用 struct 关键字访问类型定义的结构体也是错误的。

struct node
{
int fd;
time_t seconds;
struct node *next;
};

在定义函数签名时,将变量名称与其类型放在一起,以获得自描述的函数原型(prototype)。

struct node * dequeue(struct node *head);
struct node * enqueue(struct node *head, int fd);

原来的出队函数没有释放分配的空间。另外,您需要返回struct node的指针才能更新head指针。

struct node * dequeue(struct node *head)
{
if(head != NULL)
{
struct node *tmp = head;
tmp = head->next;
free(head);
head = tmp;
return(head);
}
}

原始enqueue函数中的ifwhile条件都是错误的。此外,将 return 放入 void 函数中也是不正确的。

struct node * enqueue(struct node *head, int fd)
{
struct node *curr = malloc(sizeof(struct node));
curr->fd = fd;
curr->seconds = 0;
curr->next = NULL;

if(head == NULL)
head = curr;
else
{
while(head->next != NULL)
head = head->next;
head->next = curr;
head = head->next;
}
return(head);
}

您的主函数没有正确的参数并且存在一些问题。这是一个更详细的版本。

int main(int argc, char *argv[])
{
struct node *head = NULL;
struct node *tmp = head;

// Enqueue 5 nodes
int i;
for(i = 0; i < 5; i++)
{
head = enqueue(head, i);
printf("node %d with value %d enqueued\n", i, head->fd);
if(i == 0)
tmp = head;
}
printf("\n");

//Traverse the queue
head = tmp;
for(i = 0; i < 5; i++)
{
printf("node %d with value %d traversed\n", i, head->fd);
head = head->next;
}
printf("\n");

// Dequeue 5 nodes
head = tmp;
for(i = 0; i < 5; i++)
{
printf("node %d with value %d dequeued\n", i, head->fd);
head = dequeue(head);
tmp = head;
}
return (0);
}

最后一句话:如果您学习如何在 C 中使用指针,生活会更容易!

关于c - 尝试在 C 中创建队列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com