当前位置: 首页 > news >正文

深圳建设局官网站松原新闻头条

深圳建设局官网站,松原新闻头条,黑龙江建设网网站一体化平台,网站建设公司代理商目录一、思路>>>>>>>>>>>>过程<<<<<<<<<<<<<<<1.打印2.尾插3.尾删4.头插5.头删6.查找7.指定位置后插入8.指定位置后删除9.链表的销毁二、整个程序1.SLTlist.c2.SLTlist.c一、思路 #define …

目录

  • 一、思路
  • >>>>>>>>>>>>过程<<<<<<<<<<<<<<<
  • 1.打印
  • 2.尾插
  • 3.尾删
  • 4.头插
  • 5.头删
  • 6.查找
  • 7.指定位置后插入
  • 8.指定位置后删除
  • 9.链表的销毁
  • 二、整个程序
  • 1.SLTlist.c
  • 2.SLTlist.c

一、思路

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int SLTdataType;typedef struct SLTNodelist
{SLTdataType data;struct SLTNodelist* next;
}SLTNode;//打印
void SLTPrint(SLTNode* phead);
//尾插
void SLTAddBack(SLTNode** pphead, SLTdataType x);
//尾删
void SLTDelBack(SLTNode** pphead);
//头插
void SLTAddPop(SLTNode** pphead, SLTdataType x);
//头删
void SLTDelPop(SLTNode** pphead);
//查找
SLTNode* SLTFindlist(SLTNode* phead, SLTdataType x);
//指定位置后插入
void SLTAddPoint(SLTNode** pphead, SLTNode* pos, SLTdataType x);
//指定位置后删除
void SLDelPoint(SLTNode** pphead, SLTNode* pos);
//链表的销毁
void SLTDestroy(SLTNode** pphead);

>>>>>>>>>>>>过程<<<<<<<<<<<<<<<

1.打印

void PrintfSTLlist(SLTNode*phead)
{STLNode*cur=phead;while(cur){printf("%d->\n",cur->Data);cur=cur->next;}printf("NULL\n");}

1.这个打印函数需要断言吗?
不需要,即使结构体为空,也能打印,只不过是没有数据而已,这是打印出来的就是空的。如果能打印出来,但是却断言报错,不太合适。

2.怎么打印?
用一个指针cur指向结构体,用while循环打印出来,当cur指向的结构体为空时,停止打印。

3.while的判断条件可以是while(cur->next)吗?
不可以,因为这样最后一个的数据就打印不出来了。

4.在while循环中,让cur指向下一个结构体,可以用cur++吗?
不可以,不同于顺序表,顺序表的数据是存储在一个连续的空间里的,链表它是链接起来的结构体地址。

2.尾插

SLTNode*Buynewnode(SLTDataTap x)
{SLTNode*newnode=(SLTNode*)malloc(sizeof(SLTNode));if(newnode==NULL){perror("malloc failed");return NULL;}newnode->Data=x;newnode->next=NULL;return newnode;
}
void SLTAddPop(SLTNode**pphead,SLTDataTap x)
{//开辟空间SLTNode*newnode=Buynewnode(x);if(newnode==NULL){return;}//找尾//情况一:pphead为空if(*pphead==NULL){*pphead=newnode;}//情况二:pphead不为空SLTNode*tail=pphead;else{while(tail->next){tail=tail->next;}tail->next=newnode;}
}

1.为什么void SLTAddPop(SLTNode**pphead,SLTDataTap)这不能用一级指针接收?

2.怎么进行尾插?
在插入一个数据之前,首先得为这个结构体开辟一个结点,用malloc开辟,由于插入数据时我们都要进行开辟一个结的操作,所以我们可以打包成一个函数SLTNode*Buynewnode(SLTDataTap x)
进行尾插那么就是要找到尾,找尾分两种情况:1. 当*pphead本身为空时,就直接将newnode插入就可以了;2. *pphead本身不为空时,只要找到tail->next为空的,那个就是结构体的尾了
在这里插入图片描述

3.当pphead不为空时,找尾while循环的判断条件可以写成这样tail!=NULL与插入结点时tail=newnode吗?
不可以,因为这样就无法保持链接状态了。

3.尾删

