行业知识
用C语言链表实现图书管理
2026-11-30 19:04:00
来源:互联网转载
#include #include #include struct ListNode { int val;//编号 char title[50];//书名 float price;//价格 struct ListNode* next;};// 在尾部插入节点struct ListNode* insertAtTail(struct ListNode* head, int val,char mytitle[50],float price) { struct ListNode* new_node = (struct ListNode*)malloc(sizeof(struct ListNode)); new_node->val = val; strcpy(new_node->title,mytitle); new_node->price=price; new_node->next = NULL; if (head == NULL) { return new_node; } struct ListNode* p = head; while (p->next != NULL) { p = p->next; } p->next = new_node; return head;}void listAll(struct ListNode* head){ printf("编号\t书名\t价格\n"); struct ListNode* p=head; while(p!=NULL){ printf("%d\t%s\t%f", p->val, p->title, p->price); printf("\n"); p=p->next; }}// 删除指定值的节点struct ListNode* deleteNode(struct ListNode* head, int val) { struct ListNode* p = head; struct ListNode* prev = NULL; while (p != NULL && p->val != val) { prev = p; p = p->next; } if (p == NULL) { return head; } if (prev == NULL) { head = head->next; } else { prev->next = p->next; } free(p); return head;}// 修改指定值的节点的值struct ListNode* modifyNode(struct ListNode* head, int old_val,char newTitle[50],float newPrice) { struct ListNode* p = head; while (p != NULL && p->val != old_val) { p = p->next; } if (p == NULL) { return head; } strcpy(p->title ,newTitle); p->price = newPrice; return head;}// 查找指定值的节点的位置int getIndexByVal(struct ListNode* head, int val) { struct ListNode* p = head; int i = 0; while (p != NULL && p->val != val) { p = p->next; i++; } if (p == NULL) { return -1; } return i;}int main(){ struct ListNode* p=NULL; for (int i = 0; i 要改成班级管理,ListNode中val不变,其它属性变成学生姓名、成绩等等。
()注意:c语言的编译不要用visual studio,因为它连scanf都会报错,会要求您使用scanf_s。建议用记事本或visual studio code编写,然后在命令行窗口输入gcc命令来编译。
()
下一篇:吃鸡怎么更换服务器?