1. 實驗要求
- 編程實現順序表的以下基本操作:建立順序表,修改順序表,插入順序表,刪除順序表。
- 採用順序表結構編程實現:兩個集合的運算:交集/並集/差集。
2. 實驗目的
通過該實驗,深入理解順序表的邏輯結構、物理結構等概念,掌握順序表基本操作的編程實現,注意順序表插入、刪除等操作過程中數據元素的移動現象,學生編寫程序時,要考慮程序的健壯性,熟練掌握通過函數參數返回函數結果的辦法。
3. 實驗內容
編程實現順序表下教材第二章定義的線性表的基本操作,最好用菜單形式對應各個操作,使其變成一個完整的小軟件。
4. 實驗步驟
- 編寫程序框架,利用while循環輸入操作代碼,利用switch選擇語句對輸入的代碼進行判斷,並進行相應操作。
- 編寫各個操作的具體函數:
1>清空線性表:L.length = 0;
2>判斷線性表是否爲空:L.length是否等於0;
3>求線性表長度:L.length;
4>獲取線性表指定位置元素:位置是否合法,L.elem[i-1];
5>求前驅:位置是否合法,L.elem[i-2];
6>求後繼:位置是否合法,L.elem[i];
7>在線性表指定位置插入元素:位置是否合法,循環執行L.elem[i]= L.elem[i-1]語句 ,L.elem[i-1]=e;
8>刪除線性表指定位置元素:位置是否合法,循環執行L.elem[i-1]= L.elem[i]語句;
9>顯示線性表:for循環輸出元素;
10>定位:for循環遍歷元素,if判斷是否存在要查找元素。
5. 實驗數據測試
通過菜單調用各個操作,測試點:
- 插入數據(位置, 數據),要測插入位置不合法的情況(0,1)、(2,1),正確插入4個數據(1,2)、(1,1)、(3,3);
- 顯示順序表中的數據,屏幕輸出1, 2, 3;
- 判空,屏幕輸出順便表非空;
- 順便表長度,屏幕輸出3;
- 獲取指定位置元素,要測指定位置在【1,3】範圍之外的情況和之內的情況;
- 定位,輸入:4, 輸出:不存在,輸入2,輸出位置爲2;
- 求直接前驅,要測求第一個元素的前驅、不存在順序表中的元素的直接前驅,其他元素的直接前驅;
- 求直接後繼,要測最後一個元素的後繼、不存在順序表中的元素的直接後繼,其他元素的直接後繼;
- 刪除,要測位置在【1,3】範圍之外的情況和之內的情況;
- 清空操作後再測長度;
6. 實驗代碼
#include <iostream>
using namespace std;
typedef int ElemType;
#define MAXSIZE 100
typedef struct
{
ElemType *elem;//存儲空間的基地址
int length; //當前長度,數據元素的個數
}SqList; //順序表的結構類型爲SqList
//初始化
void InitList(SqList &L);
//清空順序表
void ClearList(SqList &L);
//判斷線性表是否爲空
void EmptyList(SqList &L);
//求線性表長度
void LenList(SqList &L);
//獲取線性表指定位置元素
void ChooseList(SqList &L,int i);
//求前驅
void BeforeList(SqList &L,int i);
//求後繼
void AfterList(SqList &L,int i);
//在線性表指定位置插入元素
void InsertList(SqList &L,int i,int j);
//刪除線性表指定位置元素
void DeleteList(SqList &L,int i);
//顯示線性表
void DisplayList(SqList &L);
//定位
void LocateList(SqList &L,int i);
int main()
{
SqList L;
InitList(L);
int i,j;
int index = 1;
while(index > 0)
{
cout << "1----清空線性表" << endl;
cout << "2----判斷線性表是否爲空" << endl;
cout << "3----求線性表長度" << endl;
cout << "4----獲取線性表指定位置元素" << endl;
cout << "5----求前驅" << endl;
cout << "6----求後繼" << endl;
cout << "7----在線性表指定位置插入元素" << endl;
cout << "8----刪除線性表指定位置元素" << endl;
cout << "9----顯示線性表" << endl;
cout << "10---定位" << endl;
cout << " 退出,輸入一個負數!" << endl;
cout << "請輸入操作代碼:" << endl;
cin >> index;
switch(index)
{
case 1:
ClearList(L);
break;
case 2:
EmptyList(L);
break;
case 3:
LenList(L);
break;
case 4:
cout << "請輸入你要獲取的位置:";
cin >> i;
ChooseList(L,i);
break;
case 5:
cout << "請輸入指定位置:";
cin >> i;
BeforeList(L,i);
break;
case 6:
cout << "請輸入指定位置:";
cin >> i;
AfterList(L,i);
break;
case 7:
cout << "請輸入要插入元素的位置:";
cin >> i;
cout << "請輸入要插入的元素:";
cin >> j;
InsertList(L,i,j);
break;
case 8:
cout << "請輸入要刪除元素的位置:";
cin >> i;
DeleteList(L,i);
break;
case 9:
DisplayList(L);
break;
case 10:
cout << "請輸入要定位元素的值:";
cin >> i;
LocateList(L,i);
break;
default:
cout << "輸入數字不合法!" << endl;
}
cout << endl;
}
return 0;
}
void InitList(SqList &L)
{
L.elem = new ElemType[MAXSIZE];
if(!L.elem)
cout << "初始化失敗!" << endl;
L.length = 0;
}
void ClearList(SqList &L)
{
if(L.length == 0)
cout << "線性表是空的!" << endl;
else
{
L.length = 0;
cout << "順序表已經清空!" << endl;
}
}
void EmptyList(SqList &L)
{
if(L.length == 0)
cout << "線性表爲空!" << endl;
else
cout << "線性表不爲空!" << endl;
}
void LenList(SqList &L)
{
cout << "線性表的長度爲:" << L.length << endl;
}
void ChooseList(SqList &L,int i)
{
if(i>0 && i<= L.length)
cout << L.elem[i-1] << endl;
else
cout << "你輸入的位置不合法!" << endl;
}
void BeforeList(SqList &L,int i)
{
if(i>1 && i<=L.length)
cout << "指定位置前驅是:" << L.elem[i-2] << endl;
else if(i == 1)
cout << "該位置無前驅!" << endl;
else
cout << "輸入位置不合法!" << endl;
}
void AfterList(SqList &L,int i)
{
if(i>0 && i<L.length)
cout << "該位置的後繼是:" << L.elem[i] << endl;
else if(i == L.length)
cout << "該位置無後繼!" << endl;
else
cout << "輸入位置不合法!" << endl;
}
void InsertList(SqList &L,int i,int j)
{
if(i>0 && i<=L.length+1 && L.length < MAXSIZE)
{
for(int q=L.length;q>=i;q--)
L.elem[q] = L.elem[q-1];
L.elem[i-1] = j;
++L.length;
}
else
cout << "輸入不合法!" << endl;
}
void DeleteList(SqList &L,int i)
{
if(i>0 && i<=L.length)
{
for(int k=i;k<L.length;k++)
L.elem[k-1] = L.elem[k];
--L.length;
}
else
cout << "輸入位置不合法!" << endl;
}
void DisplayList(SqList &L)
{
if(L.length > 0)
for(int k=0;k<L.length;k++)
cout << L.elem[k] << " ";
}
void LocateList(SqList &L,int i)
{
bool isExit = false;
for(int k=0;k<L.length;k++)
{
if(L.elem[k] == i)
{
cout << "指定元素所在位置是:" << k+1 << endl;
isExit = true;
}
}
if(!isExit)
cout << "線性表中無此元素!" << endl;
}
7. 實驗總結
- 判斷表是否爲空,清空表操作可直接利用表的length屬性。
- 表的第一個元素無直接前驅,最後一個元素無直接後繼。
- 對錶進行插入操作時應注意插入位置的取值範圍。進行插入操作時應由表尾開始操作。