C++c链表的基本操作插入

浅析C++中单链表的增、删、改、减
字体:[ ] 类型:转载 时间:
以下是对C++中单链表的增、删、改、减进行了详细的介绍,需要的朋友可以过来参考下
首先是是一个简单的例子,单链表的建立和输出。程序1.1 代码如下:#include&iostream&#include&string&struct Student{&&&Student *//定义了指向Candidate类型变量的指针};int main(){&//&cout&&"请输入学生的总数:";&cin&&n;&int i=1;&Student *p=NULL;&Student *node=NULL;&Student *head=NULL;&//建立链表&for(;i&=n;i++){&&node=new S&&cout&&"请输入第"&&i&&"个同学的姓名:";&&cin&&node-&&&cout&&"请输入第"&&i&&"个同学的成绩:";&&cin&&node-&&&if(head==NULL)&&&head=&&else&&&p-&next=&&p=&&if(i==n){&&&p-&next=NULL;&&}&}&//输出链表&p=&cout&&"链表已经建立!"&&&cout&&"\n==========下面输入刚才的数据=============\n"&&&i=1;&while(p!=NULL){&&cout&&"第"&&i&&"个同学==="&&p-&name&&"==成绩===="&&p-&score&&&&p=p-&&&i++;&}&//销毁链表&Student *d;&p=&while(p!=NULL){&&d=p;&&p=p-&&&&}&return 0;}在程序1.1中,我们已经建立了一个链表。然后,我们在小樱和鸣人之间插入一个佐井同学的成绩 代码如下:#include&iostream&#include&string&struct Student{&&&Student *//定义了指向Candidate类型变量的指针};Student * Create(Student * head){&Student *p=NULL;&Student *node=NULL;&//&cout&&"请输入学生的总数:";&cin&&n;&for(int i=1;i&=n;i++){&&node=new S&&cout&&"请输入第"&&i&&"个同学的姓名:";&&cin&&node-&&&cout&&"请输入第"&&i&&"个同学的成绩:";&&cin&&node-&&&if(head==NULL)&&&head=&&else&&&p-&next=&&p=&&if(i==n){&&&p-&next=NULL;&&}&}&}void Print(Student * head){&Student *p=NULL;&p=&cout&&"链表已经建立!"&&&cout&&"\n==========下面输入刚才的数据=============\n"&&&int i=1;&while(p!=NULL){&&cout&&"第"&&i&&"个同学==="&&p-&name&&"==成绩===="&&p-&score&&&&p=p-&&&i++;&}&cout&&"\n"&&}void Insert(Student * head,int k){&Student *p=NULL;&Student *node=NULL;&p=&int i=1;&while(p!=NULL){&&if(i+1==k){&&&node=new S&&&cout&&"第"&&k&&"位同学的名字:";&&&cin&&node-&&&&cout&&"第"&&k&&"位同学的成绩:";&&&cin&&node-&&&&node-&next=p-&&&&p-&next=&&}&&p=p-&&&i++;&}}void Destory(Student * head){&&& Student *d;&Student *p=NULL;&p=&while(p!=NULL){&&d=p;&&p=p-&&&&}}int main(){&Student *head=NULL;&//创建链表&head=Create(head);&//输出链表&Print(head);&//插入数据&&cout&&"请输入你要插入的同学的序号:";&cin&&k;&Insert(head,k);&//输出链表&Print(head);&//销毁链表&&& Destory(head);&return 0;}现在,佐井同学的成绩已经插入。但是,卡卡西老师发现,鸣人的成绩抄错了,实际上是100,需要修改成绩;然后,佐助同学辍学了,所以,还要删除他的成绩。 代码如下:#include&iostream&#include&string&struct Student{&&&Student *//定义了指向Candidate类型变量的指针};Student * Create(Student * head){&Student *p=NULL;&Student *node=NULL;&//&cout&&"请输入学生的总数:";&cin&&n;&for(int i=1;i&=n;i++){&&node=new S&&cout&&"请输入第"&&i&&"个同学的姓名:";&&cin&&node-&&&cout&&"请输入第"&&i&&"个同学的成绩:";&&cin&&node-&&&if(head==NULL)&&&head=&&else&&&p-&next=&&p=&&if(i==n){&&&p-&next=NULL;&&}&}&}void Print(Student * head){&Student *p=NULL;&p=&cout&&"链表已经建立!"&&&cout&&"\n==========下面输入刚才的数据=============\n"&&&int i=1;&while(p!=NULL){&&cout&&"第"&&i&&"个同学==="&&p-&name&&"==成绩===="&&p-&score&&&&p=p-&&&i++;&}&cout&&"\n"&&}void Insert(Student * head,int k){&Student *p=NULL;&Student *node=NULL;&p=&if(k==1){&&&node=new S&&&cout&&"第1位同学的名字:";&&&cin&&node-&&&&cout&&"第1位同学的成绩:";&&&cin&&node-&&&&node-&next=head-&&&&head=&}&int i=1;&while(p!=NULL){&&if(i+1==k){&&&node=new S&&&cout&&"第"&&k&&"位同学的名字:";&&&cin&&node-&&&&cout&&"第"&&k&&"位同学的成绩:";&&&cin&&node-&&&&node-&next=p-&&&&p-&next=&&}&&p=p-&&&i++;&}}void Destory(Student * head){&&& Student *d;&Student *p=NULL;&p=&while(p!=NULL){&&d=p;&&p=p-&&&&}}void Alter(Student * head,int k){&int i=1;&Student *p=&while(p!=NULL){&&if(i==k){&&&cout&&"第"&&k&&"位同学的名字:";&&&cin&&p-&&&&cout&&"第"&&k&&"位同学的成绩:";&&&cin&&p-&&&}&&p=p-&&&i++;&}}Student * Delete(Student * head,int k){&int i=1;&Student *p=&Student *d=&if(k==1){&&head=head-&&}else{&&while(p!=NULL){&&&if(i+1==k){&&&&p-&next=p-&next-&&&&}&&&p=p-&&&&i++;&&}&}&}int main(){&Student *head=NULL;&//创建链表&head=Create(head);&//输出链表&Print(head);&//插入数据&&cout&&"请输入你要插入的同学的序号:";&cin&&k;&Insert(head,k);&//输出链表&Print(head);&//修改链表&cout&&"请输入你要修改的同学的序号:";&cin&&k;&Alter(head,k);&//输出链表&Print(head);&//删除其中的一个项&cout&&"请输入你要删除的同学的序号:";&cin&&k;&head=Delete(head,k);&&//输出链表&Print(head);&//销毁链表&&& Destory(head);&return 0;}
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Sqlist 用C/C++通过链表实现数据初始化,插入 询,删除, 以及退出的基本操作。 Data structs 结构 238万源代码下载-
&文件名称: Sqlist
& & & & &&]
&&所属分类:
&&开发工具: C-C++
&&文件大小: 172 KB
&&上传时间:
&&下载次数: 1
&&提 供 者:
&详细说明:用C/C++通过链表实现数据初始化,插入,查询,删除,显示以及退出的基本操作。-With C/C++ data initialization through the list, insert, query, delete, display, and basic operation withdrawal.
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&Sqlist\Sqlist.opt&&......\Sqlistdef.h&&......\Sqlist.cpp&&......\Sqlistdef.cpp&&......\StdAfx.h&&......\StdAfx.cpp&&......\ReadMe.txt&&......\Sqlist.dsw&&......\Sqlist.ncb&&......\Debug\vc60.idb&&......\.....\Sqlist.pch&&......\.....\vc60.pdb&&......\.....\StdAfx.obj&&......\.....\Sqlist.ilk&&......\.....\Sqlist.exe&&......\.....\Sqlist.pdb&&......\.....\Sqlist.obj&&......\.....\Sqlistdef.obj&&......\Sqlist.plg&&......\Sqlist.dsp&&......\resource.h&&......\Script1.rc&&......\RCa02936&&......\Debug&&Sqlist
&输入关键字,在本站238万海量源码库中尽情搜索:
&[] - 基于遗传算法的排课系统,可以对学校的课程进行编排。
&[] - stm32f429操作lcd的例子,,很不错的.需要的可以下来看看.共有 2444 人关注过本帖
标题:c++ 链表 插入节点迷惑
等 级:论坛游民
帖 子:47
专家分:42
结帖率:80%
&&已结贴√
&&问题点数:10&&回复次数:11&&&
c++ 链表 插入节点迷惑
void insert(node * &head,char keyWord,char newdata)//keyWord 是查找关键字符
node *newnode=//新建结点
newnode-&data=//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode-&next=//先连
head=//后断
else//否则
{//插入在pGuard 之后
newnode-&next=pGuard-&//先连
pGuard-&next=//后断
中的void insert(node * &head,char keyWord,char newdata)改成void insert(node * head,char keyWord,char newdata)好像也没问题,到底有什么区别呢?
int*&是什么意思呢?引用?
搜索更多相关主题的帖子:
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
区别还是有的
一个是指针,指针可以改变其本身的指向,而不影响到实参
一个是指针的引用,改变本身的同时也改变到了实参
等 级:论坛游民
帖 子:13
专家分:12
以下是引用shining小南在 22:25:37的发言:
void insert(node * &head,char keyWord,char newdata)//keyWord 是查找关键字符
node *newnode=//新建结点
newnode-&data=//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode-&next=//先连
head=//后断
else//否则
{//插入在pGuard 之后
newnode-&next=pGuard-&//先连
pGuard-&next=//后断
中的void insert(node * &head,char keyWord,char newdata)改成void insert(node * head,char keyWord,char newdata)好像也没问题,到底有什么区别呢?
int*&是什么意思呢?引用?&&楼上的朋友,int*&的意思不是引用,而是先求地址然后解引用哈!
等 级:论坛游民
帖 子:47
专家分:42
指针比试可以直接操作内存中的数据,用来修改实参???
来 自:河北 石家庄
等 级:黑侠
帖 子:144
专家分:528
回复 4楼 shining小南
是的,一个很经典的例子swap(int a,int b),swap(int * a,int * b),swap(int & a,int & b);具体的区别看看资料吧。
满眼生机转化钧;天工人巧日争新。
等 级:论坛游民
帖 子:47
专家分:42
以下是引用hahayezhe在 08:49:25的发言:
区别还是有的
一个是指针,指针可以改变其本身的指向,而不影响到实参
一个是指针的引用,改变本身的同时也改变到了实参node*&head 是先&head取head的地址,再*操作指针指向head的地址?是这个意思吗?
下面是我运行的程序eg输入:music# 结果四 music u u mulsic
写成node*head和node*&head结果一样 我想搞明白的是 这两种写法对程序的影响
#include &iostream.h&
struct node//定义结点结构类型
//用于存放字符数据
node *//用于指向下一个结点(后继结点)
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata);
int main()
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
node * create()
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL
node *pEnd=//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
//用于存放从键盘输入的字符
cout &&&Please input a string end with '#':& &&
do//循环至少运行一次
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
pS=//创建新结点
pS-&data=//新结点的数据为temp
pS-&next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
head=pS;//则表头指针指向这个新结点
else//否则
pEnd-&next=pS;//把这个新结点连接在表尾
pEnd=pS;//这个新结点成为了新的表尾
}while (temp!='#');//一旦输入了结尾符,则跳出循环
//返回表头指针
void showList(node *head)
node *pRead=//访问指针一开始指向表头
cout &&&The data of the link list are:& &&
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
cout &&pRead-&//输出当前访问结点的数据
pRead=pRead-&//访问指针向后移动
node * search(node *head,char keyword)//返回结点的指针
node *pRead=
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
if (pRead-&data==keyword)//如果当前结点的数据和查找的数据相符
&&& cout&&pRead-&data&&
return pR//则返回当前结点的指针
pRead=pRead-&//数据不匹配,pRead 指针向后移动,准备查找下一个结点
return NULL;//所有的结点都不匹配,返回NULL
void insert(node * head,char keyWord,char newdata)//keyWord 是查找关键字符
node *newnode=//新建结点
newnode-&data=//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode-&next=//先连
head=//后断
else//否则
{//插入在pGuard 之后
newnode-&next=pGuard-&//先连
pGuard-&next=//后断
等 级:论坛游民
帖 子:47
专家分:42
以下是引用hahayezhe在 08:49:25的发言:
区别还是有的
一个是指针,指针可以改变其本身的指向,而不影响到实参
一个是指针的引用,改变本身的同时也改变到了实参node*&head 是先&head取head的地址,再*操作指针指向head的地址?是这个意思吗?
下面是我运行的程序eg输入:music# 结果四 music u u mulsic
写成node*head和node*&head结果一样 我想搞明白的是 这两种写法对程序的影响
#include &iostream.h&
struct node//定义结点结构类型
//用于存放字符数据
node *//用于指向下一个结点(后继结点)
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata);
int main()
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
node * create()
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL
node *pEnd=//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
//用于存放从键盘输入的字符
cout &&&Please input a string end with '#':& &&
do//循环至少运行一次
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
pS=//创建新结点
pS-&data=//新结点的数据为temp
pS-&next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
head=pS;//则表头指针指向这个新结点
else//否则
pEnd-&next=pS;//把这个新结点连接在表尾
pEnd=pS;//这个新结点成为了新的表尾
}while (temp!='#');//一旦输入了结尾符,则跳出循环
//返回表头指针
void showList(node *head)
node *pRead=//访问指针一开始指向表头
cout &&&The data of the link list are:& &&
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
cout &&pRead-&//输出当前访问结点的数据
pRead=pRead-&//访问指针向后移动
node * search(node *head,char keyword)//返回结点的指针
node *pRead=
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
if (pRead-&data==keyword)//如果当前结点的数据和查找的数据相符
&&& cout&&pRead-&data&&
return pR//则返回当前结点的指针
pRead=pRead-&//数据不匹配,pRead 指针向后移动,准备查找下一个结点
return NULL;//所有的结点都不匹配,返回NULL
void insert(node * head,char keyWord,char newdata)//keyWord 是查找关键字符
node *newnode=//新建结点
newnode-&data=//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode-&next=//先连
head=//后断
else//否则
{//插入在pGuard 之后
newnode-&next=pGuard-&//先连
pGuard-&next=//后断
等 级:论坛游民
帖 子:47
专家分:42
以下是引用ciweitou163在 14:35:21的发言:
是的,一个很经典的例子swap(int a,int b),swap(int * a,int * b),swap(int & a,int & b);具体的区别看看资料吧。
是这个吗?
#include&iostream.h&
void swap(int a,int b);
void swap1(int &a,int &b);
void swap(int *a,int *b);
void display(int a,int b);
int main()
&&& int a,b;
&&& cout&&&请输入两个数:&;
&&& cin&&a&&b;
&&& int *m=&a,*n=&b;
&&& swap(a,b);
&&& display(a,b);
&&& swap1(a,b);
&&& display(a,b);
&&& swap(m,n);
&&& display(a,b);
&&& return 0;
void swap(int a,int b)
&&& temp=a;
void swap1(int &a,int &b)
&&& temp=a;
void swap(int *a,int *b)
&&& temp=*a;
void display(int a,int b)
&&& cout&&&a=&&&a&&& &&&&b=&&&b&&
上面这个明白了,刚看了,这是根据书上该的
下面的是不明白的,帖子是截取的其中一部分
#include &iostream.h&
struct node//定义结点结构类型
//用于存放字符数据
node *//用于指向下一个结点(后继结点)
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata);
void Delete(node * &head,char keyWord);
void destroy(node * &head);
int main()
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
Delete(head,'l');
showList(head);
destroy(head);
node * create()
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL
node *pEnd=//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
//用于存放从键盘输入的字符
cout &&&Please input a string end with '#':& &&
do//循环至少运行一次
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
pS=//创建新结点
pS-&data=//新结点的数据为temp
pS-&next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
head=pS;//则表头指针指向这个新结点
else//否则
pEnd-&next=pS;//把这个新结点连接在表尾
pEnd=pS;//这个新结点成为了新的表尾
}while (temp!='#');//一旦输入了结尾符,则跳出循环
//返回表头指针
void showList(node *head)
node *pRead=//访问指针一开始指向表头
cout &&&The data of the link list are:& &&
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
cout &&pRead-&//输出当前访问结点的数据
pRead=pRead-&//访问指针向后移动
node * search(node *head,char keyword)//返回结点的指针
node *pRead=
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
if (pRead-&data==keyword)//如果当前结点的数据和查找的数据相符
return pR//则返回当前结点的指针
pRead=pRead-&//数据不匹配,pRead 指针向后移动,准备查找下一个结点
return NULL;//所有的结点都不匹配,返回NULL
void insert(node * head,char keyWord,char newdata)//keyWord 是查找关键字符
node *newnode=//新建结点
newnode-&data=//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode-&next=//先连
head=//后断
else//否则
{//插入在pGuard 之后
newnode-&next=pGuard-&//先连
pGuard-&next=//后断
void Delete(node * &head,char keyWord)//可能要操作表头指针,所以head 是引用
&&& if (head!=NULL)//如果链表没有结点,就直接输出提示
&&&&&&&&node *p;
&&&&&&&&node *pGuard=//初始化pGuard 指针
&&&&&&&&if (head-&data==keyWord)//如果头结点数据符合关键字
&&&&&&&&&&&&p=//头结点是待删除结点
&&&&&&&&&&&&head=head-&//先连
&&&&&&&&&&&&//后断
&&&&&&&&&&&&cout &&&The deleted node is & &&keyWord &&
&&&&&&&&&&&&//结束函数运行
&&&&&&&&else//否则
&&&&&&&&&&&&while (pGuard-&next!=NULL)//当pGuard 没有达到表尾
&&&&&&&&&&&&{
&&&&&&&&&&&&&&& if (pGuard-&next-&data==keyWord)//如果pGuard 后继结点数据符合关键字
&&&&&&&&&&&&&&& {
p=pGuard-&//pGuard 后继结点是待删除结点
pGuard-&next=p-&//先连
cout &&&The deleted node is & &&keyWord &&
//结束函数运行
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& pGuard=pGuard-&//pGuard 指针向后移动
&&&&&&&&&&&&}
&&& cout &&&The keyword node is not found or the link list is empty!& &&//输出提示信息
void destroy(node * &head)
&&& node *p;
&&& while (head!=NULL)//当还有头结点存在时
&&&&&&&&p=//头结点是待删除结点
&&&&&&&&head=head-&//先连
&&&&&&&&//后断
&&& cout &&&The link list has been deleted!& &&
Delete(head,'l');调用head是不是相当于参数&head,那Delete函数中head中操作的不是main函数的head而是head的地址吗?
将声明和定义都改成
void showList(node *headnode *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata););//*&head改为*head
void Delete(node * head,char keyWord);//*&head改为*head
void destroy(node * head););//*&head改为*head
但对结果输入没有影响,我想问的是*&head对函数的影响
等 级:业余侠客
帖 子:77
专家分:249
&&得分:10&
'int* &a'是指针的引用,就像int &b表达的意思一样,这里b只是一个别名。同样的道理a也是一个别名,只不过是指针的别名.
你试试这两个的区别,也许你就会明白了.
1.&&&void Test(int* a)
&&&&&&&&int x=5;
&&&&&&&&a=&x;
&&& int main()
&&&&&&&&int*
&&&&&&&&int b=3;
&&&&&&&&p=&b;
&&&&&&&&cout&&p&&
&&&&&&&&Test(p);
&&&&&&&&cout&&p&&
&&&&&&&&return 0;
2.&&&void Test(int* &a)
&&&&&&&&int x=5;
&&&&&&&&a=&x;
&&& int main()
&&&&&&&&int*
&&&&&&&&int b=3;
&&&&&&&&p=&b;
&&&&&&&&cout&&p&&
&&&&&&&&Test(p);
&&&&&&&&cout&&p&&
&&&&&&&&return 0;
等 级:论坛游民
帖 子:53
专家分:24
其实我也不太懂这个的,不过看了这些回复我还是大概懂了。因为我今天刚好学了这个。
版权所有,并保留所有权利。
Powered by , Processed in 0.041465 second(s), 8 queries.
Copyright&, BCCN.NET, All Rights ReservedC++实现链表的基本操作及测试用例_Linux编程_Linux公社-Linux系统门户网站
你好,游客
C++实现链表的基本操作及测试用例
来源:Linux社区&
作者:pawnsir
今天实现了下链表的基本操作,包括节点的创建,头插尾插,头删尾删,一次遍历寻找链表的中间节点,寻找链表的倒数第x个节点,删除无头链表的非尾节点,链表的逆置,代码如下:
#include "SLinkList.h" #include &stdlib.h& #include &stdio.h& #include &assert.h& void PrintList(SListNode* pHead)//从指针位置打印链表 { while (pHead) { printf("-&%d", pHead-&data); pHead = pHead-& } printf("\n"); } static SListNode* _BuyNode(DataType x)//创建新的节点 { SListNode*ret = (SListNode*)malloc(sizeof(SListNode)); ret-&next = NULL; ret-&data = if (ret) {
} printf("创建节点失败\n");//创建失败给出提示 return NULL; } void PushBack(SListNode* & pHead, DataType x)//尾插 { if (pHead == NULL) { pHead = _BuyNode(x); } else{ SListNode*tail = pH while (tail-&next) { tail = tail-& } tail-&next = _BuyNode(x); } } void PopBack(SListNode* & pHead)//尾删 { if (pHead == NULL) {
} else if (pHead-&next-&next == NULL) { free(pHead-&next); pHead = NULL; } else{ SListNode* tail = pH while (tail-&next-&next) { tail = tail-& } free(tail-&next); tail-&next = NULL; } } void PushFront(SListNode* & pHead, DataType x)//头插 { if (pHead == NULL) { pHead = _BuyNode(x); } else{ SListNode*tmp = _BuyNode(x); tmp-&next = pH pHead = } } void PopFront(SListNode* & pHead)//t头删 { if (pHead == NULL) {
} else{ SListNode*tail = pH pHead = pHead-& tail-&next = NULL; free(tail); } } SListNode* Find(SListNode *pHead, DataType x)//以x查找节点,若存在返回给节点,若不存在返回空 { if (pHead == NULL) { return NULL; } SListNode* tail = pH while (tail) { if (tail-&data == x) {
} tail = tail-& } return NULL; } void Insert(SListNode* & pos, DataType x)//所给位置后插入一节点 { SListNode*tmp = _BuyNode(x); if (pos == NULL) { pos = } else{ tmp-&next = pos-& pos-&next = } } void Erase(SListNode * &pos)//删除无头链表的非尾节点 { if (pos == NULL) {
} else if (pos-&next == NULL) { printf("该节点为尾节点,无法删除\n"); } else{ SListNode*tmp = pos-& pos-&data = pos-&next-& pos-&next = pos-&next-& free(tmp); tmp = NULL; } } void ReveseList(SListNode * &pHead)//链表的逆置 { SListNode*tail = pH while (tail-&next) { SListNode*tmp=NULL; tmp = tail-& tail-&next = tail-&next-& tmp-&next = pH pHead = } } SListNode* FindminList(SListNode*pHead)//一次遍历,寻找链表的中间节点 { assert(pHead); SListNode *quick=pH SListNode *slow=pH while (quick) { slow = slow-& if (quick-&next) quick = quick-&next-& else }
} SListNode* FindListPosList(SListNode*pHead, int lastpos)//寻找链表的倒数第lastpos个节点 { SListNode *quick = pH SListNode *slow = pH while (quick&&lastpos--) { quick = quick-& } if (quick == NULL) {
} while (quick) { quick = quick-& slow = slow-& }
& & 测试用例如下:
105 #include &stdio.h& #include &stdlib.h& #include "SLinkList.h" void Test1()//PushBack,PrintList,PopBack { SListNode*pHead=NULL; PushBack(pHead, 1); PushBack(pHead, 2); PushBack(pHead, 3); PushBack(pHead, 4); PushBack(pHead, 5); PrintList(pHead); PopBack(pHead); PopBack(pHead); PrintList(pHead); PopBack(pHead); PopBack(pHead); PopBack(pHead); PrintList(pHead); } void Test2()//PushFront,Popfront { SListNode*pHead = NULL; PushFront(pHead, 5); PushFront(pHead, 4); PushFront(pHead, 3); PushFront(pHead, 2); PushFront(pHead, 1); PrintList(pHead); PopFront(pHead); PrintList(pHead); PopFront(pHead); PrintList(pHead); PopFront(pHead); PrintList(pHead); PopFront(pHead); PrintList(pHead); PopFront(pHead); PrintList(pHead); PopFront(pHead); PrintList(pHead); } void Test3()//Find,Insert { SListNode*pHead = NULL; PushFront(pHead, 5); PushFront(pHead, 4); PushFront(pHead, 3); PushFront(pHead, 2); PushFront(pHead, 1); Insert(pHead, 0); pHead = Find(pHead, 3); PrintList(pHead); Insert(pHead, 4); PrintList(pHead); } void Test4()//Erase { SListNode*pHead = NULL; SListNode*tmp = NULL; PushFront(pHead, 5); PushFront(pHead, 4); PushFront(pHead, 3); PushFront(pHead, 2); PushFront(pHead, 1); tmp = Find(pHead, 5); Erase(tmp); PrintList(pHead); } void Test5()//ReveseList { SListNode*pHead = NULL; PushBack(pHead, 1); PushBack(pHead, 2); PushBack(pHead, 3); PushBack(pHead, 4); PushBack(pHead, 5); & & ReveseList(pHead); PrintList(pHead); } void Test6()//FindLastposList { SListNode*pHead = NULL; SListNode*ret = NULL; PushBack(pHead, 1); PushBack(pHead, 2); PushBack(pHead, 3); PushBack(pHead, 4); PushBack(pHead, 5); & & ret=FindListPosList(pHead, 2); printf("%d\n", ret-&data); ret = FindListPosList(pHead, 6); printf("%d\n", ret-&data); } int main() { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); system("pause"); return 0; }
如有不足希望指正,有疑问希望提出
本文永久更新链接地址:
相关资讯 & & &
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律???任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款

我要回帖

更多关于 c链表教程 的文章

 

随机推荐