数据结构与算法:链表基础

数据结构与算法:链表基础

链表是通过指针连接数据元素的一种数据结构。链表由一系列结点(NODE)组成,每个结点包含两个域:存储数据元素的数据域(datafield)和存储下一个结点地址的指针域(pointerfield)。链表是除了数组以外使用得最多的数据结构,学习链表需要掌握以下几个重要的概念:

头指针(headpointer)—指向链表中第一个结点的指针。

首元结点—指链表中存储第一个数据元素的结点。

头结点(headnode)—在首元结点之前附设的一个结点,该结点不存储数据元素,其指针域指向首元结点,其作用主要是为了方便对链表的操作。它可以对空表、非空表以及首元结点的操作进行统一处理。头指针指向头结点,单链表中可以没有头结点,此时头指针指向首元结点。

链表的表示

链表(下面默认指单链表)可以看成由一系列结点组成,每个结点又指向下一个结点。

其中head为头指针,存储数据项DataItems的是数据域,存储直接后继(即下一个结点)位置的是指针域。需注意以下几点:

链表必须包含头指针

每个节点都包含数据域与指针域

结点之间通过指针相连

最后一个结点的指针为空

链表的类型

单链表——也称线性链表,每个结点只包含一个指针域,指针的指向是单向的。

双向链表——双向链表包含两个指针域,一个指向直接后继(immediatesuccessor),一个指向直接前驱(immediatepdecessor)。

循环链表——表中最后一个结点的指针域指向头结点,整个链表形成一个环。

基本操作

链表的基本操作也包括:

插入

删除

搜索

遍历

逆置

插入操作

在链表中插入一个新结点需要不止一步(但比数组简单),我们先通过图示来学习,暂时不涉及具体代码。首先,我们需要创建一个与链表结点有相同结构的结点,然后,确定插入位置。

假如我们要把新结点B(NewNode)插入到结点A(左边结点LeftNode)、C(右边结点RightNode)之间,那么需要让B指向C。

NewNode.next?RightNode;

图示如下:

然后将结点A指向新结点。

LeftNode.next?NewNode;

这样新结点就插入到两结点之间了,最终结果如下:

在链表开头插入结点步骤于此类似,之后的代码中会涉及到,在链表末尾插入结点时,最后一个结点指向新结点,新结点指向NULL。

删除操作

删除操作也需要不止一步,首先,使用搜索算法定位要删除的结点。

目标结点(TargetNode)的前驱(LeftNode)应当指向目标结点的后继。

LeftNode.next?TargetNode.next;

然后断开目标结点与其后继的连接。

TargetNode.next?NULL;

如果被删除的结点无需再次使用的话我们就可以释放掉它占用的内存。最终结果如下:

逆置操作

转置操作相对复杂一些,我们需要让头指针指向最后一个结点,并且逆置整个链表。

首先,我们来操作最后一个结点,其指针本应指向NULL,现在我们让它指向其前驱结点。

为了确保当前结点不会丢失,必须设置一个临时结点来保存它,并使当前结点的后继指向该临时结点,从头开始依次进行操作。

除了头指针指向的第一个节点外,所有节点都应指向其直接前驱并使其直接前驱成为新的直接后继。第一个结点指向NULL。

然后通过临时结点,使头指针指向新的首元节点。

C语言实现

