##對(duì)于?空的線性表和線性結(jié)構(gòu),其特點(diǎn)如下:
? 存在唯?的?個(gè)被稱(chēng)作”第?個(gè)”的數(shù)據(jù)元素;
? 存在唯?的?個(gè)被稱(chēng)作”最后?個(gè)"的數(shù)據(jù)元素
? 除了第?個(gè)之外,結(jié)構(gòu)中的每個(gè)數(shù)據(jù)元素均有?個(gè)前驅(qū)
? 除了最后?個(gè)之外,結(jié)構(gòu)中的每個(gè)數(shù)據(jù)元素都有?個(gè)后繼.
#include
#include "stdlib.h"
#include"math.h"
#include"time.h"
#define MAXSIZE100/// 表大小
#define OK1/// 狀態(tài)
#define ERROR0/// 錯(cuò)誤
typedefintElemType;///元素類(lèi)型,假設(shè)為int
typedefintStatus;///狀態(tài):函數(shù)結(jié)果返回狀態(tài)
// 順序表的結(jié)構(gòu)
typedef?struct?{
? ? ElemType*data;///數(shù)據(jù)
?intlength;///長(zhǎng)度
} Sqlist;
//1、 初始化順序表
Status InitSqlist (Sqlist *L) {
? ? // 分配數(shù)據(jù)空間
L->data=malloc(sizeof(ElemType) *MAXSIZE);
?if(!L->data) {
? ? ? ? exit(ERROR);// 報(bào)錯(cuò)則退出
? ? }
? ? // 空白長(zhǎng)度為0
? ? L->length=0;
?returnOK;
}
// 2、順序表:數(shù)據(jù)插入
/*
?初始條件:順序線性表L已存在,1≤i≤ListLength(L);
?操作結(jié)果:在L中第i個(gè)位置之前插入新的數(shù)據(jù)元素e,L的長(zhǎng)度加1
?*/
Status InSertElem (Sqlist *L,?int?i, ElemType e) {
? ? // 位置是否合法
?if(i <0|| i > L->length) {
?returnERROR;
? ? }
? ? // 存儲(chǔ)空間是否已滿(mǎn)
?if(L->length>=MAXSIZE) {
?returnERROR;
? ? }
? ? // 插入位置是否在表尾
?if(i < L->length) {
? ? ? ? // 不在表尾,則移動(dòng)表,空出位置
?for(intj = L->length; j > i; j--) {
? ? ? ? ? ? // 插入位置以及之后的位置后移動(dòng)1位
? ? ? ? ? ? L->data[j] = L->data[j -1];
? ? ? ? }
? ? }
? ? // 插入新元素
? ? L->data[i] = e;
? ? // 表長(zhǎng)度加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;
? ? }
? ? // 被刪除元素后面的元素都往前移動(dòng)一位
?for(intj = i; j < L->length; j++) {
? ? ? ? L->data[j] = L->data[j +1];
? ? }
? ? // 長(zhǎng)度減少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、遍歷查找是否 存在摸個(gè)元素,存在則返回位置,否則返回-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 -- 長(zhǎng)度:%d\n", iStatus, L.length);
? ? // 插入數(shù)據(jù)
?for(intj =0; j <5;j++){
? ? ? ? iStatus =InSertElem(&L,0, j);
? ? }
? ? printf("插入數(shù)據(jù)L長(zhǎng)度: %d\n",L.length);
? ? // 輸出
? ? iStatus =TraverseList(L);
? ? // 取值
?intnum =2;
? ? GetElem(L, num -1, &e);
? ? printf("取第%d個(gè)元素值為: %d\n", num, e);
? ? // 刪除第2個(gè)元素
? ? 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;
}