From ad0f37d0692653aeb02b0b5d68de9d1de4982a1b Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Sat, 11 Jul 2020 20:07:21 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=BA=90=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/employee.cpp | 105 +++++++++++++++++++++++++++++++++++++++ code/employee.h | 42 ++++++++++++++++ code/main.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++++ code/repo.cpp | 105 +++++++++++++++++++++++++++++++++++++++ code/repo.h | 40 +++++++++++++++ 5 files changed, 415 insertions(+) create mode 100644 code/employee.cpp create mode 100644 code/employee.h create mode 100644 code/main.cpp create mode 100644 code/repo.cpp create mode 100644 code/repo.h diff --git a/code/employee.cpp b/code/employee.cpp new file mode 100644 index 0000000..d14d32f --- /dev/null +++ b/code/employee.cpp @@ -0,0 +1,105 @@ +#include +#include "employee.h" + +node::node() +{ + // 将结构体中所有成员都置为空,请记得在添加成员时修改本构造函数 + number = 0; + memset(str, 0, sizeof(str)); + pre = NULL; + next = NULL; +} + +// 创建并返回一个空的带头结点的双向链表 +list makeEmpty() +{ + list head = new node; + return head; +} + +// 精确查找,返回表中value的位置,若找不到则返回NULL +list findValue(list listHead, int value) +{ + list p = listHead->next; + while(p != NULL) + { + // 此处查找的是number,如果要查找其它内容,请记得修改代码 + if(p->number == value) + break; + p = p->next; + } + return p; +} + +// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL +list findKeyword(list listHead, char keyword[]) +{ + list p = listHead->next; + while(p != NULL) + { + // 此处匹配的是keyword,如果要匹配其它内容,请记得修改代码 + if(strstr(p->str, keyword) != NULL) // 判断keyword是不是p->str的子串 + break; + p = p->next; + } + return p; +} + +// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false +bool insertNode(list position) +{ + if(position == NULL) // position指向非法位置 + { + return false; + } + node *s = new node; + s->next = position->next; // 修改新增结点后继指针 + position->next = s; // 修改position的后继指针 + if(s->next != NULL) // s不是链表尾节点 + s->next->pre = s; // 修改新增结点下一结点的前驱指针 + s->pre = position; // 修改新增结点前驱指针 + + // 在这个函数里没有给新增的结点赋值 + // 请在调用完该函数之后,给新增结点的每一项挨个赋值 + // 新增结点的指针就是“该函数的参数->next” + + return true; +} + +// 将position指向的元素删除并返回true。若position指向非法位置,返回false +bool deleteNode(list position) +{ + // 第一,如果position指向非法位置(如NULL),此时返回false + // 第二,不能直接删除头结点,如需删除空链表请使用deleteLinkList函数 + if(position == NULL || position->pre == NULL) + { + return false; + } + position->pre->next = position->next; // 断开position与前一个的联系 + if(position->next != NULL) // position不是链表尾节点 + position->next->pre = position->pre; // 断开position与后一个的联系 + delete position; + position = NULL; + + return true; +} + +// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 +list addNode(list listHead) +{ + insertNode(listHead); + return listHead->next; +} + +// 释放链表所占空间(释放全部结点) +void deleteLinkList(list listHead) +{ + node *p = listHead; + while(p->next != NULL) // 依次释放链表结点所占空间 + { + p = p->next; + delete p->pre; + } + delete p; + return; +} diff --git a/code/employee.h b/code/employee.h new file mode 100644 index 0000000..2cba970 --- /dev/null +++ b/code/employee.h @@ -0,0 +1,42 @@ +#ifndef __EMPLOYEE_H__ +#define __EMPLOYEE_H__ + +// 标准头文件结构 + +// 员工信息结点的数据结构(双向链表) + +/* 请在使用时修改结构体的名字, + 添加相应的结构体成员,同时修改构造函数(在linklist.cpp中) */ + +struct node { + int number; // int型整数 + char str[30]; // 容量为30的字符数组 + struct node *pre; // 前趋指针 + struct node *next; // 后继指针 + node(); // 构造函数,用于新建结点时初始化内容 +}; + +typedef node *list; // list 就相当于 node* + +// 创建并返回一个空的带头结点的双向链表 +list makeEmpty(); + +// 精确查找,返回表中value的位置,若找不到则返回NULL +list findValue(list listHead, int value); + +// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL +list findKeyword(list listHead, char keyword[]); + +// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false +bool insertNode(list position); + +// 将position指向的元素删除并返回true。若position指向非法位置,返回false +bool deleteNode(list position); + +// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 +list addNode(list listHead); + +// 释放链表所占空间(释放全部结点) +void deleteLinkList(list listHead); + +#endif \ No newline at end of file diff --git a/code/main.cpp b/code/main.cpp new file mode 100644 index 0000000..06ee1fc --- /dev/null +++ b/code/main.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include "repo.h" +#include "employee.h" + +/* 此 cpp 文件用于测试链表实现是否正确, + 也可作为该链表的使用范例 */ + +int main() +{ + list head; // 链表头指针 + int x; // 暂存链表的number项 + char s[30]; // 暂存链表的str项 + node *p; // 暂存链表指针 + int n; + bool flag; + /* + 输入数据: +6 +12 aaa +2 bbb +4 aaa +87 DDD +10 aaa +2 FFF +4 +2 12 87 5 +4 +aaa +bbb +FFF +abc +4 +12 87 2 666 + */ + head = makeEmpty(); // 调用makeEmpty函数,新建空链表 + + + scanf("%d", &n); + while(n--) // 调用addNode函数,向链表中添加数据 + { + scanf("%d %s", &x, s); // 输入结点内容 + p = addNode(head); // 添加一个新结点 + p->number = x; // 给该结点赋值 + strcpy(p->str, s); + } + + + scanf("%d", &n); + while(n--) // 调用findValue函数,查找链表结点 + { + scanf("%d", &x); + p = findValue(head, x); + if(p == NULL) // 没找到 + printf("Error: 找不到 %d\n", x); + else // 找到了 + printf("Found: number=%d, str=%s\n", p->number, p->str); + } + + + scanf("%d", &n); + while(n--) // 调用findKeyword函数,查找链表结点 + { + scanf("%s", s); + p = head; + int count = 0; + while(p != NULL) // 开始从头到尾找 + { + p = findKeyword(p, s); + // 关键字匹配我们认为可能有多个结果,所以会接着往下找 + if(p == NULL) // 没找到 + { + if(count == 0) // 一个也没找到 + printf("Error: 找不到 %s\n", s); + break; + } + else // 找到了 + { + printf("Found: number=%d, str=%s\n", p->number, p->str); + count++; + } + } + printf("%s 共找到 %d 个\n", s, count); + } + + + scanf("%d", &n); + while(n--) // 调用deleteNode,删除p所指节点 + { + scanf("%d", &x); + p = findValue(head, x); + flag = deleteNode(p); // 可简写为flag = deleteNode(findValue(head, x)); + if(flag == false) + printf("Delete number=%d Error\n", x); + else + printf("Delete number=%d successed\n", x); + } + + + p = head; + while(p->next != NULL) + p = p->next; // 这里让p指向链表尾节点 + flag = insertNode(p); // 调用insertNode,在p之后插入一个节点 + if(flag == false) + printf("Insert Error\n"); + else + { + // 给新增结点赋值,新增结点的指针就是“该函数的参数->next” + p->next->number = 123; + strcpy(p->next->str, "new node"); + printf("Insert: number=%d, str=%s\n", p->next->number, p->next->str); + } + + + for(p = head->next; p != NULL; p = p->next) // 遍历链表,输出节点内容 + printf("number=%d,str=%s; ", p->number, p->str); + + + deleteLinkList(head); // 释放链表空间 + + return 0; +} \ No newline at end of file diff --git a/code/repo.cpp b/code/repo.cpp new file mode 100644 index 0000000..47e53ad --- /dev/null +++ b/code/repo.cpp @@ -0,0 +1,105 @@ +#include +#include "repo.h" + +node::node() +{ + // 将结构体中所有成员都置为空,请记得在添加成员时修改本构造函数 + number = 0; + memset(str, 0, sizeof(str)); + pre = NULL; + next = NULL; +} + +// 创建并返回一个空的带头结点的双向链表 +list makeEmpty() +{ + list head = new node; + return head; +} + +// 精确查找,返回表中value的位置,若找不到则返回NULL +list findValue(list listHead, int value) +{ + list p = listHead->next; + while(p != NULL) + { + // 此处查找的是number,如果要查找其它内容,请记得修改代码 + if(p->number == value) + break; + p = p->next; + } + return p; +} + +// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL +list findKeyword(list listHead, char keyword[]) +{ + list p = listHead->next; + while(p != NULL) + { + // 此处匹配的是keyword,如果要匹配其它内容,请记得修改代码 + if(strstr(p->str, keyword) != NULL) // 判断keyword是不是p->str的子串 + break; + p = p->next; + } + return p; +} + +// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false +bool insertNode(list position) +{ + if(position == NULL) // position指向非法位置 + { + return false; + } + node *s = new node; + s->next = position->next; // 修改新增结点后继指针 + position->next = s; // 修改position的后继指针 + if(s->next != NULL) // s不是链表尾节点 + s->next->pre = s; // 修改新增结点下一结点的前驱指针 + s->pre = position; // 修改新增结点前驱指针 + + // 在这个函数里没有给新增的结点赋值 + // 请在调用完该函数之后,给新增结点的每一项挨个赋值 + // 新增结点的指针就是“该函数的参数->next” + + return true; +} + +// 将position指向的元素删除并返回true。若position指向非法位置,返回false +bool deleteNode(list position) +{ + // 第一,如果position指向非法位置(如NULL),此时返回false + // 第二,不能直接删除头结点,如需删除空链表请使用deleteLinkList函数 + if(position == NULL || position->pre == NULL) + { + return false; + } + position->pre->next = position->next; // 断开position与前一个的联系 + if(position->next != NULL) // position不是链表尾节点 + position->next->pre = position->pre; // 断开position与后一个的联系 + delete position; + position = NULL; + + return true; +} + +// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 +list addNode(list listHead) +{ + insertNode(listHead); + return listHead->next; +} + +// 释放链表所占空间(释放全部结点) +void deleteLinkList(list listHead) +{ + node *p = listHead; + while(p->next != NULL) // 依次释放链表结点所占空间 + { + p = p->next; + delete p->pre; + } + delete p; + return; +} diff --git a/code/repo.h b/code/repo.h new file mode 100644 index 0000000..766b5d4 --- /dev/null +++ b/code/repo.h @@ -0,0 +1,40 @@ +#ifndef __REPO_H__ +#define __REPO_H__ + +// 标准头文件结构。在使用此模板时,请先修改文件名,以及上面的宏定义名 + +/* 双向链表的单个节点,请在使用时修改结构体的名字, + 添加相应的结构体成员,同时修改构造函数(在linklist.cpp中) */ + +struct node { + int number; // int型整数 + char str[30]; // 容量为30的字符数组 + struct node *pre; // 前趋指针 + struct node *next; // 后继指针 + node(); // 构造函数,用于新建结点时初始化内容 +}; + +typedef node *list; // list 就相当于 node* + +// 创建并返回一个空的带头结点的双向链表 +list makeEmpty(); + +// 精确查找,返回表中value的位置,若找不到则返回NULL +list findValue(list listHead, int value); + +// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL +list findKeyword(list listHead, char keyword[]); + +// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false +bool insertNode(list position); + +// 将position指向的元素删除并返回true。若position指向非法位置,返回false +bool deleteNode(list position); + +// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 +list addNode(list listHead); + +// 释放链表所占空间(释放全部结点) +void deleteLinkList(list listHead); + +#endif \ No newline at end of file -- Gitee From fd75cec06979fe84ae104c18e5ae636a10ec234e Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Mon, 13 Jul 2020 22:02:48 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20code?= =?UTF-8?q?/employee.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/employee.h | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 code/employee.h diff --git a/code/employee.h b/code/employee.h deleted file mode 100644 index 2cba970..0000000 --- a/code/employee.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef __EMPLOYEE_H__ -#define __EMPLOYEE_H__ - -// 标准头文件结构 - -// 员工信息结点的数据结构(双向链表) - -/* 请在使用时修改结构体的名字, - 添加相应的结构体成员,同时修改构造函数(在linklist.cpp中) */ - -struct node { - int number; // int型整数 - char str[30]; // 容量为30的字符数组 - struct node *pre; // 前趋指针 - struct node *next; // 后继指针 - node(); // 构造函数,用于新建结点时初始化内容 -}; - -typedef node *list; // list 就相当于 node* - -// 创建并返回一个空的带头结点的双向链表 -list makeEmpty(); - -// 精确查找,返回表中value的位置,若找不到则返回NULL -list findValue(list listHead, int value); - -// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL -list findKeyword(list listHead, char keyword[]); - -// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false -bool insertNode(list position); - -// 将position指向的元素删除并返回true。若position指向非法位置,返回false -bool deleteNode(list position); - -// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 -list addNode(list listHead); - -// 释放链表所占空间(释放全部结点) -void deleteLinkList(list listHead); - -#endif \ No newline at end of file -- Gitee From 61140aaf24b09e713869c6fd1c196228aceb33fc Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Mon, 13 Jul 2020 22:02:55 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20code?= =?UTF-8?q?/employee.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/employee.cpp | 105 ---------------------------------------------- 1 file changed, 105 deletions(-) delete mode 100644 code/employee.cpp diff --git a/code/employee.cpp b/code/employee.cpp deleted file mode 100644 index d14d32f..0000000 --- a/code/employee.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include "employee.h" - -node::node() -{ - // 将结构体中所有成员都置为空,请记得在添加成员时修改本构造函数 - number = 0; - memset(str, 0, sizeof(str)); - pre = NULL; - next = NULL; -} - -// 创建并返回一个空的带头结点的双向链表 -list makeEmpty() -{ - list head = new node; - return head; -} - -// 精确查找,返回表中value的位置,若找不到则返回NULL -list findValue(list listHead, int value) -{ - list p = listHead->next; - while(p != NULL) - { - // 此处查找的是number,如果要查找其它内容,请记得修改代码 - if(p->number == value) - break; - p = p->next; - } - return p; -} - -// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL -list findKeyword(list listHead, char keyword[]) -{ - list p = listHead->next; - while(p != NULL) - { - // 此处匹配的是keyword,如果要匹配其它内容,请记得修改代码 - if(strstr(p->str, keyword) != NULL) // 判断keyword是不是p->str的子串 - break; - p = p->next; - } - return p; -} - -// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false -bool insertNode(list position) -{ - if(position == NULL) // position指向非法位置 - { - return false; - } - node *s = new node; - s->next = position->next; // 修改新增结点后继指针 - position->next = s; // 修改position的后继指针 - if(s->next != NULL) // s不是链表尾节点 - s->next->pre = s; // 修改新增结点下一结点的前驱指针 - s->pre = position; // 修改新增结点前驱指针 - - // 在这个函数里没有给新增的结点赋值 - // 请在调用完该函数之后,给新增结点的每一项挨个赋值 - // 新增结点的指针就是“该函数的参数->next” - - return true; -} - -// 将position指向的元素删除并返回true。若position指向非法位置,返回false -bool deleteNode(list position) -{ - // 第一,如果position指向非法位置(如NULL),此时返回false - // 第二,不能直接删除头结点,如需删除空链表请使用deleteLinkList函数 - if(position == NULL || position->pre == NULL) - { - return false; - } - position->pre->next = position->next; // 断开position与前一个的联系 - if(position->next != NULL) // position不是链表尾节点 - position->next->pre = position->pre; // 断开position与后一个的联系 - delete position; - position = NULL; - - return true; -} - -// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 -list addNode(list listHead) -{ - insertNode(listHead); - return listHead->next; -} - -// 释放链表所占空间(释放全部结点) -void deleteLinkList(list listHead) -{ - node *p = listHead; - while(p->next != NULL) // 依次释放链表结点所占空间 - { - p = p->next; - delete p->pre; - } - delete p; - return; -} -- Gitee From 28a8cea0eece8397f09d22a06ce7d2e6585160a7 Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Mon, 13 Jul 2020 22:08:17 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20code?= =?UTF-8?q?/main.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/main.cpp | 123 -------------------------------------------------- 1 file changed, 123 deletions(-) delete mode 100644 code/main.cpp diff --git a/code/main.cpp b/code/main.cpp deleted file mode 100644 index 06ee1fc..0000000 --- a/code/main.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include -#include -#include -#include "repo.h" -#include "employee.h" - -/* 此 cpp 文件用于测试链表实现是否正确, - 也可作为该链表的使用范例 */ - -int main() -{ - list head; // 链表头指针 - int x; // 暂存链表的number项 - char s[30]; // 暂存链表的str项 - node *p; // 暂存链表指针 - int n; - bool flag; - /* - 输入数据: -6 -12 aaa -2 bbb -4 aaa -87 DDD -10 aaa -2 FFF -4 -2 12 87 5 -4 -aaa -bbb -FFF -abc -4 -12 87 2 666 - */ - head = makeEmpty(); // 调用makeEmpty函数,新建空链表 - - - scanf("%d", &n); - while(n--) // 调用addNode函数,向链表中添加数据 - { - scanf("%d %s", &x, s); // 输入结点内容 - p = addNode(head); // 添加一个新结点 - p->number = x; // 给该结点赋值 - strcpy(p->str, s); - } - - - scanf("%d", &n); - while(n--) // 调用findValue函数,查找链表结点 - { - scanf("%d", &x); - p = findValue(head, x); - if(p == NULL) // 没找到 - printf("Error: 找不到 %d\n", x); - else // 找到了 - printf("Found: number=%d, str=%s\n", p->number, p->str); - } - - - scanf("%d", &n); - while(n--) // 调用findKeyword函数,查找链表结点 - { - scanf("%s", s); - p = head; - int count = 0; - while(p != NULL) // 开始从头到尾找 - { - p = findKeyword(p, s); - // 关键字匹配我们认为可能有多个结果,所以会接着往下找 - if(p == NULL) // 没找到 - { - if(count == 0) // 一个也没找到 - printf("Error: 找不到 %s\n", s); - break; - } - else // 找到了 - { - printf("Found: number=%d, str=%s\n", p->number, p->str); - count++; - } - } - printf("%s 共找到 %d 个\n", s, count); - } - - - scanf("%d", &n); - while(n--) // 调用deleteNode,删除p所指节点 - { - scanf("%d", &x); - p = findValue(head, x); - flag = deleteNode(p); // 可简写为flag = deleteNode(findValue(head, x)); - if(flag == false) - printf("Delete number=%d Error\n", x); - else - printf("Delete number=%d successed\n", x); - } - - - p = head; - while(p->next != NULL) - p = p->next; // 这里让p指向链表尾节点 - flag = insertNode(p); // 调用insertNode,在p之后插入一个节点 - if(flag == false) - printf("Insert Error\n"); - else - { - // 给新增结点赋值,新增结点的指针就是“该函数的参数->next” - p->next->number = 123; - strcpy(p->next->str, "new node"); - printf("Insert: number=%d, str=%s\n", p->next->number, p->next->str); - } - - - for(p = head->next; p != NULL; p = p->next) // 遍历链表,输出节点内容 - printf("number=%d,str=%s; ", p->number, p->str); - - - deleteLinkList(head); // 释放链表空间 - - return 0; -} \ No newline at end of file -- Gitee From 7c58894403421b1a674af7fd486353a0d5404356 Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Mon, 13 Jul 2020 22:08:22 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20code?= =?UTF-8?q?/repo.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/repo.cpp | 105 -------------------------------------------------- 1 file changed, 105 deletions(-) delete mode 100644 code/repo.cpp diff --git a/code/repo.cpp b/code/repo.cpp deleted file mode 100644 index 47e53ad..0000000 --- a/code/repo.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include "repo.h" - -node::node() -{ - // 将结构体中所有成员都置为空,请记得在添加成员时修改本构造函数 - number = 0; - memset(str, 0, sizeof(str)); - pre = NULL; - next = NULL; -} - -// 创建并返回一个空的带头结点的双向链表 -list makeEmpty() -{ - list head = new node; - return head; -} - -// 精确查找,返回表中value的位置,若找不到则返回NULL -list findValue(list listHead, int value) -{ - list p = listHead->next; - while(p != NULL) - { - // 此处查找的是number,如果要查找其它内容,请记得修改代码 - if(p->number == value) - break; - p = p->next; - } - return p; -} - -// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL -list findKeyword(list listHead, char keyword[]) -{ - list p = listHead->next; - while(p != NULL) - { - // 此处匹配的是keyword,如果要匹配其它内容,请记得修改代码 - if(strstr(p->str, keyword) != NULL) // 判断keyword是不是p->str的子串 - break; - p = p->next; - } - return p; -} - -// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false -bool insertNode(list position) -{ - if(position == NULL) // position指向非法位置 - { - return false; - } - node *s = new node; - s->next = position->next; // 修改新增结点后继指针 - position->next = s; // 修改position的后继指针 - if(s->next != NULL) // s不是链表尾节点 - s->next->pre = s; // 修改新增结点下一结点的前驱指针 - s->pre = position; // 修改新增结点前驱指针 - - // 在这个函数里没有给新增的结点赋值 - // 请在调用完该函数之后,给新增结点的每一项挨个赋值 - // 新增结点的指针就是“该函数的参数->next” - - return true; -} - -// 将position指向的元素删除并返回true。若position指向非法位置,返回false -bool deleteNode(list position) -{ - // 第一,如果position指向非法位置(如NULL),此时返回false - // 第二,不能直接删除头结点,如需删除空链表请使用deleteLinkList函数 - if(position == NULL || position->pre == NULL) - { - return false; - } - position->pre->next = position->next; // 断开position与前一个的联系 - if(position->next != NULL) // position不是链表尾节点 - position->next->pre = position->pre; // 断开position与后一个的联系 - delete position; - position = NULL; - - return true; -} - -// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 -list addNode(list listHead) -{ - insertNode(listHead); - return listHead->next; -} - -// 释放链表所占空间(释放全部结点) -void deleteLinkList(list listHead) -{ - node *p = listHead; - while(p->next != NULL) // 依次释放链表结点所占空间 - { - p = p->next; - delete p->pre; - } - delete p; - return; -} -- Gitee From 3b2408a12567680f491fbe92af42f687b680d426 Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Mon, 13 Jul 2020 22:08:27 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20code?= =?UTF-8?q?/repo.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/repo.h | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 code/repo.h diff --git a/code/repo.h b/code/repo.h deleted file mode 100644 index 766b5d4..0000000 --- a/code/repo.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef __REPO_H__ -#define __REPO_H__ - -// 标准头文件结构。在使用此模板时,请先修改文件名,以及上面的宏定义名 - -/* 双向链表的单个节点,请在使用时修改结构体的名字, - 添加相应的结构体成员,同时修改构造函数(在linklist.cpp中) */ - -struct node { - int number; // int型整数 - char str[30]; // 容量为30的字符数组 - struct node *pre; // 前趋指针 - struct node *next; // 后继指针 - node(); // 构造函数,用于新建结点时初始化内容 -}; - -typedef node *list; // list 就相当于 node* - -// 创建并返回一个空的带头结点的双向链表 -list makeEmpty(); - -// 精确查找,返回表中value的位置,若找不到则返回NULL -list findValue(list listHead, int value); - -// 关键字匹配,返回表中含keyword关键字的第一个位置,若找不到则返回NULL -list findKeyword(list listHead, char keyword[]); - -// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false -bool insertNode(list position); - -// 将position指向的元素删除并返回true。若position指向非法位置,返回false -bool deleteNode(list position); - -// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 -list addNode(list listHead); - -// 释放链表所占空间(释放全部结点) -void deleteLinkList(list listHead); - -#endif \ No newline at end of file -- Gitee From 7f6a0affc918d82377f6d9ca1021e0ba82011d05 Mon Sep 17 00:00:00 2001 From: hyp19991114 Date: Mon, 13 Jul 2020 22:08:45 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=91=98=E5=B7=A5?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E3=80=81=E5=BA=93=E5=AD=98=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=9A=84=E6=BA=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/date.cpp | 85 ++++++++++++++++++++++++++ code/date.h | 19 ++++++ code/employee.cpp | 149 ++++++++++++++++++++++++++++++++++++++++++++++ code/employee.h | 48 +++++++++++++++ code/repo.cpp | 7 +++ code/repo.h | 8 +++ 6 files changed, 316 insertions(+) create mode 100644 code/date.cpp create mode 100644 code/date.h create mode 100644 code/employee.cpp create mode 100644 code/employee.h create mode 100644 code/repo.cpp create mode 100644 code/repo.h diff --git a/code/date.cpp b/code/date.cpp new file mode 100644 index 0000000..0ebd7f3 --- /dev/null +++ b/code/date.cpp @@ -0,0 +1,85 @@ +#include "date.h" + +date::date() :year(0), month(0), day(0) {} + +date::date(int y, int m, int d) : + year(y), month(m), day(d) {} + +// 拷贝构造函数 +date::date(const date &_date) : + year(_date.year), month(_date.month), day(_date.day) {} + +// 重载大于号,判断日期a是否大于日期b +/*********************************************** + * addDate -- 返回日期a + 天数b的结果 + * 参数:d:日期结构体(年月日) + * days:要加的天数 + * 返回值:计算结果,类型为日期(年月日) + ***********************************************/ +bool operator>(const date &a, const date &b) +{ + if(a.year != b.year) + return a.year > b.year; + if(a.month != b.month) + return a.month > b.month; + return a.day > b.day; +} + +/*********************************************** + * addDate -- 返回日期a + 天数b的结果 + * 参数:d:日期结构体(年月日) + * days:要加的天数 + * 返回值:计算结果,类型为日期(年月日) + ***********************************************/ +date addDay(date d, int days) +{ + for(int i = days; i != 0; --i) // 模拟每一天的增加 + { + if(d.day == 31) // 对31日的处理 + { + if(d.month != 12) // 不是12月31日 + { + d.month++; + d.day = 1; + } + else // 是12月31日 + { + d.year++; + d.month = 1; + d.day = 1; + } + continue; + } + if(d.day == 30) // 对30日的处理 + { + // 4、6、9、11月只有30天,下一天是新的月份 + if(d.month == 4 || d.month == 6 || d.month == 9 || d.month == 11) + { + d.month++; + d.day = 1; + } + else // 不用更改月份,直接下一天 + d.day++; + continue; + } + if(d.day == 29 && d.month == 2) // 闰年的2月最后一天 + { + d.month++; + d.day = 1; + continue; + } + if(d.day == 28 && d.month == 2) // 2月28日 + { + if(((d.year % 4 == 0) && (d.year % 100 != 0)) || (d.year % 400 == 0)) + d.day++; // 如果是闰年,日期+1 + else + { + d.month++; // 如果是平年,则是下个月的第一天 + d.day = 1; + } + continue; + } + d.day++; // 其他情况,直接下一天 + } + return d; +} diff --git a/code/date.h b/code/date.h new file mode 100644 index 0000000..afc0acd --- /dev/null +++ b/code/date.h @@ -0,0 +1,19 @@ +#ifndef __DATE_H__ +#define __DATE_H__ + +struct date +{ + int year; // 年 + int month; // 月 + int day; // 日 + date(); // 默认构造函数,年月日均初始化为0 + date(int y, int m, int d); // 根据参数 y,m,d 初始化年月日 + date(const date &_date); // 拷贝构造函数 + friend bool operator>(const date &a, const date &b); + // 重载大于号,判断日期a是否大于日期b +}; + +// 返回日期a + 天数b的结果 +date addDay(date d, int days); + +#endif // !__DATE_H__ diff --git a/code/employee.cpp b/code/employee.cpp new file mode 100644 index 0000000..0fc8626 --- /dev/null +++ b/code/employee.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include "employee.h" + +employee::employee() +{ + // 将结构体中所有成员都置为空 + id = 0; + memset(name, 0, sizeof(name)); + memset(sex, 0, sizeof(sex)); + memset(phone, 0, sizeof(phone)); + basic_salary = 0; + absentDays = 0; + memset(department, 0, sizeof(department)); + memset(job, 0, sizeof(job)); + pre = NULL; + next = NULL; +} + +// 创建并返回一个空的带头结点的双向链表 +employee_list employee_makeEmpty() +{ + employee_list head = new employee; + return head; +} + +// 工号的精确查找,返回表中id的位置,若找不到则返回NULL +employee_list employee_findId(employee_list listHead, int value) +{ + employee_list p = listHead->next; + while(p != NULL) + { + if(p->id == value) + break; + p = p->next; + } + return p; +} + +// 姓名的精确查找,返回表中name的位置,若找不到则返回NULL +employee_list employee_findName(employee_list listHead, char value[]) +{ + employee_list p = listHead->next; + while(p != NULL) + { + if(strcmp(p->name, value) == 0) + break; + p = p->next; + } + return p; +} + +// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false +bool employee_insertNode(employee_list position) +{ + if(position == NULL) // position指向非法位置 + { + return false; + } + employee *s = new employee; + s->next = position->next; // 修改新增结点后继指针 + position->next = s; // 修改position的后继指针 + if(s->next != NULL) // s不是链表尾节点 + s->next->pre = s; // 修改新增结点下一结点的前驱指针 + s->pre = position; // 修改新增结点前驱指针 + + // 在这个函数里没有给新增的结点赋值 + // 请在调用完该函数之后,给新增结点的每一项挨个赋值 + // 新增结点的指针就是“该函数的参数->next” + + return true; +} + +// 将position指向的元素删除并返回true。若position指向非法位置,返回false +bool employee_deleteNode(employee_list position) +{ + // 第一,如果position指向非法位置(如NULL),此时返回false + // 第二,不能直接删除头结点,如需删除空链表请使用deleteLinkList函数 + if(position == NULL || position->pre == NULL) + { + return false; + } + position->pre->next = position->next; // 断开position与前一个的联系 + if(position->next != NULL) // position不是链表尾节点 + position->next->pre = position->pre; // 断开position与后一个的联系 + delete position; + position = NULL; + + return true; +} + +// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 +employee_list employee_addNode(employee_list listHead) +{ + employee_insertNode(listHead); + return listHead->next; +} + +// 释放链表所占空间(释放全部结点) +void employee_deleteLinkList(employee_list listHead) +{ + employee *p = listHead; + while(p->next != NULL) // 依次释放链表结点所占空间 + { + p = p->next; + delete p->pre; + } + delete p; + return; +} + +// 从文件读入已有信息 +bool employee_inputFromFile(employee_list listHead) +{ + puts("请输入文件名或文件的地址,以回车结束。注意:地址中的'\\'应写作\"\\\\\"。"); + char fileName[200]; // 文件名或地址 + scanf("%[^\n]", fileName); + fflush(stdin); + FILE *fp = fopen(fileName, "r"); + if(fp == NULL) // 未打开成功 + { + puts("无法打开指定的文件,请重试。"); + system("pause"); + return false; + } + char temp[500]; // 假定一条数据的最大长度为500个字符 + int cnt = 0; // 记录读入数据条数 + while(fgets(temp, 500, fp) != NULL) + { + employee *s = employee_addNode(listHead); + sscanf(temp, "%d\t%s\t%s\t%s\t%d\t%d\t%s\t%s", + &s->id, s->name, s->sex, s->phone, + &s->basic_salary, &s->absentDays, + s->department, s->job); + cnt++; + } + if(cnt != 0) + { + printf("成功读入 %d 条数据。", cnt); + } + else + { + printf("未能成功读取数据,请检查文件 %s 是否为空。\n", fileName); + } + fclose(fp); + system("pause"); + return true; +} \ No newline at end of file diff --git a/code/employee.h b/code/employee.h new file mode 100644 index 0000000..4cab13f --- /dev/null +++ b/code/employee.h @@ -0,0 +1,48 @@ +#ifndef __EMPLOYEE_H__ +#define __EMPLOYEE_H__ + +// 标准头文件结构 + +// 员工信息结点的数据结构(双向链表) +struct employee { + int id; // 工号 + char name[10]; // 姓名 + char sex[5]; // 性别 + char phone[15]; // 电话 + int basic_salary; // 基本工资 + int absentDays; // 缺勤的天数 + char department[30]; // 部门 + char job[30]; // 职务 + struct employee *pre; // 前趋指针 + struct employee *next; // 后继指针 + employee(); // 构造函数,用于新建结点时初始化内容 +}; + +typedef employee *employee_list; +// employee_list 就相当于 employee* + +// 创建并返回一个空的带头结点的双向链表 +employee_list employee_makeEmpty(); + +// 工号的精确查找,返回表中id的位置,若找不到则返回NULL +employee_list employee_findId(employee_list listHead, int value); + +// 姓名的精确查找,返回表中name的位置,若找不到则返回NULL +employee_list employee_findName(employee_list listHead, char value[]); + +// 在position指向的结点之后插入一个结点,返回true。如果position指向非法位置,返回false +bool employee_insertNode(employee_list position); + +// 将position指向的元素删除并返回true。若position指向非法位置,返回false +bool employee_deleteNode(employee_list position); + +// 使用头插法,在链表头部新增一个结点,返回新增结点的指针 +employee_list employee_addNode(employee_list listHead); + +// 释放链表所占空间(释放全部结点) +void employee_deleteLinkList(employee_list listHead); + +// 从文件读入已有信息 +bool employee_inputFromFile(employee_list listHead); + +#endif diff --git a/code/repo.cpp b/code/repo.cpp new file mode 100644 index 0000000..9cf68bd --- /dev/null +++ b/code/repo.cpp @@ -0,0 +1,7 @@ +#include +#include "repo.h" + +// 查询商品信息结构体中是否有剩余货 + +// 卖出货后减去相应数量 + diff --git a/code/repo.h b/code/repo.h new file mode 100644 index 0000000..a507af6 --- /dev/null +++ b/code/repo.h @@ -0,0 +1,8 @@ +#ifndef __REPO_H__ +#define __REPO_H__ + +// 标准头文件结构。 + + + +#endif -- Gitee