forked from Zxlove720/c-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2024.3.11.c
139 lines (129 loc) · 2.33 KB
/
2024.3.11.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct list
{
datatype data;
struct list* next;
}list;
list* creatnode(datatype value)
{
list* newnode = (list*)malloc(sizeof(list));
if (newnode == NULL)
{
printf("内存申请失败\n");
exit(1);
}
newnode->data = value;
newnode->next = NULL;
return newnode;
}
void addnode(list** head)
{
printf("请输入值-->\n");
datatype value;
scanf("%d", &value);
list* newnode = creatnode(value);
list* pointer = *head;
if (pointer == NULL)
{
*head = newnode;
printf("输入成功\n");
return;
}
while (pointer->next != NULL)
{
pointer = pointer->next;
}
pointer->next = newnode;
printf("输入成功\n");
}
void printflist(list** head)
{
list* pointer = *head;
while (pointer != NULL)
{
printf("%d ", pointer->data);
pointer = pointer->next;
}
printf("\n");
}
void insertnode(list** head)
{
printf("输入你想要插入的值\n");
datatype value;
scanf("%d", &value);
printf("输入你想要插入的位置\n");
int place;
scanf("%d", &place);
list* pointer = *head;
list* newnode = creatnode(value);
int i = 0;
while (pointer != NULL && i < place - 1)
{
pointer = pointer->next;
i++;
}
if (pointer == NULL)
{
printf("插入位置不合法,请重新选择!\n");
return;
}
newnode->next = pointer->next;
pointer->next = newnode;
}
void deletenode(list** head)
{
list* pointer = *head;
printf("输入你想删除第几个?\n");
int place;
scanf("%d", &place);
int i = 0;
while (pointer != NULL && i < place - 2)
{
pointer = pointer->next;
i++;
}
if (pointer == NULL)
{
printf("没有这个数,请重新输入!\n");
return;
}
list* temp = pointer->next;
pointer->next = temp->next;
free(temp);
temp = NULL;
}
int lengthlist(list** head)
{
list* pointer = *head;
int count = 0;
while (pointer != NULL)
{
count++;
pointer = pointer->next;
}
return count;
}
int main()
{
list* head = NULL;
printf("想要输入几个值?\n");
int n;
scanf("%d", &n);
int i = 0;
for (i = 0; i < n; i++)
{
addnode(&head);
}
printflist(&head);
insertnode(&head);
printflist(&head);
printf("%d\n", lengthlist(&head));
deletenode(&head);
printflist(&head);
int length = lengthlist(&head);
printf("%d\n", length);
return 0;
}