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

数据结构 第二章 线性表(4)单链表的实现

2016-03-24 14:03 477 查看

包含的文件

c2-2.h是单链表存储结构。

bo2-2.cpp是基于顺序表的基本操作

1.InitList():初始化单链表。

2.DestroyList():销毁单链表。

3.ClearList():置空单链表。

4.ListEmpty():是否为空表。

5.ListLength():表的长度。

6.GetElem():获取第i个元素值。

7.LocateElem():获取与e判定正确的位置。

8.PriorElem():返回前驱。

9.NextElem():返回后驱。

10.ListInsert():插入。

11.ListDelete():删除。

12.ListTraverse():依次访问每个元素。

main2-2.cpp是检验bo2-2.cpp各项操作是否正确的主函数。

代码(3个)

1.c2-2.h

/************************************
FileName: c2-2.h
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/22
Description: Dynamic allocation link-node storage structure.
Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/

struct  LNode
{
ElemType data;
LNode *next;
};

typedef LNode *LinkList;


2.bo2-2.cpp

/************************************
FileName: bo2-2.cpp
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/22
Description: This file represent the page 28 in the book,
include 12 operations

Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/

Status InitList(LinkList &L)
{
L = (LinkList)malloc(sizeof(LNode));
if (!L)
{
exit(OVERFLOW);
}
L->next = NULL;
return OK;
}

Status DestroyList(LinkList &L)
{
LinkList q;
while (L)
{
q = L->next;
free(L);
L = q;
}
return OK;
}

Status ClearList(LinkList L)
{
LinkList p, q;
p = L->next;
while (p)
{
q = p->next;
free(p);
p = q;
}
L->next = NULL;
return OK;
}

Status ListEmpty(LinkList L)
{
if (L->next)
return FALSE;
else
return TRUE;
}

int ListLength(LinkList L)
{
int i = 0;
LinkList p = L->next;
while (p)
{
i++;
p = p->next;
}
return i;
}

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

int LocateElem(LinkList L, ElemType e, Status(*compare)(ElemType, ElemType))
{
int i = 0;
LinkList p = L->next;
while (p)
{
i++;
if (compare(p->data, e))
{
return i;
}
p = p->next;
}
return 0;
}

Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
{
LinkList q, p = L->next;
while (p->next)
{
q = p->next;
if (q->data == cur_e)
{
pre_e = p->data;
return OK;
}
p = q;
}
return INFEASIBLE;
}

Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e)
{
LinkList p = L->next;
while (p->next)
{
if (p->data == cur_e)
{
next_e = p->next->data;
return OK;
}
p = p->next;
}
return INFEASIBLE;
}

Status ListInsert(LinkList L, int i, ElemType e)
{
int j = 0;
LinkList p = L, s;
while (p&&j < i - 1)
{
p = p->next;
j++;
}
if (!p || j>i - 1)
return ERROR;
s = (LinkList)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}

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

Status ListTraverse(LinkList L, void(*vi)(ElemType))
{
LinkList p = L->next;
while (p)
{
vi(p->data);
p = p->next;
}
cout << endl;
return OK;
}


3.main2-2.cpp

/************************************
FileName: main2-1.cpp
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/24
Description: bo2-2.cpp is verified by correct this file

Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/

#include "Header.h"
typedef int ElemType;
#include "c2-2.h"
#include "bo2-2.cpp"

Status comp(ElemType c1, ElemType c2)
{
if (c1 == c2)
{
return TRUE;
}
else
return FALSE;
}
void visit(ElemType c)
{
cout << c << endl;
}

void main()
{
LinkList L;
int i,j;
ElemType e, e0;
i = InitList(L);
if (i)
{
cout << "initialize success.\n";
}
i = ListEmpty(L);
cout << "Empty? " << i << endl;
for (j = 1; j <= 5; j++)
{
ListInsert(L, 1, j);
}

ListTraverse(L, visit);
i = ListLength(L);
cout << "Length of list = " << i << endl;

GetElem(L, 2, i);
cout << "2th of list is " << i << endl;

i = LocateElem(L, 4, comp);

cout << "the locate of list compare 4 is " << i << endl;

PriorElem(L, 4, e);
cout << "the priorelem of 4 is " << e << endl;
NextElem(L, 4, e);
cout << "the next of 4 is " << e << endl;

ListDelete(L, 2, e0);
cout << "the delete elem is " << e0 << endl;
ListTraverse(L, visit);

DestroyList(L);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息