您的位置:首页 > 理论基础 > 数据结构算法

(C语言)单链表的链式实现(数据结构二)

2013-10-09 21:38 711 查看
1.数据类型定义

在代码中为了清楚的表示一些错误和函数运行状态,我们预先定义一些变量来表示这些状态。在head.h头文件中有如下定义:

//定义数据结构中要用到的一些变量和类型
#ifndef HEAD_H
#define HEAD_H

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define TRUE  1
#define FALSE 0
#define OK    1
#define ERROR  0
#define INFEASIBLE -1
#define OVERFLOW   -2    //分配内存出错

typedef int  Status;     //函数返回值类型
typedef int  ElemType;   //用户定义的数据类型

#endif


2.单链表数据结构实现

为了实现单链表,我们定义结构体 LNode,具体代码如下:

typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;


3.链表方法摘要

Status InitList(LinearList & L);    //初始化链表

Status DestroyList(LinearList &L);   //销毁链表

Status ClearList(LinearList &L);     //清空链表

Status ListEmpty(LinearList L);      //链表是否为空

Status ListLength(LinearList L);     //链表长度

Status GetElem(LinearList L,int i,ElemType &e);  //获得链表第i位置的长度,返回给e

Status LocateElem(LinearList L,ElemType e,Status(*comp)(ElemType,ElemType)); //链表中满足comp条件的数据的位置

Status PriorElem(LinearList L,ElemType cur_e,ElemType &per_e)  // cur_e的前一个数据

Status NextElem(LinearList L,ElemType cur_e,ElemType &next_e);  //cur_e的后一个数据

Status ListInsert(LinearList &L,int i,ElemType e);    //在第i个位置插入e

Status ListDelete(LinearList &L,int i,ElemType &e);   //删除第i位置数据,并给e

Status Union(LinearList &La,LinearList Lb);     //La=la并Lb

Status MergeList(LinearList La,LinearList Lb,LinearList &Lc);  //La和Lb从小到大排序后给Lc


4.单链表顺序实现

在LinkList.h文件中实现单链表的方法,具体代码如下:

#ifndef LINKLIST_H
#define LINKLIST_H
#include "head.h"

typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList;

Status equal(int a,int b){
return a==b;
}

Status InitList(LinkList &L){
L=(LinkList)malloc(sizeof(LNode));
if (!L) return OVERFLOW;
L->data=-1;
L->next=NULL;
return OK;
}
Status DestroyList(LinkList &L){
LinkList q=L;
while(q!=NULL){
LinkList p=q;
q=q->next;
free(p);
p=NULL;
}
return OK;
};

Status ClearList(LinkList &L){
DestroyList(L);
InitList(L);
return OK;
}
Status ListEmpty(LinkList &L){
return L->next==NULL;
}
int ListLength(LinkList L){
int i=0;
LinkList pt;
pt=L->next;
while(pt){
i++;
pt=pt->next;
}
return i;
}

Status GetElem(LinkList L,int i,ElemType &e){
if(i<1||i>ListLength(L)) return ERROR;
int n=1;
LinkList pt;
pt=L;
while(n<=i&&pt){
pt=pt->next;
++n;
}
e=pt->data;
return OK;
}

Status LocateElem(LinkList L,ElemType e,Status(*comp)(ElemType,ElemType)){
int i=1;
for (;i<=ListLength(L);i++)
{
ElemType e1;
GetElem(L,i,e1);
if (comp(e,e1))
break;
}
if (i>ListLength(L))
{
return 0;
}
return i;
}

Status PriorElem(LinkList L,ElemType cur_e,ElemType &per_e){
int i=LocateElem(L,cur_e,equal);
if (i<=1) return ERROR;
GetElem(L,i-1,per_e);
return OK;
}

Status NextElem(LinkList L,ElemType cur_e,ElemType &next_e){
int i=LocateElem(L,cur_e,equal);
if ( i==0 || i==ListLength(L)) return ERROR;
GetElem(L,i+1,next_e);
return OK;
}