void SLTDelBack(SLTNode**pphead)
{assert(*pphead);//情况一:只有一个数据if((*pphead)->next==NULL){free(*pphead);*pphead=NULL;}//情况二:不止一个数据SLTNode*tail=*pphead;SLTNode*pre=*pphead;while(tail->next){pre=tail;tail=tail->next;}free(tail);tail=NULL;pre->next=NULL;
}

1.尾删需要断言吗?
需要,因为如果链表为空,是删不了的;
2.尾删的思路
尾删分三种讨论:

1.如果链表为空,删不了,我们这里断言判断一下
2.链表中只有一个数据
3.链表中的数据为一个以上

4.头插

void SLTAddPop(SLTNode** pphead, SLTdataType x)
{assert(pphead);SLTNode* newnode = Buynewnode(x);if (newnode == NULL){return;}newnode->next = *pphead;*pphead = newnode;
}

1.头插需要断言吗?
因为pphead永远不能为空,所以要断言;但是当链表为空的时候,可以插入数据,*pphead是不需要断言的。
2.头插的思路
首先先要创建一个结点,将结点的next与链表的第一个指针链接起来。最后要注意把链表的头给改成newnode。

5.头删

void SLTDelPop(SLTNode** pphead)
{assert(pphead);assert(*pphead);SLTNode* cur = *pphead;*pphead = cur->next;free(cur);cur = NULL;
}

1.头删需要断言吗?
因为pphead永远不能为空,所以要断言;且空链表不能删除,所以*pphead也需要断言。
2.头删的思路
在这里插入图片描述

6.查找

SLTNode* SLTFindlist(SLTNode* phead, SLTdataType x)
{SLTNode* cur = phead;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}

1.查找需要断言吗?
不需要,链表为空就返回NULL;
2.查找的思路
当链表的cur不为空,就继续逐一比对,找到了就返回cur,没找到就指向下一个;直到cur指向空,如果还没找到,就返回NULL;

7.指定位置后插入

