/
teach_link.c
54 lines (51 loc) · 855 Bytes
/
teach_link.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
#include <stdio.h>
typedef struct node {
int val;
struct node *next;
}node_t;
node_t *link_head = NULL;
node_t *make_node(int val)
{
node_t *p = (node_t *)malloc(sizeof(node_t));
if (p == NULL) {
perror("malloc");
return NULL
}
p->val = val;
p->next = NULL;
return p;
}
/* link end insert */
node_t *link_insert_e(node_t *head, int val)
{
node_t *p, *q;
p = make_node(val);
if (p == NULL) {
printf("link_insert_e fail\n");
return NULL;
}
if (head == NULL) {
head = p;
return head;
}
for (q = head; q->next != NULL; q = q->next)
;
q->next = p;
return head;
}
node_t *link_insert_b(node_t *head, int val)
{
node_t *p;
p = make_node(val);
if (p == NULL) {
printf("link_insert_e fail\n");
return NULL;
}
if (head == NULL) {
head = p;
return head;
}
p->next = head->next;
head->next = p;
return head;
}