【发布时间】:2015-02-20 21:14:57
【问题描述】:
我目前正在做作业。在我的程序中我不得不使用队列,所以我用链表写了一个队列。但它似乎不喜欢我的语法。
所以我有结构
typedef struct Node{
pthread_t thread;
int threadID;
int securityMode; // nTS = 1, S = 2, U = 3
//int cluseterHalf; //either 1st or 2nd
struct NODE *next;
int executionTime;
int isLast;
}NODE;
typedef NODE *Link;
这就是我尝试入队的地方。
void Enqueue(Link node, Link *Queue){
Link previous = NULL;
Link current = *Queue;
while (current->isLast){
previous = current;
current = current->next;
}
if(previous == NULL){
node->next = current;
*Queue = node;
}
else{
previous->next = node;
node->next = current;
}
}
我尝试稍微更改我的代码,但出现此错误。
Cluster.c:162:13: warning: assignment from incompatible pointer type
[enabled by default]
current = current->next;
^
Cluster.c:165:16: warning: assignment from incompatible pointer type [enabled by default]
node->next = current;
^
Cluster.c:169:20: warning: assignment from incompatible pointer type [enabled by default]
previous->next = node;
^
Cluster.c:170:16: warning: assignment from incompatible pointer type [enabled by default]
node->next = current;
我尝试查看一些类似于我的 stackoverflow 问题。 1)Question1
所以我做了很多合乎逻辑和不合逻辑的尝试。我试着写 node->next = ¤t 因为 next 是一个指针,它将获取地址值。但它没有工作:( 我也试着做 oposit *(node->next) = current
我终于找到了适合我的正确选项,但我不确定这是否是我想要的。我在想我必须有 struct NODE *next 但是如果我将 NODE *next 更改为 NODE next ,则不会出现这些错误。但我得到了不同的:
Cluster.c:25:15: error: field ‘next’ has incomplete type
struct NODE next;
你能告诉我如何解决这个问题吗? 谢谢!
【问题讨论】:
-
这一行:'typedef NODE *Link;'这是一个坏主意,因为它在对指针进行 typedef 时产生了 typedef 的 typedef,在新的 typedef 名称中包含某种关于它指向什么以及它是一个指针的指示是更好的编码实践。建议“typedef NODE *pNode”,并且没有任何正当理由对结构定义进行 typedef。只需在需要时使用“struct Node”并删除 typedef 修饰符
标签: c pointers linked-list queue