Table2
上面的代码给出警告和错误“在参数列表中声明的结构节点”和“将指针取消引用到不完整类型”
请帮助我使代码运行并解决错误。
答案 0 :(得分:2)
有几个问题。
struct node
仅在main
范围内声明。f
之前使用它。这是您的程序的外观:
#include <stdio.h>
#include <stdlib.h>
struct node // structure declared at global scope
{
int data;
struct node *next;
};
void f(struct node *a); // declare function
int main() // function has now return type int
{
struct node *first = (struct node*)malloc(sizeof(struct node));
struct node *second = (struct node*)malloc(sizeof(struct node));
struct node *third = (struct node*)malloc(sizeof(struct node));
scanf("%d %d %d", &(first->data), &(second->data), &(third->data));
first->next = second;
second->next = third;
third->next = NULL;
struct node *t = (struct node *)first;
f(t);
}
void f(struct node *a) // function has now return type void
{
while (a != NULL)
{
printf("%d", a->data);
a = a->next;
}
}
免责声明:该程序可以正确编译而不会发出警告,但是我没有检查它是否真的有意义。
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ===== Define structure outside function ===== */
/*
Use `typedef` to rename the data type.
`node_t` is equal with `struct node_t`, and
`node` is equal with `struct node_t *`.
*/
typedef struct node_t
{
int data;
struct node_t *next;
} node_t, *node;
/* Define link list
*/
typedef struct list_t
{
node head;
node rear;
int size;
} list_t, *list;
list list_create()
{
list l = NULL;
if ((l = malloc(sizeof(list_t))) == NULL)
{
exit(-1);
}
memset(l, 0, sizeof(list_t));
return l;
}
int list_destroy(list *pl)
{
node cur;
node prev;
if (pl == NULL || *pl == NULL)
return -1;
cur = (*pl)->head;
while (cur != NULL)
{
prev = cur;
cur = cur->next;
free(prev);
}
free(*pl);
*pl = NULL;
return 0;
}
int list_push(list l, int data)
{
node n;
if (l == NULL)
return -1;
if ((n = malloc(sizeof(node_t))) == NULL)
{
exit(-1);
}
n->data = data;
n->next = NULL;
if (l->head == NULL)
{
l->head = n;
}
else
{
l->rear->next = n;
}
l->rear = n;
l->size++;
return 0;
}
int list_pop(list l, int *pdata)
{
if (l == NULL || l->head == NULL)
{
return -1;
}
*pdata = l->head->data;
if (l->head == l->rear)
{
l->rear = NULL;
}
l->head = l->head->next;
l->size--;
return 0;
}
void list_foreach(list l, void (*func)(int data, void *arg), void *arg)
{
node cur = l->head;
while (cur != NULL)
{
func(cur->data, arg);
cur = cur->next;
}
}
static void print_node(int data, void *arg)
{
printf("%d\n", data);
}
int main(int argc, char *argv[])
{
int i;
int d;
list l;
l = list_create();
for (i = 0; i < 5; i++)
{
scanf("%d", &d);
getchar();
list_push(l, d);
}
list_foreach(l, print_node, NULL);
printf("Pop: [status %d], ", list_pop(l, &d));
printf("[value %d]\n", d);
printf("Pop: [status %d], ", list_pop(l, &d));
printf("[value %d]\n", d);
list_foreach(l, print_node, NULL);
list_destroy(&l);
return 0;
}