-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
242 lines (216 loc) · 6.44 KB
/
functions.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdlib.h>
#include "structs.h"
nodes *insertNode(nodes *head, book newBook)
{
nodes *newNode = (nodes *)malloc(sizeof(nodes));
if (newNode == NULL)
{
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
newNode->book_data = newBook;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
nodes *current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
}
return head;
}
nodes *deleteNode(nodes *head, int bookID)
{
// 如果链表为空,直接返回
if (head == NULL)
{
printf("List is empty.\n");
return NULL;
}
nodes *temp = head;
nodes *prev = NULL;
// delete head
if (temp != NULL && temp->book_data.book_id == bookID)
{
head = temp->next;
free(temp); // free
printf("Node with ID %d deleted successfully.\n", bookID);
return head;
}
// 遍历链表找到要删除的节点及其前一个节点
while (temp != NULL && temp->book_data.book_id != bookID)
{
prev = temp;
temp = temp->next;
}
// 如果节点不存在
if (temp == NULL)
{
printf("Node with ID %d not found.\n", bookID);
return head;
}
// 跳过要删除的节点
prev->next = temp->next;
free(temp); // 释放内存
printf("Node with ID %d deleted successfully.\n", bookID);
return head;
}
nodes *deleteNodeName(nodes *head, char title[])
{
// 如果链表为空,直接返回
if (head == NULL)
{
printf("List is empty.\n");
return NULL;
}
nodes *temp = head;
nodes *prev = NULL;
if (temp != NULL && strcmp(temp->book_data.title, title) == 0)
{
head = temp->next;
free(temp); // free
printf("Node with title %s deleted successfully.\n", title);
return head;
}
while (temp != NULL && strcmp(temp->book_data.title, title) != 0)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
{
printf("Node with title %s not found.\n", title);
return head;
}
// 跳过要删除的节点
prev->next = temp->next;
free(temp); // 释放内存
printf("Node with title %s deleted successfully.\n", title);
return head;
}
nodes *deleteNodeAuthor(nodes *head, char *author)
{
if (head == NULL)
{
printf("List is empty.\n");
return NULL;
}
nodes *temp = head;
nodes *prev = NULL;
if (temp != NULL && strcmp(temp->book_data.author, author) == 0)
{
head = temp->next;
free(temp); // free
printf("Node with title %s deleted successfully.\n", author);
return head;
}
while (temp != NULL && strcmp(temp->book_data.author, author) != 0)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
{
printf("Node with title %s not found.\n", author);
return head;
}
// 跳过要删除的节点
prev->next = temp->next;
free(temp); // 释放内存
printf("Node with title %s deleted successfully.\n", author);
return head;
}
void displayList(nodes *head)
{
nodes *current = head;
// 遍历链表并输出每个节点的信息
while (current != NULL)
{
printf("Book ID: %d\n", current->book_data.book_id);
printf("Title: %s\n", current->book_data.title);
printf("Author: %s\n", current->book_data.author);
printf("Is lend: %s\n", current->book_data.this_lend ? "yes" : "no");
printf("-------------\n");
if (current->book_data.this_lend)
{
printf("Lend data:\n");
printf("Name: %s, Date: %d-%d-%d, ", current->book_data.lender.name, current->book_data.lender.lend_time.tm_year, current->book_data.lender.lend_time.tm_mon, current->book_data.lender.lend_time.tm_mday);
printf("Time: %d:%d:%d", current->book_data.lender.lend_time.tm_hour, current->book_data.lender.lend_time.tm_min, current->book_data.lender.lend_time.tm_sec);
}
printf("\n");
// 移动到下一个节点
current = current->next;
}
}
void displayTitle(nodes *head, char *title)
{
nodes *current = head;
while (current != NULL)
{
if (strcmp(current->book_data.title, title) == 0)
{
printf("Book ID: %d\n", current->book_data.book_id);
printf("Title: %s\n", current->book_data.title);
printf("Author: %s\n", current->book_data.author);
printf("Is lend: %d", current->book_data.this_lend);
printf("\n");
}
// 移动到下一个节点
current = current->next;
}
}
void displayAuthor(nodes *head, char *author)
{
nodes *current = head;
while (current != NULL)
{
if (strcmp(current->book_data.author, author) == 0)
{
printf("Book ID: %d\n", current->book_data.book_id);
printf("Title: %s\n", current->book_data.title);
printf("Author: %s\n", current->book_data.author);
printf("Is lend: %d", current->book_data.this_lend);
printf("\n");
}
// 移动到下一个节点
current = current->next;
}
}
void displayID(nodes *head, int id)
{
nodes *current = head;
while (current != NULL)
{
if (current->book_data.book_id == id)
{
printf("Book ID: %d\n", current->book_data.book_id);
printf("Title: %s\n", current->book_data.title);
printf("Author: %s\n", current->book_data.author);
printf("Is lend: %d", current->book_data.this_lend);
printf("\n");
}
// 移动到下一个节点
current = current->next;
}
}
void changeLendStatus(nodes *head, int id, char *name, char *date, char *time)
{
nodes *current = head;
while (current != NULL)
{
if (current->book_data.book_id == id)
{
current->book_data.this_lend = 1;
strcpy(current->book_data.lender.name, name);
sscanf(date, "%d-%d-%d", ¤t->book_data.lender.lend_time.tm_year, ¤t->book_data.lender.lend_time.tm_mon, ¤t->book_data.lender.lend_time.tm_mday);
sscanf(time, "%d:%d:%d", ¤t->book_data.lender.lend_time.tm_hour, ¤t->book_data.lender.lend_time.tm_min, ¤t->book_data.lender.lend_time.tm_sec);
}
current = current->next;
}
}