1. 程式人生 > >考研資料結構複習之線性表

考研資料結構複習之線性表

線性表之順序表學習

#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() { } /*線性表初始化正確方法*/ void InitList(SeqList * L) { L->length = 0; } /*線性表初始化錯誤方法 void InitList(SeqList L) { L.length = 0; }*/ /*這裡的錯誤是因為初始化的時候需要注意到L的本身需要改變,可以使用指標,或者是引用型別.引用型別的程式碼如下: void InitList(SeqList &L) { L.length = 0; } 只需記住:不需要修改表L本身的時候就可以不用指標或者引用型別, 像查詢是否為空,只是得到資料(順序表的長度),而不是修改就可以直接使用例如bool ListEmpty(SeqList L)的傳參形式。 這是其實也是引數的傳遞的按照值傳遞和引用傳遞的區別。 不清楚的可以回去看一下教材。 再補充一點: void InitList(SeqList &L) { L.length = 0; }初始化的時候----使用時: SeqList L; InitList(L); void InitList(SeqList * L) { L->length = 0; }初始化的時候----使用時: SeqList L; InitList(&L); */
bool ListEmpty(SeqList L) { if (L.length==0) { return true; } return false; } /*按照序號來查詢元素*/ 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; } } /*查詢線性表中元素所在的位置*/ int GetLocateElem(SeqList L, DataType e) { for (int i = 0; i <L.length; i++) { if (L.list[i]==e) { return i + 1; } } return -1; } /*往線性表中插入資料(1)判斷線性表是否滿,插入位置是否合理, 可插入的位置注意到第一個和最後一個*/ 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; } } /*刪除線性表中的元素並且將刪除的元素的值賦給e (1)先判斷線性表是否為空 (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; }

測試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;
    }

這裡寫圖片描述