链表代码.docx

上传人:b****7 文档编号:26379519 上传时间:2023-06-18 格式:DOCX 页数:19 大小:17.36KB
下载 相关 举报
链表代码.docx_第1页
第1页 / 共19页
链表代码.docx_第2页
第2页 / 共19页
链表代码.docx_第3页
第3页 / 共19页
链表代码.docx_第4页
第4页 / 共19页
链表代码.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

链表代码.docx

《链表代码.docx》由会员分享,可在线阅读,更多相关《链表代码.docx(19页珍藏版)》请在冰豆网上搜索。

链表代码.docx

链表代码

头文件

template//结点记录模版声明

structLNode

{

ElemTypedata;

LNode*next;

};

template//类模版说明

classLinkList

{

private:

LNode*head;//头指针

LNode*curPtr;//当前结点指针

intcurNum;//当前结点序号

intsize;

LNode*GetNode(intpos);//返回结点指针

public:

LinkList();

~LinkList();

voidInitList();//生成一个空单链表

voidClear();//清空单链表

boolSetElem(ElemTypee,intpos);//结点数据赋值

boolGetElem(ElemType&e,intpos);//读取指定结点中的数据

intFind(ElemTypev);//按值查找结点,并返回该结点的序号,找不到返回0

boolInsert(ElemTypee,intpos);//为新元素生成一个新结点,并把它插入到指定结点前

intInsertOrder(ElemTypee);//有序插入

boolDelete(ElemType&e,intpos);//删除指定的第pos个结点

intPriorElem(ElemTypee,ElemType&x);//读取指定元素的前驱

intNextElem(ElemTypee,ElemType&x);//读取指定元素的后继

voidTraverse(void(*visit)(ElemType&b));

};

template

voidLinkList:

:

InitList()

{

head=newLNode;

head->next=curPtr=NULL;

size=0;

curNum=0;

}

template

LinkList:

:

LinkList()

{

InitList();

}

template

LinkList:

:

~LinkList()

{

Clear();

deletehead;

}

template

voidLinkList:

:

Clear()//删除所有的元素结点,但保留头结点

{

LNode*p,*t;

p=head->next;

while(p!

=NULL)

{

t=p->next;

deletep;

p=t;

}

size=curNum=0;

head->next=curPtr=NULL;

}

template

boolLinkList:

:

SetElem(ElemTypee,intpos)

{

if(pos<1||pos>size)

returnfalse;//pos越界

if(pos!

=curNum)

curPtr=GetNode(pos);//获取pos结点的指针

curPtr->data=e;//给pos结点赋值e

returntrue;

}

template

boolLinkList:

:

GetElem(ElemType&e,intpos)

{

if(pos<1||pos>size)

returnfalse;

curPtr=GetNode(pos);

e=curPtr->data;

returntrue;

}

template

intLinkList:

:

Find(ElemTypev)

{

LNode*p;

if(size==0)

return0;

p=head->next;//p指向首结点

for(intpos=1;pos<=size;pos++)

{

if(p->data==v)

break;

p=p->next;//p指向其后继

}

if(pos>size)

return0;//未找到

curPtr=p;//p结点置为当前结点

curNum=pos;

returnpos;//返回结点序号

}

template

boolLinkList:

:

Insert(ElemTypee,intpos)

{

LNode*newP;

newP=newLNode;//newP指向动态分配的新结点

newP->data=e;//新结点数据赋值

if(size==0)

{

head->next=newP;

newP->next=NULL;

curPtr=newP;

curNum=1;

size++;

cout<<"开始无元素,此元素自动设置为首元素!

";

returntrue;

}

if(pos<0||pos>size)

returnfalse;

if(pos==0)//pos为默认值,在表尾添加新结点

{

if(curNum!

=size)

curPtr=GetNode(size);//获取尾结点的指针

curPtr->next=newP;//新结点成为尾结点的后继

newP->next=NULL;//新结点被置为尾结点

}

if(pos==1)

{

newP->next=head->next;

head->next=newP;

}

if(pos!

=0&&pos!

=1)//前插入新结点

{

curPtr=GetNode(pos-1);//返回pos的前驱结点的指针

newP->next=curPtr->next;//新结点指向pos结点

curPtr->next=newP;//新结点成为pos前驱的后继

}

size++;

curPtr=newP;//新结点置为当前结点

curNum=(pos!

=0)?

pos:

size;//设置当前结点的序号

returntrue;

}

template

intLinkList:

:

InsertOrder(ElemTypee)

