第一篇:線性表-順序表的設(shè)計

##對于?空的線性表和線性結(jié)構(gòu),其特點如下:

? 存在唯?的?個被稱作”第?個”的數(shù)據(jù)元素;

? 存在唯?的?個被稱作”最后?個"的數(shù)據(jù)元素

? 除了第?個之外,結(jié)構(gòu)中的每個數(shù)據(jù)元素均有?個前驅(qū)

? 除了最后?個之外,結(jié)構(gòu)中的每個數(shù)據(jù)元素都有?個后繼.

#include

#include "stdlib.h"

#include"math.h"

#include"time.h"

#define MAXSIZE100/// 表大小

#define OK1/// 狀態(tài)

#define ERROR0/// 錯誤

typedefintElemType;///元素類型,假設(shè)為int

typedefintStatus;///狀態(tài):函數(shù)結(jié)果返回狀態(tài)

// 順序表的結(jié)構(gòu)

typedef?struct?{

? ? ElemType*data;///數(shù)據(jù)

?intlength;///長度

} Sqlist;

//1、 初始化順序表

Status InitSqlist (Sqlist *L) {

? ? // 分配數(shù)據(jù)空間

L->data=malloc(sizeof(ElemType) *MAXSIZE);

?if(!L->data) {

? ? ? ? exit(ERROR);// 報錯則退出

? ? }

? ? // 空白長度為0

? ? L->length=0;

?returnOK;

}

// 2、順序表:數(shù)據(jù)插入

/*

?初始條件:順序線性表L已存在,1≤i≤ListLength(L);

?操作結(jié)果:在L中第i個位置之前插入新的數(shù)據(jù)元素e,L的長度加1

?*/

Status InSertElem (Sqlist *L,?int?i, ElemType e) {

? ? // 位置是否合法

?if(i <0|| i > L->length) {

?returnERROR;

? ? }

? ? // 存儲空間是否已滿

?if(L->length>=MAXSIZE) {

?returnERROR;

? ? }

? ? // 插入位置是否在表尾

?if(i < L->length) {

? ? ? ? // 不在表尾,則移動表,空出位置

?for(intj = L->length; j > i; j--) {

? ? ? ? ? ? // 插入位置以及之后的位置后移動1位

? ? ? ? ? ? L->data[j] = L->data[j -1];

? ? ? ? }

? ? }

? ? // 插入新元素

? ? L->data[i] = e;

? ? // 表長度加1

? ? ++L->length;

?returnOK;

}

// 3、順序表:取值

Status GetElem (Sqlist L,?int?i, ElemType *e) {

?if((i > L.length-1) || i <0) {

?returnERROR;

? ? }

? ? (*e) = L.data[i];

?returnOK;

}

// 4、順序表:刪除

Status ListDeleteNum(Sqlist *L,?int?i) {

? ? // 異常情況處理

?if(i <0|| i > (L->length-1) || L ->length==0) {

?returnERROR;

? ? }

? ? // 被刪除元素后面的元素都往前移動一位

?for(intj = i; j < L->length; j++) {

? ? ? ? L->data[j] = L->data[j +1];

? ? }

? ? // 長度減少1

? ? L->length--;

?returnOK;

}

// 5、順序表:遍歷輸出

Status TraverseList (Sqlist L) {

?for(inti =0; i < L.length; i++) {

? ? ? ? ElemTypee = L.data[i];

? ? ? ? printf("元素值:%d -- %d\n", i, e);

? ? }

?returnOK;

}

// 6、遍歷查找是否 存在摸個元素,存在則返回位置,否則返回-1

int?LocateElem(Sqlist L, ElemType e) {

?if(L.length==0) {

?return-1;

? ? }

?inti =0;

?for(i =0; i < L.length-1; i++) {

?if(L.data[i] == e) {

?break;

? ? ? ? }

? ? }

?if(i > L.length-1) {

?return-1;

? ? }

?return(i +1);

}

intmain(intargc,constchar* argv[]) {

? ? // insert code here...

? ? printf("Hello, World!\n");

? ? SqlistL;

? ? ElemType e;

? ? StatusiStatus;

? ? // 初始化順序表

? ? iStatus =InitSqlist(&L);

? ? printf("初始化結(jié)果: %d\n -- 長度:%d\n", iStatus, L.length);

? ? // 插入數(shù)據(jù)

?for(intj =0; j <5;j++){

? ? ? ? iStatus =InSertElem(&L,0, j);

? ? }

? ? printf("插入數(shù)據(jù)L長度: %d\n",L.length);

? ? // 輸出

? ? iStatus =TraverseList(L);

? ? // 取值

?intnum =2;

? ? GetElem(L, num -1, &e);

? ? printf("取第%d個元素值為: %d\n", num, e);

? ? // 刪除第2個元素

? ? num =2;

? ? iStatus =ListDeleteNum(&L, num -1);

? ? // 輸出

? ? iStatus =TraverseList(L);

? ? printf("++++++++++++++++++++++++++\n");

? ? // 查找

?for(intj = L.length-1; j <27; j++){

? ? ? ? iStatus =InSertElem(&L,0, j);

? ? }

? ? // 輸出

? ? iStatus =TraverseList(L);

? ? printf("20的位置:%d\n",LocateElem(L,20));

?return0;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容