#includestdio.h#includestring.h#includestdlib.h#includestdbool.hstructnode{intdata;intkey;structnode*next;};structnode*head=NULL;structnode*curnt=NULL;//displaythelistvoidprintList(){structnode*ptr=head;printf("\n[");//startfromthebeginningwhile(ptr!=NULL){printf("(%d,%d)",ptr-key,ptr-data);ptr=ptr-next;}printf("]");}//insertlinkatthefirstlocationvoidinsertFirst(intkey,intdata){//catealinkstructnode*link=(structnode*)malloc(sizeof(structnode));link-key=key;link-data=data;//pointittooldfirstnodelink-next=head;//pointfirsttonewfirstnodehead=link;}//deletefirstitemstructnode*deleteFirst(){//savefencetofirstlinkstructnode*tempLink=head;//marknexttofirstlinkasfirsthead=head-next;//turnthedeletedlinkturntempLink;}//islistemptyboolisEmpty(){turnhead==NULL;}intlength(){intlength=0;structnode*curnt;for(curnt=head;curnt!=NULL;curnt=curnt-next){length++;}turnlength;}//findalinkwithgivenkeystructnode*find(intkey){//startfromthefirstlinkstructnode*curnt=head;//iflistisemptyif(head==NULL){turnNULL;}//navigatethroughlistwhile(curnt-key!=key){//ifitislastnodeif(curnt-next==NULL){turnNULL;}else{//gotonextlinkcurnt=curnt-next;}}//ifdatafound,turnthecurntLinkturncurnt;}//deletealinkwithgivenkeystructnode*delete(intkey){//startfromthefirstlinkstructnode*curnt=head;structnode*pvious=NULL;//iflistisemptyif(head==NULL){turnNULL;}//navigatethroughlistwhile(curnt-key!=key){//ifitislastnodeif(curnt-next==NULL){turnNULL;}else{//stofencetocurntlinkpvious=curnt;//movetonextlinkcurnt=curnt-next;}}//foundamatch,updatethelinkif(curnt==head){//changefirsttopointtonextlinkhead=head-next;}else{//bypassthecurntlinkpvious-next=curnt-next;}turncurnt;}voidsort(){inti,j,k,tempKey,tempData;structnode*curnt;structnode*next;intsize=length();k=size;for(i=0;isize-1;i++,k--){curnt=head;next=head-next;for(j=1;jk;j++){if(curnt-datanext-data){tempData=curnt-data;curnt-data=next-data;next-data=tempData;tempKey=curnt-key;curnt-key=next-key;next-key=tempKey;}curnt=curnt-next;next=next-next;}}}voidverse(structnode**head_f){structnode*pv=NULL;structnode*curnt=*head_f;structnode*next;while(curnt!=NULL){next=curnt-next;curnt-next=pv;pv=curnt;curnt=next;}*head_f=pv;}intmain(){insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);printf("OriginalList:");//printlistprintList();while(!isEmpty()){structnode*temp=deleteFirst();printf("\nDeletedvalue:");printf("(%d,%d)",temp-key,temp-data);}printf("\nListafterdeletingallitems:");printList();insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);printf("\nRestodList:");printList();printf("\n");structnode*foundLink=find(4);if(foundLink!=NULL){printf("Elementfound:");printf("(%d,%d)",foundLink-key,foundLink-data);printf("\n");}else{printf("Elementnotfound.");}delete(4);printf("Listafterdeletinganitem:");printList();printf("\n");foundLink=find(4);if(foundLink!=NULL){printf("Elementfound:");printf("(%d,%d)",foundLink-key,foundLink-data);printf("\n");}else{printf("Elementnotfound.");}printf("\n");sort();printf("Listaftersortingthedata:");printList();verse(head);printf("\nListafterversingthedata:");printList();turn0;}

输出结果如下:

OriginalList:[(6,56)(5,40)(4,1)(3,30)(2,20)(1,10)]Deletedvalue:(6,56)Deletedvalue:(5,40)Deletedvalue:(4,1)Deletedvalue:(3,30)Deletedvalue:(2,20)Deletedvalue:(1,10)Listafterdeletingallitems:[]RestodList:[(6,56)(5,40)(4,1)(3,30)(2,20)(1,10)]Elementfound:(4,1)Listafterdeletinganitem:[(6,56)(5,40)(3,30)(2,20)(1,10)]Elementnotfound.Listaftersortingthedata:[(1,10)(2,20)(3,30)(5,40)(6,56)]Listafterversingthedata:[(6,56)(5,40)(3,30)(2,20)(1,10)]

转载请参看关于博客页面相关要求。









































白癫风医院
治疗白癜风最好的医院



转载请注明:http://www.92nongye.com/zyjs/204612461.html