Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用节点指针设置链表解决分配中的不兼容类型_C - Fatal编程技术网

C 如何使用节点指针设置链表解决分配中的不兼容类型

C 如何使用节点指针设置链表解决分配中的不兼容类型,c,C,好的,这就是我目前所知道的 typedef struct node{ int *next; int val; }node; void pqPrint(){ node *current=front; printf("Queue Contains:"); while(current->next!=NULL){ printf(" %d ", current->val); node temp; temp.next=current->next; current->next=temp

好的,这就是我目前所知道的

typedef struct node{
int *next;
int val;
}node;
void pqPrint(){
node *current=front;
printf("Queue Contains:");
while(current->next!=NULL){
printf(" %d ", current->val);
node temp;
temp.next=current->next;
current->next=temp;
}
printf("\n");
}

我一直用
current->next=temp得到上面提到的错误

我在您的描述中没有发现任何错误,但无论如何,这里有一些事情要做。我根据上面的清单编写了一个完整的程序:

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

typedef struct node{
    struct node *next;
    int val;
}node;

node *front;

void pqPrint() {
    node *current=front;
    printf("Queue Contains:");
    while(current != NULL){
        printf(" %d ", current->val);
        current = current->next;
    }
    printf("\n");
}

void    main(void)
{
    front = malloc(sizeof(front));
    front->val = 10;
    front->next = NULL;
    pqPrint();
}
#包括
#包括
类型定义结构节点{
结构节点*下一步;
int-val;
}节点;
节点*前端;
作废印刷品(){
节点*电流=前端;
printf(“队列包含:”);
while(当前!=NULL){
printf(“%d”,当前->值);
当前=当前->下一步;
}
printf(“\n”);
}
真空总管(真空)
{
前端=malloc(sizeof(前端));
前->val=10;
前->下一步=空;
pqPrint();
}
几点意见: 1.大多数编译器都会抱怨,结构中的下一个元素不应该是int类型。 2.您需要将一个列表的开头声明为front,并对其进行初始化。 3.您需要为列表中的每个元素分配内存,通常使用malloc(),但请注意,我的示例没有验证结果(在现实生活中,您必须检查malloc()返回的NULL)。 4.如果指针指向左侧,则需要使用->(not.)引用指针指向的结构中的单独元素。
5.我猜你的温度变量把你弄糊涂了,所以我把它去掉了。请注意,在您的示例中,您总是会错过第一个元素。

首先……您的下一个元素不应该是int指针,大多数编译器都会警告您这一点。它应该是一个
结构节点*
。第二,尝试将完整的节点对象分配给指针。它们是两种完全不同的数据类型。将您的temp设置为
节点*
,它应该可以工作

temp
不是指针。因为“temp”不是指针。你为什么需要它?这看起来真是一团糟。为什么下一个指针是“int*”而不是“node*”?