void SLTAddPoint(SLTNode** pphead, SLTNode* pos, SLTdataType x)
{assert(pos);assert(pphead);SLTNode* newnode = Buynewnode(x);if (newnode == NULL){return;}//newnode->next = pos->next;pos->next = newnode;}

1.需要断言吗?
因为pphead永远不能为空,所以要断言;指定的位置pos不能为空,所以需要断言;
2.思路
创建一个新结点newnode,然后先让newnode->next = pos->next;让newnode的后面链接起来,在让pos和newnode链接起来pos->next = newnode;;反过来写的话,由于pos->next已经被改了,所以不能是pos与newnode链接,插入就会失败;

8.指定位置后删除

void SLDelPoint(SLTNode** pphead, SLTNode* pos)
{assert(pos);assert(pphead);assert(*pphead);if ((*pphead)->next == NULL&&pos->next==NULL){return;}else{SLTNode* cur = pos->next;pos->next = cur->next;free(cur);cur = NULL;}
}

1.需要断言吗?
因为pphead永远不能为空,所以要断言;因为如果链表为空,是删不了的,所以*phead需要断言,指定的位置pos不能为空,所以需要断言;

9.链表的销毁

void SLTDestroy(SLTNode** pphead)
{assert(pphead);assert(*pphead);SLTNode* cur = *pphead;SLTNode* pre = cur;while (cur){pre = cur->next;free(cur);cur = pre;}*pphead = cur;
}

2.思路
结点逐一free,最后记得把*pphead改为最后的cur。

二、整个程序

1.SLTlist.c

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int SLTdataType;typedef struct SLTNodelist
{SLTdataType data;struct SLTNodelist* next;
}SLTNode;//打印
void SLTPrint(SLTNode* phead);
//尾插
void SLTAddBack(SLTNode** pphead, SLTdataType x);
//尾删
void SLTDelBack(SLTNode** pphead);
//头插
void SLTAddPop(SLTNode** pphead, SLTdataType x);
//头删
void SLTDelPop(SLTNode** pphead);
//查找
SLTNode* SLTFindlist(SLTNode* phead, SLTdataType x);
//指定位置后插入
void SLTAddPoint(SLTNode** pphead, SLTNode* pos, SLTdataType x);
//指定位置后删除
void SLDelPoint(SLTNode** pphead, SLTNode* pos);
//链表的销毁
void SLTDestroy(SLTNode** pphead);

2.SLTlist.c

#define _CRT_SECURE_NO_WARNINGS 1#include"SLTlist.h"void SLTPrint(SLTNode* phead)
{SLTNode* cur = phead;while (cur){printf("%d->", cur->data);cur = cur->next;}printf("NULL\n");
}SLTNode* Buynewnode(SLTdataType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));if (newnode == NULL){return NULL;}newnode->data = x;newnode->next = NULL;return newnode;
}void SLTAddBack(SLTNode** pphead, SLTdataType x)
{assert(pphead);SLTNode* newnode = Buynewnode(x);if (newnode == NULL){return;}//情况1:pphead为空if (*pphead == NULL){*pphead = newnode;}//情况2:pphead不为空//找尾else{SLTNode* tail = *pphead;while (tail->next){tail = tail->next;}tail->next = newnode;}
}void SLTDelBack(SLTNode** pphead)
{assert(pphead);assert(*pphead);//情况1:链表中只有一个节点if ((*pphead)->next == NULL){free(*pphead);*pphead = NULL;}//情况2:链表中只有一个节点else{SLTNode* tail = *pphead;while (tail->next->next){tail = tail->next;}SLTNode* pre = tail->next;free(pre);tail->next = NULL;}}void SLTAddPop(SLTNode** pphead, SLTdataType x)
{assert(pphead);SLTNode* newnode = Buynewnode(x);if (newnode == NULL){return;}newnode->next = *pphead;*pphead = newnode;
}void SLTDelPop(SLTNode** pphead)
{assert(pphead);assert(*pphead);SLTNode* cur = *pphead;*pphead = cur->next;free(cur);cur = NULL;
}SLTNode* SLTFindlist(SLTNode* phead, SLTdataType x)
{SLTNode* cur = phead;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}void SLTAddPoint(SLTNode** pphead, SLTNode* pos, SLTdataType x)
{assert(pos);assert(pphead);SLTNode* newnode = Buynewnode(x);if (newnode == NULL){return;}//newnode->next = pos->next;pos->next = newnode;}void SLDelPoint(SLTNode** pphead, SLTNode* pos)
{assert(pos);assert(pphead);assert(*pphead);if ((*pphead)->next == NULL&&pos->next==NULL){return;}else{SLTNode* cur = pos->next;pos->next = cur->next;free(cur);cur = NULL;}
}void SLTDestroy(SLTNode** pphead)
{assert(pphead);assert(*pphead);SLTNode* cur = *pphead;SLTNode* pre = cur;while (cur){pre = cur->next;free(cur);cur = pre;}*pphead = cur;
}
http://www.hrbkazy.com/news/44064.html

相关文章:

  • 医院网站怎么做优化排名护肤品营销策划方案
  • 专业的上海网站建设公司网站的优化与推广分析
  • 怎么做劫持网站网络销售哪个平台最好
  • 家用宽带怎样做网站服务器百度店铺怎么入驻
  • 电子商务网站制作教程关键词排名关键词优化
  • 如何做好企业网站建设工作网页模板之家
  • 网站制作厦门公司网站开发语言
  • 柳州市住房和城乡建设部网站郑州seo优化顾问热狗
  • 武汉网站开发软件程序员站长统计官方网站
  • 专做负面的网站百度seo指数查询
  • ftp上传网站企业邮箱怎么开通注册
  • 专业做室内设计的网站有哪些方面没有限制的国外搜索引擎
  • 上海市建设合同信息表网站怎么搭建自己的网站
  • 医院网站开发百度文库网址域名ip解析
  • c语言开发网站教程广州今天新闻
  • 如何做好商务网站的运营怎么做媒体资源网
  • 建设公司官网流程站长工具seo综合查询广告
  • mac 用什么软件做网站好重庆关键词优化
  • 仿别人的网站开发制作app软件
  • 有那个网站可以做报名链接的成品网站1688入口网页版怎样
  • 淘宝建设网站常见问题网址搜索引擎入口
  • 网站开发容易做吗国内真正的永久免费砖石
  • 备案过的网站换空间全网关键词指数查询
  • 深圳公司查询天津网站优化软件
  • 企业做网站这些问题必须要注意2023年适合小学生的新闻
  • 网站是谁做的seo培训优化课程
  • 凯里展示型网站设计英国搜索引擎
  • 网站开发教程免费竞价推广招聘
  • 网站打开时的客户引导页中山百度推广公司
  • filetype ppt 网站建设宁波超值关键词优化