您的位置:首页 > 其它

顺序表的实现

2015-06-20 12:53 399 查看
文件:lineList.h

define TRUE 1

define FALSE 0

define OK 1

define ERROR -1

define OVERFLOW -2

define MAX 100

define INCREASE 20

typedef char ElemType ;

typedef int State ;

typedef struct {

ElemType *elem ;

int listSize ;

int listLength ;

} Sqlist ;

void InitList(Sqlist &L) ; // 构造一个空的线性表

void CreatList(Sqlist &L ,int n) ; // 为线性表初始化n个元素

State DestroyList(Sqlist &L) ; // 销毁一个存在的线性表

State ClearList(Sqlist &L) ; // 清空一个存在的线性表

State ListEmpty(Sqlist L) ; // 判断一个已经存在的线性表,若不空返回True ,否则返回false

State ListLength(Sqlist L) ; // 若线性表存在, 返回线性表的长度。

State GetElem(Sqlist L , int i , ElemType &e) ; // 返回L中第i个数据元素的值

int LocateElem(Sqlist L , ElemType e) ; // 返回e在L中的位置

State ListInsert(Sqlist &L,int i , ElemType e) ; // 在L中第i个元素前插入e

State ListDelete(Sqlist &L,int i , ElemType &e) ; // 在L中删除第i个元素

State ListTravel(Sqlist L) ; // 遍历线性表的元素

void UnionList(Sqlist &La, Sqlist Lb) ; // 将Lb中不在La中的元素插入La中

文件:lineList.cpp

include

include

include

include”lineList.h”

void InitList(Sqlist &L){

// 创建一个空的线性表

L.elem = (ElemType )malloc(MAX sizeof(Sqlist)) ;

if(!L.elem ) // 存储分配空间失败

exit(OVERFLOW) ;

L.listLength = 0;

L.listSize = MAX ; // 初始存储空间大小

}

void CreatList(Sqlist &L, int n ) {

// 为线性表初始化,n个元素

if(!L.elem) // 线性表不存在

exit(OVERFLOW) ;

for(int i = 0 ; i < n ; i ++){

scanf(“%c”,&L.elem[i]) ;

}

L.listLength = n ;

}

State DestroyList(Sqlist &L){

// 销毁已经存在的线性表L;

if(L.elem) // 链表已经存在

free(L.elem) ; // 释放链表空间

L.elem = NULL ;

L.listLength = 0;

L.listSize = 0 ;

return OK ;

}

State ClearList(Sqlist &L){

// 清空线性表的内容

if(L.elem){ // 若线性表存在 ,

for(int i = 0; i < L.listLength ; i++)

L.elem[i] = 0 ;

L.listLength = 0 ;

return OK ;

}

else

return ERROR ;

}

State ListEmpty(Sqlist L ){

// 线性表为空返回True ,否则返回 false

if(L.elem){ // 线性表存在

if(L.listLength == 0)

return TRUE ;

else

return FALSE ;

}

return ERROR ;

}

State ListLength(Sqlist L){

// 返回线性表的长度

if(L.elem){ // 线性表存在

return L.listLength ;

}

else

return ERROR ;

}

State GetElem(Sqlist L , int i , ElemType &e){

// 获得 线性表L第i个位置的元素,赋值给e

if(L.elem){ // 线性表存在

if(i < 1 || i > L.listLength ) // 位置不合法

return ERROR ;

e = L.elem[i - 1] ;

return OK ;

}

else

return ERROR ;

}

int LocateElem(Sqlist L , ElemType e) {

// 返回e在线性表中的位置

if(L.elem){ // 若线性表存在

for(int i = 0 ; i < L.listLength ; i++)

if(e == L.elem[i])

return i ;

return ERROR ;

}

return ERROR ;

}

State ListInsert(Sqlist &L,int i , ElemType e){

// 在线性表中第i个位置前插入元素 e

if(i < 1 || i > L.listLength)

exit(OVERFLOW) ; // 插入位置不合法

if(L.listLength >= L.listSize){ //线性表空间已满

ElemType newbase = (ElemType )realloc(L.elem , (L.listSize + INCREASE)*sizeof(Sqlist)) ;

if(!newbase)

exit(OVERFLOW) ; // 存储空间分配失败

L.elem = newbase ;

L.listSize += INCREASE ;

}

for(int k = L.listLength - 1 ; k >= i ; k–) // 第i个元素后的元素后移

L.elem[k + 1] = L.elem[k] ;

L.elem[i] = e ; // 将e置入线性表中,

L.listLength ++ ; // 长度加1

return OK ;

}

State ListDelete(Sqlist &L , int i , ElemType &e){

// 删除线性表中的第i个元素,并赋值给e

if(i < 1 || i > L.listLength)

exit(OVERFLOW) ; // 删除位置不合法

e = L.elem[i - 1] ;

for(int k = i - 1 ; k < L.listLength - 1 ; k++)

L.elem[k] = L.elem[k + 1] ; // 第i个元素后的元素前移

L.listLength– ;

return OK ;

}

State ListTravel(Sqlist L){

// 遍历线性表的内容

if(L.elem){ // 线性表存在

for(int i = 0 ; i < L.listLength ; i++)

printf(“%c”,L.elem[i]);

printf(“\n”) ;

return OK ;

}

else

return ERROR;

}

void UnionList(Sqlist &La, Sqlist Lb){

// 将Lb中不在La中的元素插入到La中

int L1 = ListLength(La) ;

int L2 = ListLength(Lb) ;

for(int i = 0 ; i < L2 ; i++ ){

if(LocateElem(La,Lb.elem[i])== -1) // 如果在La中不存在Lb.elem[i] ,返回-1

{

ListInsert(La,L1,Lb.elem[i]) ; // 将Lb.elem[i]插入到La的末尾

L1++ ;

}

}

}

插入算法和删除算法的时间复杂度分析:

当在顺序表存储结构的线性表中某个位置插入或者删除一个数据元素是,其时间主要耗费在移动元素上,而移动元素的个数取决于插入或删除的元素的位置。

假设pi是在第i个位置前上插入一个元素的概率,则在长度为n的线性表中插入一个元素所需要移动元素次数的平均次数为

Ei =


假设qi是删除第i个位置上的元素的概率, 则在长度为n的线性表中删除一个元素所需要移动元素次数的平均次数为

Ed =


一般在线性表中的任何位置删除或者插入元素都是等概率的,

Ei =

Ed =


所以的 Ei =



=

Ed =



=


所以在顺序线性表中插入和删除一个元素的时间复杂度 为 O(n)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  顺序表的实现