順序表的基本操作及實現(二)

這個順序表實現數組採用的靜態分配方法,一旦空間佔滿,再加入新的數據是將會產生溢出,進而導致程序崩潰!
這篇中,參數傳遞的不是指針,而是引用,保存爲源文件時要以.cpp結尾。
文末有完整的代碼示例。
順序表從存儲類型描述如下:

#define MaxSize 50
typedef int ElemType;

typedef struct{
    ElemType data[MaxSize];
    int length;
}SqList;

這裏給出了順序表的一些基本操作方法:

void InitList(SqList &L); //初始化順序表
bool InsertList(SqList &L, int i, ElemType e);//在順序表的第i個位置插入元素
bool DeletList(SqList &L, int i, ElemType &e); //刪除順序表達的第i個元素的位置,並用e返回
int LocateElem(SqList L, ElemType e); //按值順序查找元素,並返回其位序
ElemType GetElem(SqList L, int i, ElemType &e); //查找順序表中指定位置的元素,並用e返回

對應的實現過程如下代碼:

void InitList(SqList &L)
{
    L.length = 0;
}

bool InsertList(SqList &L, int i, ElemType e)
{
    int j=0;
    if(i < 1 || i > L.length + 1)
    {
        return false;
    }
    if(L.length >= MaxSize)
    {
        return false;
    }

    for(j = L.length; j >= i; j--)
    {
        L.data[j] = L.data[j-1];
    }
    L.data[i-1] = e;
    L.length++;
    return true;
}

bool DeletList(SqList &L, int i, ElemType &e)
{
    int j;
    if(i < 1 || i > L.length + 1)
    {
        return false;
    }
    e = L.data[i-1];
    for(j = i; j < L.length; j++)
    {
        L.data[j-1] = L.data[j];
    }
    L.length--;
    return true;
}

int LocateElem(SqList L, ElemType e)
{
    int i;
    for(i = 0; i < L.length; i++)
    {
        if(L.data[i] == e)
        {
            return i+1;
        }
    }
    return 0;
}

ElemType GetElem(SqList L, int i, ElemType &e)
{
    if(i < 0 || i > L.length - 1)
    {
        return false;
    }
    e = L.data[i];
    return 1;
}

寫一個簡單的main()函數測試,如下:

int main()
{
    int i = 1, s, e = 0;
    SqList sqList;
    InitList(sqList);

    scanf("%d", &s);
    while(s != 9999)
    {
        InsertList(sqList, i++, s);
        scanf("%d", &s);
    }

    printf("所有元素:");
    for(i = 0; i < sqList.length; i++)
    {
        printf("%d ", sqList.data[i]);
    }

    DeletList(sqList, 2, e);
    printf("\n刪除的元素爲:%d", e);

    printf("\n所有元素:");
    for(i = 0; i < sqList.length; i++)
    {
        printf("%d ", sqList.data[i]);
    }

    printf("\n元素值爲3的位序:%d \n", LocateElem(sqList, 3));

    GetElem(sqList, 2, e);
    printf("順序表中位置爲2的元素值:%d \n", e);

    return 0;
}

運行結果如下:
這是順序表的測試運行結果
完整的代碼如下:

#include "stdio.h"

#define MaxSize 50
typedef int ElemType;

typedef struct{
    ElemType data[MaxSize];
    int length;
}SqList;

void InitList(SqList &L); //初始化順序表
bool InsertList(SqList &L, int i, ElemType e);//在順序表的第i個位置插入元素
bool DeletList(SqList &L, int i, ElemType &e); //刪除順序表達的第i個元素的位置,並用e返回
int LocateElem(SqList L, ElemType e); //按值順序查找元素,並返回其位序
ElemType GetElem(SqList L, int i, ElemType &e); //查找順序表中指定位置的元素,並用e返回


int main()
{
    int i = 1, s, e = 0;
    SqList sqList;
    InitList(sqList);

    scanf("%d", &s);
    while(s != 9999)
    {
        InsertList(sqList, i++, s);
        scanf("%d", &s);
    }

    printf("所有元素:");
    for(i = 0; i < sqList.length; i++)
    {
        printf("%d ", sqList.data[i]);
    }

    DeletList(sqList, 2, e);
    printf("\n刪除的元素爲:%d", e);

    printf("\n所有元素:");
    for(i = 0; i < sqList.length; i++)
    {
        printf("%d ", sqList.data[i]);
    }

    printf("\n元素值爲3的位序:%d \n", LocateElem(sqList, 3));

    GetElem(sqList, 2, e);
    printf("順序表中位置爲2的元素值:%d \n", e);
    
    return 0;
}

void InitList(SqList &L)
{
    L.length = 0;
}

bool InsertList(SqList &L, int i, ElemType e)
{
    int j=0;
    if(i < 1 || i > L.length + 1)
    {
        return false;
    }
    if(L.length >= MaxSize)
    {
        return false;
    }
    for(j = L.length; j >= i; j--)
    {
        L.data[j] = L.data[j-1];
    }
    L.data[i-1] = e;
    L.length++;
    return true;
}

bool DeletList(SqList &L, int i, ElemType &e)
{
    int j;
    if(i < 1 || i > L.length + 1)
    {
        return false;
    }
    e = L.data[i-1];
    for(j = i; j < L.length; j++)
    {
        L.data[j-1] = L.data[j];
    }
    L.length--;
    return true;
}

int LocateElem(SqList L, ElemType e)
{
    int i;
    for(i = 0; i < L.length; i++)
    {
        if(L.data[i] == e)
        {
            return i+1;
        }
    }
    return 0;
}

ElemType GetElem(SqList L, int i, ElemType &e)
{
    if(i < 0 || i > L.length - 1)
    {
        return false;
    }
    e = L.data[i];
    return 1;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章