-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest72.C
62 lines (54 loc) · 987 Bytes
/
test72.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//题目:创建一个链表
//ps:这就是实例的代码,还有一个有错误的版本,可以去看看错在哪里
//
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
struct LNode * next;
}LNode,* LinkList;
LinkList CreateList(int n);
void print(LinkList h);
int main()
{
LinkList Head = NULL;
int n;
printf("输入要创建的节点数量:\n");
scanf("%d",&n);
Head = CreateList(n);
printf("刚创建的各个链表元素的值为:\n");
print(Head);
printf("\n\n");
system("pause");
return 0;
}
LinkList CreateList(int n)
{
LinkList L,p,q;
int i;
L = (LNode *)malloc(sizeof(LNode));
if (!L)
return 0;
L->next = NULL;
q = L;
for (i = 1;i <= n;i++)
{
p = (LinkList)malloc(sizeof(LNode));
printf("输入第%d个元素的值:\n",i);
scanf("%d",&(p->data));
p->next = NULL;
q->next = p;
q = p;
}
return L;
}
void print(LinkList h)
{
LinkList p = h->next;
while (p != NULL)
{
printf("%d",p->data);
p = p->next;
}
}