用c语言编写程序加简单流程图

通过键盘输入数据建立一个线性表,依次输入元素25,21,46,90,12,98,并输出该线性表。设计一个菜单界面:根据屏幕菜单的选择,进行数据的插入、删除和查找,并在插入或删除数据后,再输出线性表。如,在第2个位置上插入元素43,然后输出顺序表。删除顺序表第4个元素,输出改变的顺序表。

望采纳

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

#define MAXSIZE 100   // 线性表最大长度

typedef struct {
    int data[MAXSIZE];  // 存储数据元素
    int length;         // 线性表长度
} SqList;

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

void InsertList(SqList *L, int i, int e) {
    if (i < 1 || i > L->length + 1) {
        printf("插入位置不合法\n");
        return ;
    }
    if (L->length == MAXSIZE) {
        printf("顺序表已满,不能插入\n");
        return ;
    }
    int j;
    for (j = L->length; j >= i; j--) {
        L->data[j] = L->data[j - 1];
    }
    L->data[i - 1] = e;
    L->length++;
}

void DeleteList(SqList *L, int i) {
    if (i < 1 || i > L->length) {
        printf("删除位置不合法\n");
        return ;
    }
    int j;
    for (j = i; j < L->length; j++) {
        L->data[j - 1] = L->data[j];
    }
    L->length--;
}

int SearchList(SqList *L, int e) {
    int i;
    for (i = 0; i < L->length; i++) {
        if (L->data[i] == e) {
            return i + 1;
        }
    }
    return 0;
}

void PrintList(SqList *L) {
    int i;
    printf("当前线性表内容为:\n");
    for (i = 0; i < L->length; i++) {
        printf("%d ", L->data[i]);
    }
    printf("\n");
}

int main() {
    SqList L;
    InitList(&L);   // 初始化线性表
    InsertList(&L, 1, 25);
    InsertList(&L, 2, 21);
    InsertList(&L, 3, 46);
    InsertList(&L, 4, 90);
    InsertList(&L, 5, 12);
    InsertList(&L, 6, 98);
    PrintList(&L);  // 输出顺序表
    
    int choice, pos, elem;
    while (1) {
        printf("\n1.插入元素\n");
        printf("2.删除元素\n");
        printf("3.查找元素\n");
        printf("0.退出程序\n");
        printf("请输入您的选择:");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("请输入要插入的位置和元素值:");
                scanf("%d %d", &pos, &elem);
                InsertList(&L, pos, elem);
                PrintList(&L);
                break;
            case 2:
                printf("请输入要删除的元素位置:");
                scanf("%d", &pos);
                DeleteList(&L, pos);
                PrintList(&L);
                break;
            case 3:
                printf("请输入要查找的元素值:");
                scanf("%d", &elem);
                pos = SearchList(&L, elem);
                if (pos != 0) {
                    printf("元素 %d 在顺序表中的位置为%d\n", elem, pos);
                } else {
                    printf("元素 %d 不在顺序表中\n", elem);
                }
                break;
            case 0:
                printf("程序已退出\n");
                return 0;
            default:
                printf("选择错误,请重新输入\n");
                break;
        }
    }

    return 0;
}