線性表的順序存儲結構(一)

不管三七二十一先貼代碼
申明一下代碼是參照李春葆《數據結構教程》修改

#include <stdio.h>
#include <stdlib.h>

//C語言沒有boolean
typedef int bool;  //這裏的分號不要忘記了,宏定義不需要加分號
#define true  1    
#define false 0

//利用宏來模擬參數默認值
//C語言既不支持重載也不支持默認參數
#define DEFARG(name, defval) ((#name[0]) ? (name + 0 ) : defval)
#define DeleteElem1(lsit, index, arg0) DeleteElem(lsit, index, DEFARG(arg0, NULL))

#define MAXSIZE 50

typedef int ElemType;
typedef struct
{
    ElemType Elem[MAXSIZE];
    int Size;     //容量
}List;

//約定:index爲物理位置序號
bool CreateList(List* &L, ElemType arr[], int index)
{
    int i = 0;

    if (index < 1 || index > MAXSIZE) {
        printf("Index Outsize\n");
        return false;
    }

    (*L) = (List*)malloc(sizeof(List));
    if (*L == NULL) {
        printf("malloc failed!\n");
        return false;
    }

    /*
    在這裏還犯一個錯誤,書上bool CreateList(List* &L, ElemType arr[], int index)用C編譯不過,然後我就改了,
    bool CreateList(List* L, ElemType arr[], int index)
我去掉了引用&,是這樣想的,malloc一個新的地址,然後改變這個新地址的內容,然後把這個新地址賦值給L,就OK了,
想的很簡單,然後就杯具了,實參對象的內容沒有任何變化,開始還一直想不明白,直到問了大牛。
我是傳了一個形參指針,一般形參指針只能改變它所指向內存地址裏的內容,但是現在我們malloc了一個
新地址,並且對這個新地址做了初始化,我們真正要做的是改變指針的指向,使實參對象指向一個新的地址。
一級指針只能改變它所指向的變量的值,要想改變一級指針指向需要傳一個二級指針或者一級指針的引用,
通過它改變一級指針的值。

    for (; i < index; ++i) {
        (*L)->Elem[i] = arr[i];
    }
    (*L)->Size = index;

    return true;
}

void DispList(List *L)
{
    int i = 0;
    for (; i < L->Size; i++) {
        printf("%d ", L->Elem[i]);
    }
    printf("\n");
}

bool IsEmpty(List *L)
{
    return ((L->Size == 0) ? true : false);
}

int GetSize(List* L)
{
    return L->Size;
}

//約定:index爲物理位置
bool GetElem(List* L, int index, ElemType* e)
{
    //List本身不能爲空,且查找不能越界
    if (L->Size == 0 || index < 1 || index > L->Size) {
        *e = -999999;
        return false;
    }

    index--;  //轉換爲邏輯序號
    *e = L->Elem[index];

    return true;
}

//返回線性表中所找到的第一個元素e的物理位置序號
bool LocatElem(List* L, int* index, ElemType e)
{
    int i = 0;
    for (; i < L->Size; ++i) {
        if (L->Elem[i] == e) {
            *index = (++i);
            return true;
        }
    }

    *index = -999999;
    return false;
}

//約定:InsetList是將元素插入到index位置
//e插入元素
bool InsertList(List* L, int index, ElemType e)
{
    //插入的線性表不能滿,且插入位置最小爲1,最大爲L->Size+1.
    if (L->Size == MAXSIZE || index < 1 || index > L->Size + 1) {
        return false;
    }

#if 0
    int i = L->Size;//這裏不用轉換成邏輯序號,因爲我們要往後移一位
    index--;        //轉換爲邏輯序號
    for (; i > index; --i)
        L->Elem[i] = L->Elem[i - 1];
    /*執行順序是:初始化--判斷--函數體——然後for循環的第3句--判斷——函數體---for循環的第三句--判斷……
    最後一次循環操作是i = index + 1, 將index處的內容賦值給index + 1處,然後執行for循環第三句:減一,判斷不滿足條件循環結束*/
#endif

#if 1
    //若插入數據不在標尾
    int j;
    index--;   //轉換爲邏輯序號
    if (index < L->Size) {
        for (j = L->Size - 1; j >= index; --j)
            L->Elem[j + 1] = L->Elem[j];
    }

#endif

    L->Elem[index] = e;
    L->Size++;

    return true;
}

//約定:刪除index(物理序號)處的元素,由e返回
bool DeleteElem(List* L, int index, ElemType* e)
{
    if (L->Size == 0 || index < 1 || index > L->Size) {
        return false;
    }

    int i = index - 1;
    for (; i < L->Size; ++i)
        L->Elem[i] = L->Elem[i + 1];
    L->Size--;

    return true;
}


int main()
{
    ElemType arr[] = { 1, 2, 3, 4, 5, 6, 7 };
    List *lsit;

    CreateList(&lsit, arr, 7);
    DispList(lsit);

    printf("GetSize():%d\n", GetSize(lsit));

    printf("Insert:\n");
    InsertList(lsit, 2, 10);
    DispList(lsit);
    InsertList(lsit, 1, 22);
    DispList(lsit);
    InsertList(lsit, 7, 278);
    DispList(lsit);
    InsertList(lsit, 10, 92);
    DispList(lsit);

    printf("\nDelete:\n");
    DeleteElem1(lsit, 1,);
    DispList(lsit);
    DeleteElem1(lsit, 3, );
    DispList(lsit);
    DeleteElem1(lsit, 2, );
    DispList(lsit);

    printf("IsEmpty():%d\n", IsEmpty(lsit));

    printf("\nGetElem:\n");
    ElemType e;
    GetElem(lsit, 3, &e);
    printf("GetElem(3):%d\n", e);

    ElemType e1;
    GetElem(lsit, 7, &e1);
    printf("GetElem(7):%d\n", e1);

    ElemType e2;
    GetElem(lsit, 21, &e2);
    printf("GetElem(21):%d\n", e2);

    printf("\nLocatElem:\n");
    int index1;
    LocatElem(lsit, &index1, 1);
    printf("LocatElem(1):%d\n", index1);

    int index2;
    LocatElem(lsit, &index2, 3);
    printf("LocatElem(3):%d\n", index2);

    int index3;
    LocatElem(lsit, &index3, 92);
    printf("LocatElem(92):%d\n", index3);

    int index4;
    LocatElem(lsit, &index4, 31);
    printf("LocatElem(31):%d\n", index4);

    return 0;
}

注:本人正在學習狀態,文中多有引用,也有解釋紕漏之處,敬請包涵、指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章