-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2024.3.29.c
116 lines (107 loc) · 1.85 KB
/
2024.3.29.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
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int datatype;
typedef struct list
{
datatype data;
struct list* next;
}list;
list* creathead()
{
list* head = (list*)malloc(sizeof(list));
if (head == NULL)
{
printf("error\n");
exit(1);
}
head->next = NULL;
return head;
}
bool isempty(list* head)
{
if (head->next == NULL)
{
return true;
}
return false;
}
void addnode(list* head)
{
printf("输入要入表的元素\n");
datatype element;
scanf("%d", &element);
list* newnode = (list*)malloc(sizeof(list));
if (newnode == NULL)
{
printf("error\n");
return;
}
newnode->data = element;
newnode->next = NULL;
list* pointer = head;
while (pointer->next != NULL)
{
pointer = pointer->next;
}
pointer->next = newnode;
}
void printlist(list* head)
{
list* pointer = head->next;
while (pointer != NULL)
{
printf("%d ", pointer->data);
pointer = pointer->next;
}
printf("\n");
}
void insertnode(list* head)
{
list* pointer = head;
printf("输入插入的元素\n");
datatype element;
scanf("%d", &element);
printf("请输入插入的位置\n");
int place;
scanf("%d", &place);
int i = 0;
while (pointer != NULL && i < place - 1)
{
pointer = pointer->next;
i++;
}
list* newnode = (list*)malloc(sizeof(list));
if (newnode == NULL)
{
printf("error\n");
return;
}
newnode->data = element;
newnode->next = pointer->next;
pointer->next = newnode;
}
int main()
{
list* head = creathead();
bool flag = isempty(head);
if (!flag)
{
printf("error\n");
return -1;
}
int i = 0;
int n;
printf("有几个元素要入表?\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
addnode(head);
}
printlist(head);
insertnode(head);
printlist(head);
return 0;
}