線(xiàn)性表之順序表學(xué)習(xí)
#pragma once
#define MaxListSize 100
typedef int DataType;
class SeqListTest
{
public:
SeqListTest();
~SeqListTest();
};
typedef struct
{
DataType list[MaxListSize];
int length;
}SeqList;
void InitList(SeqList *L);
bool ListEmpty(SeqList L);
int GetElemByNum(SeqList L, int i, DataType *e);
int GetLocateElem(SeqList L, DataType e);
int InsertList(SeqList *L, int i, DataType e);
int DeleteList(SeqList *L, int i, DataType *e);
int ListLength(SeqList L);
void ClearList(SeqList *L);
void PrintList(SeqList *L);
#include "SeqListTest.h"
#include<iostream>
using namespace std;
SeqListTest::SeqListTest()
{
}
SeqListTest::~SeqListTest()
{
}
/*線(xiàn)性表初始化正確方法*/
void InitList(SeqList * L)
{
L->length = 0;
}
/*線(xiàn)性表初始化錯(cuò)誤方法
void InitList(SeqList L)
{
L.length = 0;
}*/
/*這里的錯(cuò)誤是因?yàn)槌跏蓟臅r(shí)候需要注意到L的本身需要改變,可以使用指針,或者是引用類(lèi)型.引用類(lèi)型的代碼如下:
void InitList(SeqList &L)
{
L.length = 0;
}
只需記住:不需要修改表L本身的時(shí)候就可以不用指針或者引用類(lèi)型,
像查詢(xún)是否為空,只是得到數(shù)據(jù)(順序表的長(zhǎng)度),而不是修改就可以直接使用例如bool ListEmpty(SeqList L)的傳參形式。
這是其實(shí)也是參數(shù)的傳遞的按照值傳遞和引用傳遞的區(qū)別。
不清楚的可以回去看一下教材。
再補(bǔ)充一點(diǎn):
void InitList(SeqList &L)
{
L.length = 0;
}初始化的時(shí)候----使用時(shí):
SeqList L;
InitList(L);
void InitList(SeqList * L)
{
L->length = 0;
}初始化的時(shí)候----使用時(shí):
SeqList L;
InitList(&L);
此時(shí)的&是取得位置的符號(hào)。
*/
bool ListEmpty(SeqList L)
{
if (L.length==0)
{
return true;
}
return false;
}
/*按照序號(hào)來(lái)查找元素*/
int GetElemByNum(SeqList L, int i, DataType * e)
{
if (ListEmpty(L))
{
return -1;
}
else if (i<1&&i>L.length)
{
return -1;
}
else
{
*e = L.list[i - 1];
return 1;
}
}
/*查找線(xiàn)性表中元素所在的位置*/
int GetLocateElem(SeqList L, DataType e)
{
for (int i = 0; i <L.length; i++)
{
if (L.list[i]==e)
{
return i + 1;
}
}
return -1;
}
/*往線(xiàn)性表中插入數(shù)據(jù)(1)判斷線(xiàn)性表是否滿(mǎn),插入位置是否合理,
可插入的位置注意到第一個(gè)和最后一個(gè)*/
int InsertList(SeqList * L, int i, DataType e)
{
int j;
if (L->length==MaxListSize)
{
return -1;
}
/*插入位置不合法*/
else if(i<1&&i>L->length+1)
{
return -1;
}
else
{
for ( j = L->length; j >i; j--)
{
L->list[j ] = L->list[j-1];
}
L->list[i-1] = e;
L->length = L->length + 1;
return 1;
}
}
/*刪除線(xiàn)性表中的元素并且將刪除的元素的值賦給e
(1)先判斷線(xiàn)性表是否為空
(2)刪除位置是否合法*/
int DeleteList(SeqList * L, int i, DataType * e)
{
int j;
if (ListEmpty(*L))
{
return -1;
}
else if (i<1&&i>L->length)
{
return -1;
}
else
{
*e=L->list[i - 1];
for ( j= i; j< L->length; j++)
{
L->list[j - 1] = L->list[j];
}
L->length = L->length - 1;
return -1;
}
}
int ListLength(SeqList L)
{
return L.length;
}
void ClearList(SeqList * L)
{
L->length = 0;
}
void PrintList(SeqList * L)
{
for (int i = 0; i < L->length; i++)
{
cout << L->list[i] << " ";
}
cout << endl;
}
測(cè)試main:
int main() {
SeqList L;
InitList(&L);
InsertList(&L, 1, 1);
InsertList(&L, 2, 2);
InsertList(&L, 3, 3);
InsertList(&L, 4, 4);
InsertList(&L, 5, 5);
PrintList(&L);
cout << ListLength(L) << endl;
DataType e;
DeleteList(&L, 1, &e);
cout << e << endl;
PrintList(&L);
cout << GetLocateElem(L, 3) << endl;
GetElemByNum (L,2,&e);
cout << e << endl;
cout << ListLength(L) << endl;
system("PAUSE");
return 0;
}
這里寫(xiě)圖片描述