{

intj;

LNode*newP,*priorP,*curP;

newP=newLNode;

newP->data=e;

priorP=head;//指针priorP指向头结点

if(size==0)

{

head->next=newP;

newP->next=NULL;

j=0;

}

else

{

curP=head->next;//指针curP指向首结点

for(j=0;j

{

if(e<=curP->data)

{

priorP->next=newP;

newP->next=curP;

break;

}

priorP=curP;

curP=curP->next;

}

if(j>=size)

{

priorP=GetNode(size);

priorP->next=newP;

newP->next=NULL;

}

}

size++;

curPtr=newP;

curNum=j+1;

returncurNum;

}

template

boolLinkList:

:

Delete(ElemType&e,intpos)

{

LNode*curP;

if(pos<1||pos>size)

returnfalse;

if(pos==1)

{

curP=head->next;

head->next=curP->next;

e=curP->data;

deletecurP;

}

else

{

curPtr=GetNode(pos-1);//返回pos的前驱结点的指针

curP=curPtr->next;//curP指向pos结

curPtr->next=curP->next;//pos前驱的后继为pos的后继

e=curP->data;

deletecurP;//回收pos结点的存储单元

}

size--;

returntrue;

}

template

intLinkList:

:

PriorElem(ElemTypee,ElemType&x)//读取元素e,返回它前驱的数据值和序号

{

LNode*priorP,*curP;

priorP=head;

curP=head->next;

if(e==curP->data)

return0;

for(inti=1;curP!

=NULL;i++)

{

if(curP->data==e)

{

x=priorP->data;//前驱元素赋值给x返回

break;

}

priorP=curP;

curP=curP->next;

}

if(curP==NULL)

return0;//无e元素

returni-1;//返回前驱的序号

}

template

intLinkList:

:

NextElem(ElemTypee,ElemType&x)//查找元素e,获取它的后继元素,并用x返回

{

LNode*p;

p=head->next;

for(inti=1;p!

=NULL;i++)

{

if(p->data==e)

{

if(p->next==NULL)

return0;//无后继

x=p->next->data;//后继元素赋值给x返回

break;

}

p=p->next;

}

if(p==NULL)

return0;//无e元素

returni+1;//返回后继的序号

}

template

voidLinkList:

:

Traverse(void(*visit)(ElemType&b))

{

LNode*p;

p=head->next;//p指向首结点

while(p!

=NULL)

{

(*visit)(p->data);

p=p->next;

}

}

template

LNode*LinkList:

:

GetNode(intpos)//private

{

inti;

LNode*p;

if(pos<1||pos>size)

returnNULL;//pos越界

if(pos>curNum)

{

p=curPtr;//p指向当前结点

i=curNum;

}

else

{

p=head->next;//p指向首结点

i=1;

}

for(;i

p=p->next;

curPtr=p;//当前指针指向pos结点

curNum=pos;

returnp;//返回pos结点的指针

}

测试

#include

#include"linklist.h"

usingnamespacestd;

typedefintElemType;

voida(ElemType&b);

voidmain()

{

LinkListmylist;//定义单链表类对象,链表类型ElemType设置为int。

cout<<"定义单链表类成功!

"<

while

(1)

{

cout<<"顺序表操作菜单\n";

cout<<"___________________________________\n";

cout<<"1、新建一个空单链表2、清空单链表\n";

cout<<"3、元素结点赋值4、获取结点值\n";

cout<<"5、查找元素6、插入\n";

cout<<"7、有序插入8、删除结点\n";

cout<<"9、读取前驱10、读取后继\n";

cout<<"11、遍历链表12、结束\n";

cout<<"___________________________________\n";

cout<<"请选择要执行的操作:

";

intk;//选择序号

cin>>k;

switch(k)

{

case1:

{

mylist.InitList();

cout<<"创建链表成功!

"<

break;

}

case2:

{

mylist.Clear();

cout<<"清空链表成功!

"<

break;

}

case3:

{

intpos;

ElemTypee;

cout<<"请输入要赋的元素值:

";

cin>>e;

cout<<"请输入数据位置:

";

cin>>pos;

if(mylist.SetElem(e,pos)==false)

cout<<"元素赋值失败!

"<

else

cout<<"结点数据赋值成功!

"<

break;

}

case4:

{

intpos;

ElemTypee;

cout<<"请输入要获取的结点的位置:

";

cin>>pos;

if(mylist.GetElem(e,pos)==false)

cout<<"结点获取失败!

"<

else

cout<<"获取的元素为:

"<

break;

}

case5:

{

ElemTypev;

cout<<"请输入要查找的元素:

";

cin>>v;

if(mylist.Find(v)==0)

cout<<"该元素不存在!

"<

else

cout<<"该元素第一次出现的位置:

"<

break;

}

case6:

{

intpos;

ElemTypee;

cout<<"请输入要插入的元素:

";

cin>>e;

cout<<"请输入要出入元素的位置:

";

cin>>pos;

if(mylist.Insert(e,pos)==false)

cout<<"插入失败!

"<

else

cout<<"插入成功!

"<

break;

}

case7:

{

ElemTypee;

cout<<"请输入要插入的元素:

";

cin>>e;

cout<<"插入成功!

新元素序号为:

"<

break;

}

case8:

{

intpos;

ElemTypee;

cout<<"请输入要删除元素的位置:

";

cin>>pos;

if(mylist.Delete(e,pos)==false)

cout<<"删除失败!

"<

else

cout<<"删除元素"<

"<

break;

}

case9:

{

ElemTypee;

ElemTypex;

cout<<"请输入指定元素:

";

cin>>e;

if(mylist.PriorElem(e,x)==0)

cout<<"首元素"<

或无"<

"<

else

{

cout<<"指定元素前驱的序号为:

"<

cout<<"指定元素前驱的数据值为:

"<

}

break;

}

case10:

{

ElemTypee;

ElemTypex;

cout<<"请输入指定元素:

";

cin>>e;

if(mylist.NextElem(e,x)==0)

cout<<"尾元素"<

或无"<

"<

else

{

cout<<"指定元素后继的序号为:

"<

cout<<"指定元素后继的数据值为:

"<

}

break;

}

case11:

{

mylist.Traverse(a);

cout<

break;

}

case12:

{

cout<<"链表操作结束!

\n";

break;

}

default:

{

cout<<"操作无效!

"<

break;

}

}

}

}

voida(ElemType&b)

{

cout<

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1