Status ListInsert(LinkList &L,int i,ElemType e){
if(i<1||i>ListLength(L)+1) return ERROR;
LinkList pt=L;
for (int n=1;n<i;n++)
{
pt=pt->next;
}
LinkList q=(LinkList)malloc(sizeof(LNode));
q->data=e;
q->next=pt->next;
pt->next=q;
return OK;
}

Status ListDelete(LinkList &L,int i,ElemType &e){
LinkList pt;
pt=L;
int n=1;
while(n<=i-1&&pt){
pt=pt->next;
++n;
}
if(!pt||n>i) return ERROR;
LinkList q;
q=pt->next;
pt->next=q->next;
e=q->data;
free(q);
q=NULL;
return OK;
}

Status Union(LinkList &La,LinkList Lb){
int la_l=ListLength(La);
int lb_l=ListLength(Lb);
for (int i=1;i<=lb_l;i++)
{
ElemType e=0;
GetElem(Lb,i,e);
if(!LocateElem(La,e,equal)){
int l=ListLength(La);
ListInsert(La,++l,e);
}
}
return OK;
}

Status MergeList(LinkList La,LinkList Lb,LinkList &Lc){
int La_l=ListLength(La);
int Lb_l=ListLength(Lb);
InitList(Lc);
int i=1,j=1,k=1;
while(i<=La_l&&j<=Lb_l){
ElemType La_e,Lb_e;
GetElem(La,i,La_e);
GetElem(Lb,j,Lb_e);
if (La_e<=Lb_e)
{
ListInsert(Lc,k++,La_e);
i++;
}else{
ListInsert(Lc,k++,Lb_e);
j++;
}
}
while(i<=La_l){
ElemType La_e;
GetElem(La,i,La_e);
ListInsert(Lc,k++,La_e);
i++;
}
while(j<=Lb_l){
ElemType Lb_e;
GetElem(Lb,j,Lb_e);
ListInsert(Lc,k++,Lb_e);
j++;
}
return OK;
}

#endif


5.单链表测试

#include "LinkList.h"

void main(){
LinkList L;
InitList(L);                //初始化链表
for (int i=1;i<10;i++)
ListInsert(L,i,i);   //向链表中插入数据

printf("\n链表L中数据:");
for(int i=1;i<=ListLength(L);i++){
ElemType e;
GetElem(L,i,e);
printf("%d->",e);
}
printf("end");

ElemType e;
ListDelete(L,5,e);             //删除第5位置数据
printf("\n删除第5位置数据为:%d",e);

PriorElem(L,6,e);               //前一个数据
printf("\n6的前一个数据:%d",e);

NextElem(L,6,e);                 //后一个数据
printf("\n6的后一个数据:%d",e);

printf("\n链表中数据:");
for(int i=1;i<=ListLength(L);i++){
ElemType e;
GetElem(L,i,e);
printf("%d->",e);
}
printf("end\n");

LinkList Lb;
LinkList Lc;
InitList(Lb);
for(int i=1;i<10;i++)
ListInsert(Lb,i,i+5);

printf("\n链表Lb中数据:");
for(int i=1;i<=ListLength(Lb);i++){
ElemType e;
GetElem(Lb,i,e);
printf("%d->",e);
}
printf("end\n");

Union(L,Lb);    //L=L并Lb

printf("\n链表L中数据:");
for(int i=1;i<=ListLength(L);i++){
ElemType e;
GetElem(L,i,e);
printf("%d->",e);
}
printf("end");

MergeList(L,Lb,Lc);    //测试MergeList()

printf("\n链表Lc中数据:");
for(int i=1;i<=ListLength(Lc);i++){
ElemType e;
GetElem(Lc,i,e);
printf("%d->",e);
}
printf("end\n");

}


6.测试结果

链表L中数据:1->2->3->4->5->6->7->8->9->end
删除第5位置数据为:5
6的前一个数据:4
6的后一个数据:7
链表中数据:1->2->3->4->6->7->8->9->end

链表Lb中数据:6->7->8->9->10->11->12->13->14->end

链表L中数据:1->2->3->4->6->7->8->9->10->11->12->13->14->end
链表Lc中数据:1->2->3->4->6->6->7->7->8->8->9->9->10->10->11->11->12->12->13->13->14->14->end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: