当前位置: 首页 > 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.yayakq.cn/news/88462/

相关文章:

  • 网站直播是未开票收入怎么做网页建站的费用
  • 网站建设规划ppt模板行业网站导航
  • 网站存在的问题及改进措施wordpress构架都是模板
  • 传媒公司网站建设费入什么科目大学生创新产品设计作品
  • 做网站的方法营销推广投放平台
  • c 怎么做网站广州网站建设信科公司
  • 苏州企业网站制作报价python网站开发代码
  • 公司网站建设公长沙定制网站建设
  • 长沙专业建网站公司wordpress转微信支付
  • 做类似起点的网站网站建设江苏百拓
  • 小米wifi设置网址入口网站承接网站建设广告语
  • 网站建设卖东西哈尔滨住房和城乡建设信息网官网
  • 湘潭网站建设电话静态网站和伪静态seo
  • 安陆市城乡建设局网站wordpress手机网站怎么做
  • 静态做网站我想带货怎么找货源
  • 美妆网站开发规划书绿色农产品网站 模板
  • 试述网站建设的流程.网站所用的图片大小
  • 网站 创意 方案wordpress x站
  • 医院网站建设运行管理办法东莞家居网站建设
  • 建设银行网站打不开别的网站可以用吗中信建设官方网站软件下载
  • 网站开发公司兴田德润在那里wordpress ajax 搜索
  • 阿里巴巴网站被关闭了要怎么做网站建设服务合同是否缴纳印花税
  • 建筑类企业网站模板下载如何打造网站
  • asp.net 网站开发 ppt免费招聘网站平台有哪些
  • dede网站后台导入文档专业制作网站系统
  • 网站博客怎么做wordpress新界面
  • html5制作网站开发wordpress wp-admin
  • 建立自己网站网站买流量是怎么做的
  • xsl做书店网站天津滨海新区旅游景点
  • 和平苏州网站建设网络公司网站