欢迎来到天天文库
浏览记录
ID:12894834
大小:95.00 KB
页数:11页
时间:2018-07-19
《实验二 单链表基本操作的实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验二单链表基本操作的实现【实验课程名称】数据结构【实验项目名称】单链表基本操作的实现【实验目的】1理解单链表的存储结构及基本操作的定义;2掌握单链表存储基本操作;3学会设计实验数据验证程序。【实验仪器及环境】计算机,windowxp操作系统,VC++6.0【实验内容及步骤】1.单链表顺序存储基本操作存储结构定义:typedefstructLNode{//结点类型ElemTypedata;structLNode*next;}*Link,*Position;typedefstruct{//链表类型Linkhead,tail;intlen;}LinkList;实现的基本操作:#inc
2、lude#include#include#includeusingnamespacestd;#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2typedefintStatus;typedefintElemType;typedefstructLNode{//结点类型ElemTypedata;structLNode*next;}*Link,*Position;typedefstruct{//
3、链表类型Linkhead,tail;intlen;-11-}LinkList;PositionMakeNode_L(Link&p,ElemTypee)//创建结点{p=(Link)malloc(sizeof(LNode));if(!p)returnERROR;p->data=e;p->next=NULL;returnp;}voidFreeNode_L(Link&q)//释放结点{free(q);}StatusInitList_L(LinkList&L){//初始化L为一个带头结点的空链表,头尾指针指向头结点,表长赋ElemTypee;e=-1;//实际应用中此初始化语句需要修改i
4、f(!MakeNode_L(L.head,e))returnERROR;//开辟头结点L.tail=L.head;L.len=0;returnOK;}//InitList_LStatusDestroyList_L(LinkList&L){//销毁链表LLinkp;while(p=L.head->next){//依次释放有序链表中第一个元素至最后一个元素所占用空间;L.head->next=p->next;free(p);}free(L.head);L.head=NULL;L.tail=NULL;L.len=0;cout<5、d!"<next;while(p){q=p->next;free(p);p=q;}L.tail=L.head;-11-L.len=0;returnOK;}StatusInsFirst_L(LinkList&L,Links)//在首元素前插入一个结点{s->next=L.head->next;if(!L.head->next)L.tail=s;L.head->next=s;L.len++;returnOK;}6、StatusDelFirst_L(LinkList&L,Linkh,Link&q)//删除首结点{h=L.head;q=L.head->next;if(q){h->next=q->next;q->next=NULL;if(!h->next)L.tail=h;L.len--;returnOK;}elsereturnERROR;}StatusAppend_L(LinkList&L,Links)//将两个链表跟一个字符串连接起来{Linkq;if(!L.head->next)L.head->next=q=s;elseL.tail->next=q=s;while(q->next){q=q7、->next;}L.tail=q;returnOK;}-11-PositionRemove_L(LinkList&L,Link&q)//删除尾结点{Linkp;p=L.head;if(!L.head->next)cout<<"TheLinkListisempty!"<next!=L.tail)p=p->next;q=L.tail;L.tail=p;L.tail->next=NULL;L.len--;}returnq;}StatusInsBe
5、d!"<next;while(p){q=p->next;free(p);p=q;}L.tail=L.head;-11-L.len=0;returnOK;}StatusInsFirst_L(LinkList&L,Links)//在首元素前插入一个结点{s->next=L.head->next;if(!L.head->next)L.tail=s;L.head->next=s;L.len++;returnOK;}
6、StatusDelFirst_L(LinkList&L,Linkh,Link&q)//删除首结点{h=L.head;q=L.head->next;if(q){h->next=q->next;q->next=NULL;if(!h->next)L.tail=h;L.len--;returnOK;}elsereturnERROR;}StatusAppend_L(LinkList&L,Links)//将两个链表跟一个字符串连接起来{Linkq;if(!L.head->next)L.head->next=q=s;elseL.tail->next=q=s;while(q->next){q=q
7、->next;}L.tail=q;returnOK;}-11-PositionRemove_L(LinkList&L,Link&q)//删除尾结点{Linkp;p=L.head;if(!L.head->next)cout<<"TheLinkListisempty!"<next!=L.tail)p=p->next;q=L.tail;L.tail=p;L.tail->next=NULL;L.len--;}returnq;}StatusInsBe
此文档下载收益归作者所有