Loading

ch2 顺序表的实现

 

头文件SqList.h

#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 50
typedef char ElemType;
// 结构体定义
typedef struct
{
    ElemType data[MAXSIZE];
    int length;
} SqList;

// 参数:SqList* &L 结构体指针+引用
//  函数声明

// 创建顺序表
void CreateList(SqList *&L, ElemType a[], int n);

// 初始化顺序表L
void InitList(SqList *&L);

// 释放顺序表L
void DestoryList(SqList *L);

// 判断线性表L是否为空
bool ListEmpty(SqList *L);

// 求线性表L的长度
int ListLength(SqList *L);

// 输出线性表
void DispList(SqList *L);

// 求线性表中第i个元素的值
// i:1~L.length
bool GetElem(SqList *L, int i, ElemType &e);

// 查找第一个值为e的元素序号
// 序号范围:1~L.length
int LocateElem(SqList *L, ElemType e);

// 在L的位置i处插入元素e
// 位置i的范围:1~L.length
bool ListInsert(SqList *&L, int i, ElemType e);

// 删除第i个元素,用e返回其值
// 位置i的范围:1~L.length
bool ListDelete(SqList *&L, int i, ElemType &e);

 

#include "sqlist.h"
void CreateList(SqList *&L, ElemType a[], int n)
{
    int i = 0;
    L = (SqList *)malloc(sizeof(SqList));
    while (i < n)
    {
        L->data[i] = a[i];
    }
    L->length = n;
}

void InitList(SqList *&L)
{
    L = (SqList *)malloc(sizeof(SqList));
    L->length = 0;
}

void DestoryList(SqList *L)
{
    free(L);
}

bool ListEmpty(SqList *L)
{
    return L->length == 0;
}

int ListLength(SqList *L)
{
    return L->length;
}

void DispList(SqList *L)
{
    int i = 0;
    while (i < L->length)
    {
        printf("%c ", L->data[i++]);
    }
}

bool GetElem(SqList *L, int i, ElemType &e)
{
    if (i < 1 || i > L->length)
        return false;
    e = L->data[i - 1]; // 第i个元素的下标是i-1
    return true;
}

int LocateElem(SqList *L, ElemType e)
{
    int i = 0;
    while (i < L->length)
    {
        if (L->data[i] == e)
            return i + 1; // 下标+1=序号
        i++;
    }
    if (i >= L->length)
        return 0; // 表示L中无值为e的元素
}

bool ListInsert(SqList *&L, int i, ElemType e)
{
    if (i < 1 || i > L->length + 1)
        return false;
    i--; // 序号转为下标
    // 逐个元素后移,腾出下标i的位置
    for (int j = L->length - 1; j >= i; j--)
    {
        L->data[j + 1] = L->data[j];
    }
    L->data[i] = e;
    L->length++;
    return true;
}

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

源文件SQList.cpp

#include "sqlist.cpp"

int main()
{
    SqList *L;
    ElemType e;
    printf("-----------顺序表的运算测试------------\n");
    printf("1)初始化顺序表L\n");
    InitList(L);

    printf("2)依次插入a,b,c,d,e元素\n");
    ListInsert(L, 1, 'a');
    ListInsert(L, 2, 'b');
    ListInsert(L, 3, 'c');
    ListInsert(L, 4, 'd');
    ListInsert(L, 5, 'e');

    printf("3)输出顺序表L:");
    DispList(L);

    printf("\n4)顺序表L的长度:%d\n", ListLength(L));

    printf("5)顺序表是否为空?%s\n", (ListEmpty(L) ? "空" : "非空"));

    GetElem(L, 3, e);
    printf("6)顺序表L的第3个元素:%c\n", e);

    printf("7)元素c的位置:%d\n", LocateElem(L, 'c'));

    printf("8)在第4个位置插入元素f\n");
    ListInsert(L, 4, 'f');
    printf("9)输出顺序表L:");
    DispList(L);

    printf("\n10)删除L的第4个元素\n");
    ListDelete(L, 4, e);
    printf("11)输出顺序表L:");
    DispList(L);

    printf("\n12)释放顺序表L\n");
    DestoryList(L);

    return 0;
}

测试输出结果

-----------顺序表的运算测试------------
1)初始化顺序表L
2)依次插入a,b,c,d,e元素
3)输出顺序表L:a b c d e
4)顺序表L的长度:5
5)顺序表是否为空?非空
6)顺序表L的第3个元素:c
7)元素c的位置:3
8)在第4个位置插入元素f
9)输出顺序表L:a b c f d e
10)删除L的第4个元素
11)输出顺序表L:a b c d e
12)释放顺序表L

 

posted @ 2024-05-07 12:06  景的博客  阅读(26)  评论(0)    收藏  举报