顺序存储,线性表的删除和插入中出现问题,未知代码哪里出错?

需求:编制C/C++程序,利用顺序存储方式实现下列功能:从键盘输入数据建立一个线性表(整数),并输出该线性表;然后根据屏幕提示,进行数据的插入或删除等操作,并在插入或删除数据后输出线性表。
代码出现问题,无法运行,错误提醒是delete那块红了,估计是它出问题。
未知是什么原因,望指教。

typedef struct {
int data[MAX_SIZE]; 
int length;        
} SeqList;


void init(SeqList *list) {
list->length = 0;
}


void insert(SeqList *list, int position, int value) {
if (position < 0 || position > list->length || list->length == MAX_SIZE) {
printf("The insert position is invalid or the linear table is full:\n");
    return;
}

for (int i = list->length - 1; i >= position; i--) {
    list->data[i + 1] = list->data[i];
}
list->data[position] = value;
list->length++;
}


void delete(SeqList *list, int position) {
if (position < 0 || position >= list->length) {
    printf("Invalid delete location:\n");
    return;
}

for (int i = position; i < list->length - 1; i++) {
    list->data[i] = list->data[i + 1];
}
list->length--;
}


void print(SeqList *list) {
printf("Linear table contents:");
for (int i = 0; i < list->length; i++) {
    printf("%d ", list->data[i]);
}
printf("\n");
}

int main() {
SeqList list;
init(&list);

printf("Please enter the length of the linear table:");
scanf("%d", &list.length);

if (list.length > MAX_SIZE) {
    printf("The linear table length exceeds the maximum value:\n");
    return 0;
}

printf("Enter the elements of the linear table:");
for (int i = 0; i < list.length; i++) {
    scanf("%d", &list.data[i]);
}

print(&list);

int operation, position, value;
printf("\nplese select:\n");    printf("1. insect the data:\n");
printf("2. delete the data\n");
printf("0. exit\n");

while (1) {
    printf("\nplease enter the ops:");
    scanf("%d", &operation);

    if (operation == 1) {
        printf("Please enter insert position and insert value (separated by space):");
        scanf("%d %d", &position, &value);
        insert(&list, position, value);
    } else if (operation == 2) {
        printf("Please enter the deletion location:");
        scanf("%d", &position);
        delete(&list, position);
    } else if (operation == 0) {
        break;
    } else {
        printf("Invalid operation number:\n");
    }

    print(&list);
}

return 0;
}

顺便提问一句我的DEVC++底下变成这样,应该怎么恢复到初始可以看见报错原因的状态?
image.png
菜鸟上路,万分感谢指教。

阅读 702
avatarAI BotBETA

您可以通过在代码编辑器中点击编辑菜单,然后选择清除和重构代码来清除当前的错误。然后重新运行程序,应该就不会再出现红色删除符号了。

1 个回答

delete 是c语言的一个 关键字,不能用来作为函数 名字,devc++ 不懂,建议用 clion ,这个工具 和 idea 类似,比较 好用

推荐问题
logo
Microsoft
子站问答
访问
宣传栏