链表业务逻辑与辅助指针变量之间的关系
来源:互联网 发布:算法导论 原版 编辑:程序博客网 时间:2024/06/06 12:49
结构体链表基础:链表是一种常见的重要的数据结构,它是动态的进行存储分配的一种结构
一.定义接口函数
typedef struct Node{
int data;
struct Node *next;
}SLIST;
//创建链表
SLIST *CreatSlist();
//打印链表
int Slist_Print(SLIST *pHead);
//在节点数值为x前插入节点y
int SList_NodeInster(SLIST *pHead,int x,int y);
//删除节点y
int SList_NodeDel(SLIST *pHead,int y);
//删除节点
int SList_NodeDestory(SLIST *pHead);
//链表的逆置
int SList_reverse(SLIST *pHead);
二.接口函数的实现
//创建链表
SLIST *CreatSlist();
//能熟练的 写出稳定的、稳健程序 1年工作经验
SLIST *Creat_SList()
{
//1 创建头结点并初始化
int data = 0;
SLIST *pHead = NULL, *pM = NULL, *pCur;
pHead = (SLIST *)malloc(sizeof(SLIST));
if (pHead == NULL)
{
return NULL;
}
pHead->data = 0;
pHead->next = NULL;
//2循环创建结点,结点数据域中的数值从键盘输入,
//以-1作为输入结束标志
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
//准备环境 让pCur指向pHead
pCur = pHead;
while(data != -1)
{
//不断的malloc新节点 并且数据域 赋值
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
SList_Destory(pHead);
return NULL;
}
pM->data = data;
pM->next = NULL;
//1新节点入链表
pCur->next = pM;
//2 当前结点下移(新结点变成当前结点)
pCur = pM; // (pCur = pCur->next)
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
}
return pHead;
}
//能熟练的 写出稳定的、稳健程序 2年工作经验
int Creat_SList2(SLIST **Head)
{
int ret = 0;
//1 创建头结点并初始化
int data = 0;
SLIST *pHead = NULL, *pM = NULL, *pCur = NULL;
pHead = (SLIST *)malloc(sizeof(SLIST));
if (pHead == NULL)
{
ret = -1;
printf("func Creat_SList() err:%d", ret);
return ret;
}
pHead->data = 0;
pHead->next = NULL;
//2循环创建结点,结点数据域中的数值从键盘输入,
//以-1作为输入结束标志
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
//准备环境 让pCur指向pHead
pCur = pHead;
while(data != -1)
{
//不断的malloc新节点 并且数据域 赋值
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
SList_Destory(pHead);
ret = -2;
printf("func Creat_SList() err:%d malloc err", ret);
return ret;
}
pM->data = data;
pM->next = NULL;
//1新节点入链表
pCur->next = pM;
//2 当前结点下移(新结点变成当前结点)
pCur = pM; // (pCur = pCur->next)
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
}
*Head = pHead;
return ret;
}
//打印链表
int Slist_Print(SLIST *pHead);
int SList_Print(SLIST *pHead){
SLIST *p = NULL;
if (pHead == NULL)
{
return -1;
}
//准备环境
p = pHead->next;
printf("\nBegin ");
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("End ");
return 0;
}
//在结点数值为x的前面插入y
int SList_NodeInsert(SLIST *pHead, int x, int y)
{
SLIST *pCur = NULL, *pPre = NULL, *pM= NULL;
if (pHead == NULL)
{
return -1;
}
//环境准备
pPre = pHead;
pCur = pHead->next;
//不断的malloc新节点 并且数据域 赋值
pM = (SLIST *)malloc(sizeof(SLIST));
pM->data = y;
pM->next = NULL;
while(pCur)
{
if (pCur->data == x)
{
break;
}
//让pPre下移
pPre = pCur;
//让当前节点下移
pCur = pCur->next;
}
//让新节点链接后继结点
pM->next = pPre->next; //(pCur)
//让前驱结点连接pM
pPre->next = pM;
return 0;
}
//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y)
{
SLIST *pCur = NULL, *pPre = NULL;
if (pHead == NULL)
{
return -1;
}
//环境准备
pPre = pHead;
pCur = pHead->next;
while(pCur)
{
if (pCur->data == y)
{
break;
}
//让pPre下移
pPre = pCur;
//让当前节点下移
pCur = pCur->next;
}
if (pCur == NULL)
{
printf("没有找到节点 y:%d", y);
return -2;
}
//执行操作
pPre->next = pCur->next;
free(pCur);
return 0;
}
//删除当前节点
int SList_Destory(SLIST *pHead)
{
//1、删除当前结点前,需要把后继结点位置缓存
SLIST *pTmp = NULL;
SLIST *pCur = pHead;
if (pHead == NULL)
{
return -1;
}
while(pCur)
{
//缓存后继结点位置
pTmp = pCur->next;
//删除当前结点
free(pCur);
//当前结点下移
pCur = pTmp;
}
return 0;
}
//链表逆置
int SList_revse(SLIST *pHead)
{
SLIST *p = NULL, *q = NULL, *t = NULL;
if (pHead == NULL)
{
return -1;
}
if (pHead->next == NULL || pHead->next->next == NULL)
{
return 0;
}
//准备环境
p = pHead->next;
q = pHead->next->next;
while(q != NULL)
{
//1、在逆置之前需要做一个缓存
t = q->next;
//2、开始逆置
q->next = p;
//3、让p后移
p = q;
//4、q后移
q = t;
}
//让第一个业务结点赋值null
pHead->next->next = NULL;
//让链表头指向最后一个节点
pHead->next = p;
return 0;
}
三.编写测试用例
void main()
{
int ret = 0;
SLIST *pHead = NULL;
//创建 并打印
pHead = Creat_SList();
ret = SList_Print(pHead);
//插入操作
ret = SList_NodeInsert(pHead, 20, 19);
ret = SList_Print(pHead);
ret = SList_NodeDel(pHead, 19);
ret = SList_Print(pHead);
ret = SList_revse(pHead);
ret = SList_Print(pHead);
ret = SList_Destory(pHead);
system("pause");
}
以上是链表的几种基本操作,重要的地方都做了注释。
- 链表业务逻辑与辅助指针变量之间的关系
- ssh框架与mvc及业务逻辑之间的关系
- 指针与变量之间的关系
- 领域逻辑与业务逻辑的关系
- 技术与业务之间的关系
- 变量指针和地址之间的关系
- 指针与变量的关系
- 业务用例, 业务用例场景与业务用例实现之间的关系
- 数组与指针之间的微妙关系
- 数组 与 指针 之间的关系 分析
- 指针*p与地址之间的关系
- 数组,指针与函数之间的关系
- ######02.【根据表关系 写实体】,【直接凭业务逻辑 和页面显示信息,写不出:体现实体之间的依赖关系 的属性。】
- 指针与数组,指针与函数之间的关系
- 指针与函数及指针与数组之间的关系
- C++ 指针,引用,普通变量之间的关系
- C++指向结构体变量的指针与链表结构的关系应用
- 指针与数组之间的关系与转换
- C++学习(64)
- 动作使用
- CentOS7.3下创建laravel5.4项目
- 以Get方式访问wcf方法
- Docker network命令
- 链表业务逻辑与辅助指针变量之间的关系
- 设置Linux系统的空闲等待时间TMOUT
- 详解Java中的时区类TimeZone的用法
- Node的定时器
- Kotlin开发中遇到的bug
- UIButton设置 textAlignment 属性的方法
- 调度器的定时器
- hibernate加载hibernate.cfg.xml文件
- H