我正在用 C 写一个列表。下面是源代码:
#include <stdio.h>
#include <stdlib.h>
struct list {
int value;
struct list *next;
};
typedef struct list ls;
void add (ls **head, ls **tail, int val)
{
ls *new, *tmp1, *tmp2;
if (NULL == *head)
{
new = (ls*)malloc(sizeof(ls));
*head = new;
*tail = new;
new->value = val;
new->next = NULL;
return;
}
else
{
tmp1 = *head;
tmp2 = tmp1->next;
while (tmp2 != NULL)
{
tmp1 = tmp2;
tmp2 = tmp1->next;
}
new = (ls*)malloc(sizeof(ls));
new->value = val;
new->next = NULL;
*tail = new;
return;
}
}
void show (ls **head, ls **tail)
{
int i;
ls *tmp;
while (tmp->next != NULL)
{
printf("%d: %d", i, tmp->value);
i++;
tmp=tmp->next;
}
return;
}
int main (int argc, char *argv[])
{
ls *head;
ls *tail;
int n, x;
head = (ls*)NULL;
tail = (ls*)NULL;
printf("\n1. add\n2. show\n3. exit\n");
scanf("%d", &x);
switch (x)
{
case 1:
scanf("%d", &n);
add(*head, *tail, n);
break;
case 2:
show(*head, *tail);
break;
case 3:
return 0;
default:
break;
}
return 0;
}
当我用 gcc 编译它时
gcc -o lab5.out -Wall -pedantic lab5.c
我收到奇怪的错误:
lab5.c: In function ‘main’:
lab5.c:84:3: error: incompatible type for argument 1 of ‘add’
lab5.c:16:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
lab5.c:84:3: error: incompatible type for argument 2 of ‘add’
lab5.c:16:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
lab5.c:88:3: error: incompatible type for argument 1 of ‘show’
lab5.c:52:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
lab5.c:88:3: error: incompatible type for argument 2 of ‘show’
lab5.c:52:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
对我来说一切都好...
参数类型ls**
不是ls
编译器所说的。
有人看看有什么问题吗?
PS。我知道没有必要*tail
作为参数给出并且它没有被使用,但是它将是,因为我想开发这个“程序”......