顺序表代码实现

发布时间:2024年01月19日

头插法插入元素

过程
头插法

代码

//头插法
void insertHeadSeqList(SeqList *list, int data) {
    if (list->size == list->length) {
        expandSeqList(list);
    }
    for (int i = list->size; i > 0; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[0] = data;
    list->size++;
}

尾插法插入元素

过程
尾插法

代码

//尾插法
void insertTailSeqList(SeqList *list, int data) {
    if (list->size == list->length) {
        expandSeqList(list);
    }
    list->data[list->size++] = data;
}

指定位置插入

过程
指定位置插入

代码

//指定下标插入
void insertSeqList(SeqList *list, int data, int index) {
    if (index < 0 || index > list->size) {
        printf("插入失败!\n");
        return;
    }
    if (list->size == list->length) {
        expandSeqList(list);
    }
    for (int i = list->size; i > index; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[index] = data;
    list->size++;
}

指定元素删除

过程
指定元素删除

代码

//指定元素删除
void deleteDataSeqList(SeqList *list, int data) {
    for (int i = 0; i < list->size; ++i) {
        if (list->data[i] == data) {
            for (int j = i; j < list->size - 1; j++) {
                list->data[j] = list->data[j + 1];
            }
            list->size--;
            i--;
        }
    }
}

指定位置删除

过程
指定位置删除
代码

//指定下标删除
void deleteIndexSeqList(SeqList *list, int index) {
    if (index < 0 || index > list->size - 1) {
        printf("删除失败!\n");
        return;
    }
    for (int i = index; i < list->size - 1; i++) {
        list->data[i] = list->data[i + 1];
    }
    list->size--;
}

完整代码实现

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

//顺序表
typedef struct SeqList {
    int *data; //数据
    int size; //有效数据大小
    int length; //当前总容量长度
} SeqList;

//初始化
void initSeqList(SeqList *list) {
    list->size = 0;
    list->length = 10;
    list->data = (int *) malloc(sizeof(int) * list->length);
}

//扩容
void expandSeqList(SeqList *list) {
    list->length += 10;
    int *data = (int *) realloc(list->data, sizeof(int) * list->length);
    list->data = data;
}

//头插法
void insertHeadSeqList(SeqList *list, int data) {
    if (list->size == list->length) {
        expandSeqList(list);
    }
    for (int i = list->size; i > 0; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[0] = data;
    list->size++;
}

//尾插法
void insertTailSeqList(SeqList *list, int data) {
    if (list->size == list->length) {
        expandSeqList(list);
    }
    list->data[list->size++] = data;
}

//指定下标插入
void insertSeqList(SeqList *list, int data, int index) {
    if (index < 0 || index > list->size) {
        printf("插入失败!\n");
        return;
    }
    if (list->size == list->length) {
        expandSeqList(list);
    }
    for (int i = list->size; i > index; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[index] = data;
    list->size++;
}

//指定元素删除
void deleteDataSeqList(SeqList *list, int data) {
    for (int i = 0; i < list->size; ++i) {
        if (list->data[i] == data) {
            for (int j = i; j < list->size - 1; j++) {
                list->data[j] = list->data[j + 1];
            }
            list->size--;
            i--;
        }
    }
}

//指定下标删除
void deleteIndexSeqList(SeqList *list, int index) {
    if (index < 0 || index > list->size - 1) {
        printf("删除失败!\n");
        return;
    }
    for (int i = index; i < list->size - 1; i++) {
        list->data[i] = list->data[i + 1];
    }
    list->size--;
}

//打印顺序表
void printSeqList(SeqList *list) {
    for (int i = 0; i < list->size; ++i) {
        printf("%d ", list->data[i]);
    }
    printf("\n");
    printf("有效数据大小:%d\n", list->size);
    printf("当前容量大小:%d\n", list->length);
}

//测试代码
void test() {
    SeqList list;
    initSeqList(&list);
    int sel, data, index;
    while (true) {
        printf("1.头插法插入\n2.尾插法插入\n3.指定下标插入\n4.指定元素删除\n5.指定下标删除\n6.打印顺序表\n");
        scanf("%d", &sel);
        switch (sel) {
            case 1: {
                printf("元素:");
                scanf("%d", &data);
                insertHeadSeqList(&list, data);
                break;
            }
            case 2: {
                printf("元素:");
                scanf("%d", &data);
                insertTailSeqList(&list, data);
                break;
            }
            case 3: {
                printf("元素:");
                scanf("%d", &data);
                printf("下标:");
                scanf("%d", &index);
                insertSeqList(&list, data, index);
                break;
            }
            case 4: {
                printf("元素:");
                scanf("%d", &data);
                deleteDataSeqList(&list, data);
                break;
            }
            case 5: {
                printf("下标:");
                scanf("%d", &index);
                deleteIndexSeqList(&list, index);
                break;
            }
            case 6: {
                printSeqList(&list);
                break;
            }
            default:
                return;
        }
        system("pause");
        system("cls");
    }
}

int main() {
    test();
    return 0;
}
文章来源:https://blog.csdn.net/qq_52656735/article/details/135648758
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。