写链表
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
typedef struct LNode {
int data;
struct LNode* next;
}LNode, * Linklist;
Linklist CreateList1(Linklist &L, int n) {
LNode* s; LNode* headnode; int x;
headnode = (Linklist)malloc(sizeof(struct LNode));
scanf("%d", &x);
for(int t=0;t<n;t++)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = headnode->next;
headnode->next = s;
scanf("%d", &x);
}
return headnode;
}
void print(Linklist o)
{
Linklist m=o->next;
while (m)
{
printf("%d", o->data);
m->next = m;
}
}
int main()
{
Linklist* p = NULL;
CreateList1(*p,5);
print(*p);
return 0;
}
照着别人能运行的写 也会提示p是nullptr
避免这样的结果
Linklist* p = NULL; 改为Linklist p = NULL;
p本身就是指针了啊
然后CreateList1(p,5);就可以了,不要加星号,因为createlist的参数是Linklist &
最后printf(p)就可以了