C 如何正确使用链表实现队列

C 如何正确使用链表实现队列,c,pointers,linked-list,queue,C,Pointers,Linked List,Queue,我目前正在做家庭作业。在我的程序中,我必须使用queue,所以我编写了一个带有链表的队列。但它似乎不喜欢我的语法 所以我有了struct 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 execut

我目前正在做家庭作业。在我的程序中,我必须使用queue,所以我编写了一个带有链表的队列。但它似乎不喜欢我的语法

所以我有了struct

  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;
我试着研究一些与我类似的问题。 (一)

所以我做了很多逻辑和非逻辑的尝试。我试着写作 节点->下一步=&当前,因为下一步是指针,它将获得地址值。但它不起作用:( 我还尝试执行oposit*(node->next)=current

我终于找到了适合我的正确选项,但我不确定这是否是我想要的。我想我下一步必须使用struct NODE* 但如果我将NODE*改为NODE next,我不会得到这些错误。但我得到的是不同的错误:

Cluster.c:25:15: error: field ‘next’ has incomplete type
   struct NODE next;
你能告诉我怎么解决这个问题吗?
谢谢!

尝试在结构定义中将
结构节点*下一步;
更改为
结构节点*下一步;

编辑:

查看更多关于代码的信息,我认为您在指针分配方面存在一些问题。例如,我认为
链接当前=*队列;
将只分配
队列的数据,而不是地址,因此您无法访问“内部”。上一个问题也可能存在


另外,我真的不明白
链接的目的是什么,你可以只使用
节点

发布的代码在维护时会出现很多问题。此外,代码包含一些杂乱的区域,这使得理解/调试变得不必要的困难。有意义的变量名也非常有帮助。Suggest:

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;
    // notice removal of unneeded field isLast
};


void Enqueue(struct Node *newNode, struct Node **Queue)
{
    struct Node *current = *Queue;

    newNode->next = NULL;

    if(NULL == current)
    { // then handle special case of empty linked list
        *Queue = newNode;
    }

    else
    { // else, some nodes already in linked list
        // loop to end of linked list
        while (NULL != current->next)
        {
            // step to next node in linked list
            current = current->next;
        } // end while

        // add node to end of linked list
        current->next = newNode;
    } // end if
} // end function: Enqueue

这一行:“typedef NODE*Link;”是个坏主意,因为它在typedef定义指针时产生了typedef的typedef,在新的typedef名称中包含某种指示,说明它指向什么以及它是指针,这是一种更好的编码实践。建议"typedef NODE*pNode'并且从来没有任何有效的理由来定义结构。只要在需要时使用“struct NODE”并删除typedef修饰符anks!在节点之间更改节点会有帮助。但是你能解释一下为什么节点不工作和节点工作吗?因为我认为它们是一样的。我认为你在*队列上错了。因为队列Is已经是指针(链接),因此,当我将队列作为参数传递时,我将指针传递给节点数组。因此,我必须添加*以使Link current=to Queue。没有*我会出错。因为您在定义的最后定义了结构的名称NODE,同时,在结构内部,您使用NODE作为某种别名。哦,所以到那时NODE实际上并不存在是吗?没错,你可以说,在到达节点之前,只有节点存在。然后它被替换。谢谢!你能解释一下为什么我应该把struct放在队列中的参数中吗?我的意思是为什么我应该让struct NODE*newNode而不是NODE*newNode?