線性表的順序表示和實現
線性表的定義和特點
定義:由n個數據特性相同的元素構成的有限序列。
特點:
- 存在唯一的一個被稱作“第一個”的數據元素。
- 存在唯一的一個被稱作“最后一個”的數據元素。
- 除第一個外,結構中的每個數據元素均只有一個前驅。
- 除最后一個外,結構中的每個元素均只有一個后驅。
線性表的類型定義
ADT List{
數據對象:D={ai|ai∈Element,i=1,2,...,n,n>=0}
數據關系:R={<ai-1,ai>|ai-1,ai∈D,i=2,...,n}
基本操作:
Initlist(&L)
操作結果:構造一個空表L
DestroyList(&L)
初始條件:線性表L已存在
操作結果:將L置為空表
ListEmpty(L)
初始條件:線性表L已存在
操作結果:若L為空表,返回true,否則返回false
GetElem(L,i,e)
初始條件:線性表L已存在,且1<=i<=ListLength(L)
操作結果:用e返回L中第i個數據元素的值
LocateElem(L,e)
初始條件:線性表L已存在
操作結果:返回L中第1個與e相同的元素的在L中的位置。若無則返回0
PriorElement(L,cur_e,&pre_e)
初始條件:線性表L已存在
操作結果:若cur_e是L的數據元素,且不是第一個,則用pre_e返回其前驅,否則操作失敗
NextElement(L,cur_e,&next_e)
初始條件:線性表L已存在
操作結果:若cur_e是L的數據元素,且不是最后一個,則用next_e返回其后繼,否則操作失敗
ListInsert(&L,i,e)
初始條件:線性表L已存在,且1<=i<=ListLength(L)+1
操作結果:在L中第i個位置之前插入新的數據元素e,L的長度加1
ListDelete(&L,i)
初始條件:線性表L存在且非空,且1<=i<=ListLength(L)
操作結果:刪除L中第i個元素,L的長度減1
TraverseLsit(L)
初始條件:線性表L已存在
操作結果:對線性表L進行遍歷
}ADT List
順序表實現圖書館里系統
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函數返回值類型,其值是函數結果狀態代碼。
typedef int ElemType; //ElemType 為可定義的數據類型,此設為int類型
#define MAXSIZE 100 //順序表可能達到的最大長度
struct Book {
string id;//ISBN
string name;//書名
double price;//定價
};
typedef struct {
Book *elem; //存儲空間的基地址
int length; //當前長度
} SqList;
Status InitList_Sq(SqList &L) { //算法2.1 順序表的初始化
//構造一個空的順序表L
L.elem = new Book[MAXSIZE]; //為順序表分配一個大小為MAXSIZE的數組空間
if (!L.elem)
exit(OVERFLOW); //存儲分配失敗退出
L.length = 0; //空表長度為0
return OK;
}
Status GetElem(SqList L, int i, Book &e) {//算法2.2 順序表的取值
if (i < 1 || i > L.length)
return ERROR; //判斷i值是否合理,若不合理,返回ERROR
e = L.elem[i - 1]; //elem[i-1]單元存儲第i個數據元素
return OK;
}
int LocateElem_Sq(SqList L, double e) { //算法2.3 順序表的查找
//順序表的查找
for (int i = 0; i < L.length; i++)
if (L.elem[i].price == e)
return i + 1;//查找成功,返回序號i+1
return 0;//查找失敗,返回0
}
Status ListInsert_Sq(SqList &L, int i, Book e) { //算法2.4 順序表的插入
//在順序表L中第i個位置之前插入新的元素e
//i值的合法范圍是1<=i<=L.length+1
if ((i < 1) || (i > L.length + 1))
return ERROR; //i值不合法
if (L.length == MAXSIZE)
return ERROR; //當前存儲空間已滿
for (int j = L.length - 1; j >= i - 1; j--)
L.elem[j + 1] = L.elem[j]; //插入位置及之后的元素后移
L.elem[i - 1] = e; //將新元素e放入第i個位置
++L.length; //表長增1
return OK;
}
Status ListDelete_Sq(SqList &L, int i) { //算法2.5 順序表的刪除
//在順序表L中刪除第i個元素,並用e返回其值
//i值的合法范圍是1<=i<=L.length
if ((i < 1) || (i > L.length))
return ERROR; //i值不合法
for (int j = i; j <= L.length-1; j++)
L.elem[j - 1] = L.elem[j]; //被刪除元素之后的元素前移
--L.length; //表長減1
return OK;
}
int main() {
SqList L;
int i = 0, temp, a, c, choose;
double price;
Book e;
string head_1, head_2, head_3;
cout << "1. 建立\n";
cout << "2. 輸入\n";
cout << "3. 取值\n";
cout << "4. 查找\n";
cout << "5. 插入\n";
cout << "6. 刪除\n";
cout << "7. 輸出\n";
cout << "0. 退出\n\n";
choose = -1;
while (choose != 0) {
cout << "請選擇:";
cin >> choose;
switch (choose) {
case 1://創建順序表
if (InitList_Sq(L))
cout << "成功建立順序表\n\n";
else
cout << "順序表建立失敗\n\n";
break;
case 2: {//順序表信息輸入
i = 0;
L.elem = new Book[MAXSIZE];
if (!L.elem)
exit(OVERFLOW);
L.length = 0;
fstream file;
file.open("book.txt");
if (!file) {
cout << "錯誤!未找到文件!" << endl;
exit(ERROR);
}
file >> head_1 >> head_2 >> head_3;
while (!file.eof()) {
file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
i++;
}
cout << "輸入 book.txt 信息完畢\n\n";
L.length = i;
file.close();
}
break;
case 3://順序表的取值
cout << "請輸入一個位置用來取值:\n";
cin >> i;
temp = GetElem(L, i, e);
if (temp != 0) {
cout << "查找成功\n";
cout << "第" << i << "本圖書的信息是:\n";
cout << left << setw(15) << e.id << "\t" << left << setw(50)
<< e.name << "\t" << left << setw(5) << e.price << endl
<< endl;
} else
cout << "查找失敗!位置超出范圍\n\n";
break;
case 4: //順序表的查找
cout << "請輸入所要查找價格:";
cin >> price;
temp = LocateElem_Sq(L, price);
if (temp != 0) {
cout << "查找成功\n";
cout << "該價格對應的書名為:" << L.elem[temp - 1].name << endl << endl;
} else
cout << "查找失敗!沒有這個價格對應的書籍\n\n";
break;
case 5: //順序表的插入
cout << "請輸入插入的位置和書本信息,包括:編號 書名 價格(用空格隔開):";
cin >> a;
cin >> e.id >> e.name >> e.price; //輸入a和b,a代表插入的位置,b代表插入的數值(書本信息)
if (ListInsert_Sq(L, a, e))
cout << "插入成功.\n\n";
else
cout << "插入失敗.\n\n";
break;
case 6: //順序表的刪除
cout << "請輸入所要刪除的書籍的位置:";
cin >> c;
if (ListDelete_Sq(L, c))
cout << "刪除成功.\n\n";
else
cout << "刪除失敗.\n\n";
break;
case 7: //順序表的輸出
cout << "當前圖書系統信息(順序表)讀出:\n";
for (i = 0; i < L.length; i++)
cout << left << setw(15) << L.elem[i].id << "\t" << left
<< setw(50) << L.elem[i].name << "\t" << left
<< setw(5) << L.elem[i].price << endl;
cout << endl;
break;
}
}
return 0;
}
算法分析
- 查找平均移動次數:(n+1)/2
- 插入平均移動次數:移動n-i+1,n/2
- 插入平均移動次數:移動n-i,(n-1)/2