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

作者站长头像
站长
· 阅读数 5

需求:编制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++底下变成这样,应该怎么恢复到初始可以看见报错原因的状态?顺序存储,线性表的删除和插入中出现问题,未知代码哪里出错?菜鸟上路,万分感谢指教。

回复
1个回答
avatar
test
2024-06-27

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

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容