使用链表实现在 C 中排队
Enqueue in C using linked list implementation
我在以下代码中使用入队时收到此函数中的注释错误:我无法解决它们
void enqueue (tweet ** head, tweet ** tail, tweet * node)
{
tweet * p = (tweet*)malloc(sizeof(tweet));
p->id = 10000;
strcpy(p->text,"Hi");
strcpy(p->user, "Me");
p->next = NULL;
if(isEmpty(*head))
{
head = tail = p; // incompatible pointer type error here
}
else
{
tail->next = p; /*error: '*tail' is a pointer; did you mean to use '->'?, when I try to use '->'
still it shows this error*/
tail = p; // incompatible pointer type error here
}
}
这是我包含的头文件:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct microtweet
{
int id; //non-unique integer value
char user [51]; // the username / userid of the person who wrote the tweet
char text [141]; // the text of the tweet
struct microtweet *next; //dynamic connection to the next tweet
}
tweet;
tweet *head;
tweet *tail;
void enqueue (tweet ** head, tweet ** tail, tweet * node);
int isEmpty (tweet * head);
问题是 head
和 tail
是 tweet **
类型,而 p
是 tweet *
类型。您可以通过将 head
更改为 *head
并将 tail
更改为 *tail
.
来修复它
第一次修复:
// head = tail = p; <-- incorrect
*head = *tail = p;
第二次修正:
// tail->next = p; <-- incorrect
(*tail)->next = p;
第三次修复:
// tail = p; <-- incorrect
*tail = p;
我在以下代码中使用入队时收到此函数中的注释错误:我无法解决它们
void enqueue (tweet ** head, tweet ** tail, tweet * node)
{
tweet * p = (tweet*)malloc(sizeof(tweet));
p->id = 10000;
strcpy(p->text,"Hi");
strcpy(p->user, "Me");
p->next = NULL;
if(isEmpty(*head))
{
head = tail = p; // incompatible pointer type error here
}
else
{
tail->next = p; /*error: '*tail' is a pointer; did you mean to use '->'?, when I try to use '->'
still it shows this error*/
tail = p; // incompatible pointer type error here
}
}
这是我包含的头文件:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct microtweet
{
int id; //non-unique integer value
char user [51]; // the username / userid of the person who wrote the tweet
char text [141]; // the text of the tweet
struct microtweet *next; //dynamic connection to the next tweet
}
tweet;
tweet *head;
tweet *tail;
void enqueue (tweet ** head, tweet ** tail, tweet * node);
int isEmpty (tweet * head);
问题是 head
和 tail
是 tweet **
类型,而 p
是 tweet *
类型。您可以通过将 head
更改为 *head
并将 tail
更改为 *tail
.
第一次修复:
// head = tail = p; <-- incorrect
*head = *tail = p;
第二次修正:
// tail->next = p; <-- incorrect
(*tail)->next = p;
第三次修复:
// tail = p; <-- incorrect
*tail = p;