目录
---------------------------------------------------------------------------------------------------------------------------------
1.补充上节插入节点的第三种方法(按序插入)
图示说明需求原理:
代码实现:
//链表插入之 按序插入
STU_NODE *insertLink(STU_NODE *head,STU_NODE tmp){
//从堆区申请带插入的节点空间
STU_NODE *pi = new STU_NODE();
//给空间赋值
*pi = tmp;
pi->next = nullptr;
//判断链表这个结点为不为空
if(nullptr == head){ //不存在
head = pi;
}else{//链表存在
//寻找插入点
STU_NODE *pf=head, *pb = head;
while((pb->num < pi->num)&& (pb->next != NULL))
{
//pf保存pb的位置
pf = pb;//pb移动到下一个节点
pb = pb->next;
}
//判断插入点的位置
if(pb->num>=pi->num)//头部、中部插入
{
if(pb == head)
{//头部
pi->next = head;
head = pi;
}
else{//尾部
pf->next = pi;
pi->next = pb;
}
}else{//尾部插入
pb->next = pi;
}
}
return head;
}
实际效果:
2.查询链表节点
1.方法调用
2.搜索函数实现
STU_NODE *searchLink(STU_NODE *head,char *name){
//判断链表是否存在
//判断链表这个结点为不为空
if(nullptr == head){ //不存在
cout<<"Link is not exist"<<endl;
return nullptr;
}
//逐个节点查询
STU_NODE *pnode = head;
while((strcpy_s(pnode->name,name) != 0) && (pnode->next != nullptr))
pnode = pnode->next;
if(strcpy_s(pnode->name,name) == 0){
return pnode;
}else{
cout<<"未找到相关节点"<<endl;
}
}
3.搜索功能结果展示测试
3.删除链表
1.图示删除链表的原理
2.函数调用
3.代码实现删除函数
STU_NODE *deleteLink(STU_NODE *head,int num){
//判断链表这个结点为不为空
if(nullptr == head){ //不存在
cout<<"Link is not exist"<<endl;
return nullptr;
}
//逐个节点比较,寻找删除点
STU_NODE *pf=head, *pb = head;
while((pb->num != num) && (pb->next != nullptr)){
pf = pb;
pb = pb->next;
}
//找到删除点
if(pb->num == num){
if(pb == head) //头结点删除
{
head = head->next;
}else{//中尾部删除
pf->next = pb->next;
}
delete pb;
}else{
cout<<"未找到要删除的节点:"<<endl;
}
return head;
}
4.结果展示
4.释放整个链表
1.函数调用
2.释放链表的函数实现
STU_NODE *freeLink(STU_NODE *head)
{
//判断链表这个结点为不为空
if(nullptr == head){ //不存在
cout<<"Link is not exist"<<endl;
return nullptr;
}
//逐个节点遍历删除
STU_NODE *pb = head;
while (pb != nullptr) {
head = head->next;
delete pb;
pb = head;
}
return head;
}
3.结果展示
5.main函数最终所有代码
#include <iostream>
#include "link.h"
#include <string.h>
using namespace std;
STU_NODE *head = nullptr;
int main()
{
helpMessage();
while(1){
char cmd[64] = "";
cout<<"请输入操作指令: ";
cin >> cmd;
if(strcmp(cmd,"help") == 0){
helpMessage();
}else if(strcmp(cmd,"insert") == 0){
cout << "-------insert-------"<<endl;
cout<<"请输入要插入的节点信息(num name):";
STU_NODE tmp;
cin>>tmp.num>>tmp.name;
//往哪个链表插,此时需要一个指向链表的头结点指针
head = insertLink(head,tmp);
}else if(strcmp(cmd,"print") == 0){
//遍历链表
printLink(head);
}else if(strcmp(cmd,"search") == 0){
cout << "-------search-------"<<endl;
cout<<"请输入查询的姓名:";
char name[32]="";
cin>>name;
STU_NODE *ret = nullptr;
ret = searchLink(head,name);
if(ret != nullptr){
cout<<"查询的结构:num="<<ret->num<<", name="<<ret->name<<endl;
}
}else if(strcmp(cmd,"delete") == 0){
cout << "-------delete-------"<<endl;
cout << "请输入要删除的学号num: ";
int num = 0;
cin>>num;
head = deleteLink(head,num);
}else if(strcmp(cmd,"free") == 0){
cout << "-------free-------"<<endl;
head = freeLink(head);
}else if(strcmp(cmd,"clear") == 0){
system("cls");
}else if(strcmp(cmd,"quit") == 0){
head = freeLink(head);
return 0;
}
}
return 0;
}