-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlelinklist.c
108 lines (88 loc) · 1.41 KB
/
singlelinklist.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<stdlib.h>
#include<stdio.h>
typedef int elemtype;
typedef struct node
{
elemtype num;
struct node * next;
}node;
node *create(node * head);
void print();
node *create(node * head)
{
node * p1, *p2;
int i = 1;
p1 = p2 = (node *)malloc(sizeof(node));
scanf_s("%d", &p1->num);
p1->next = NULL;
while (p1->num>0)
{
if (head == NULL)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1; //cover memory
p1 = (node *)malloc(sizeof(node));
i = i + 1;
printf("p%d_addr=%d", i, p1);
printf("%d", head->num);
scanf_s("%d", &p1->num);
}
free(p1);
p1= NULL;
p2->next = NULL;
printf("over");
return head;
}
void insert(node * c, int i, int e)
{
node *p = c;
int j = 0;
while (p&&j < i - 1)
{
p = p->next; ++j;
}
node * s = (node*)malloc(sizeof(node));
s->num = e;
s->next = p->next;//consider the linklist is ok
p->next = s;
}
void delete(node * c, int i)
{
node * p = c;
int j=0;
while (p->next&&j<i-1) //before linklist
{
p = p->next; ++j;
}
node *q;
q = p->next;
p->next = q->next;
free(q);
}
void print(node * head)
{
node * temp;
temp = head;
while (temp!=NULL)
{
printf("%6d\n", temp->num);
temp = temp->next;
}
printf("end");
}
void main()
{
struct node *head;
head = NULL; //②建一个空表
head = create(head);/*创建单链表*/
print(head);/*打印单链表*/
insert(head, 2, 3222);
print(head);
delete(head, 3);
print(head);
}