【问题标题】:How to correctly implement queue with using Linked list如何使用链表正确实现队列
【发布时间】: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 = &current 因为 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


【解决方案1】:

尝试在您的结构定义中将struct NODE *next; 更改为struct Node *next;

编辑:

查看更多代码,我认为您在指针分配方面存在一些问题。例如,我认为Link current = *Queue; 只会分配Queue 的数据,而不是地址,因此您无法访问“内部”。以前的问题也可能相同。

另外,我不太明白Link 的目的是什么,你可以只用NODE

【讨论】:

  • 谢谢!将 NODE 更改为 Node 有所帮助。但是你能解释一下为什么节点不起作用而节点起作用吗?因为我认为它们是一样的。
  • 我认为你在 *Queue 上错了。因为队列已经是一个指针(链接),所以当我将队列作为参数传递时,我将指针传递给节点数组。所以我必须添加 * 以使 Link current = 到 Queue。没有 * 我得到错误。
  • 因为您在定义的最后定义了结构的名称 NODE,同时在结构内部,您使用 Node 作为某种别名。
  • 哦,到那时 NODE 真的不存在了吧?
  • 没错,你可以说在你到达 NODE 之前,只有 Node 存在。然后它被替换了。
【解决方案2】:

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

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

【讨论】:

  • 谢谢!你能解释一下为什么我应该把结构放在 Enqueue 的参数中吗?我的意思是为什么我应该有 struct Node *newNode 而不是 Node *newNode?​​span>
猜你喜欢
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多