我是数据结构的新手我已经使用数组为Insertion
和Deletion
操作完成了以下代码,它运行时没有错误但是有一个运行时错误plz帮助我找出错误。
#include <stdio.h>
#define MAX 5
void insert(int *,int pos, int num);
void del(int *,int pos);
void display(int *);
int main()
{
int arr[5];
/*Start of array insertion function*/
insert(arr,1,11);
insert(arr,2,12);
insert(arr,3,13);
insert(arr,4,14);
insert(arr,5,15);
/*End of array insertion function*/
printf("Elements of Array:\n"); /*printing aray elements after insertion*/
display(arr); /*display funtion called for printing array elements*/
/*Start of deletion of array function*/
del(arr,5);
del(arr,2);
/*End of deletion of array function*/
printf("After deletion of array:\n");/*printing array elements after deletion*/
display(arr);/*display function called for printing array element*/
return 0;
}
/*inserts an element num at given position pos*/
void insert(int *arr, int pos, int num) {
/*shifts element to right*/
int i;
for (i = MAX - 1; i >= pos; i--) {
/*swapping*/
arr[i] = arr[i - 1];
arr[i]=num;
}
}
/*deletes an element from the given position pos */
void del(int *arr, int pos){
/*skip to the desired position*/
int i;
for(i=pos;i<MAX;i++){
arr[i-1]=arr[i];
arr[i-1]=0;
}
}
/*display the content of the array*/
void display(int *arr){
/*traverse the entire array*/
int i;
for(i=0;i<MAX;i++){
printf("%d\t",arr[i]);
printf("\n");
}
}
以下输出显示─
Elements of Array:
-13136
11
12
13
14
After deletion of array:
-13136
0
0
0
14
Process finished with exit code 0
但我的预期产量是 -
Elements of Array:
11 12 13 14 15
After deletion:
11 0 13 14 0
我的代码有什么问题为什么错误的输出显示为Insertion
和Deletion
operation?
好吧,我有一些空余时间:正如我在评论中所说,主要错误是在for的每一步分配变量arr []元素。你只需要设置一次。你甚至不需要在删除功能中使用for:
#define MAX 5
void insert(int *,int pos, int num);
void del(int *,int pos);
void display(int *);
int main()
{
int arr[MAX];
/*Start of array insertion function*/
insert(arr,1,11);
insert(arr,2,12);
insert(arr,3,13);
insert(arr,4,14);
insert(arr,5,15);
/*End of array insertion function*/
printf("Elements of Array:\n"); /*printing aray elements after insertion*/
display(arr); /*display funtion called for printing array elements*/
/*Start of deletion of array function*/
del(arr,5);
del(arr,2);
/*End of deletion of array function*/
printf("After deletion of array:\n");/*printing array elements after deletion*/
display(arr);/*display function called for printing array element*/
return 0;
}
/*inserts an element num at given position pos*/
void insert(int *arr, int pos, int num) {
if(pos < 1 || pos > MAX)
{
printf("INSERT: Invalid position %d\n",pos);
return;
}
/*shifts element to right*/
int i;
for (i = MAX - 1; i > pos ; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = num;
}
/*deletes an element from the given position pos */
void del(int *arr, int pos){
if(pos < 1 || pos > MAX)
{
printf("DELETE: Invalid position %d\n",pos);
return;
}
arr[pos - 1]=0;
}
/*display the content of the array*/
void display(int *arr){
/*traverse the entire array*/
int i;
for(i=0;i<MAX;i++){
printf("%d\t",arr[i]);
printf("\n");
}
}
我还使用MAX来设置数组大小,以避免错误并添加了一个插入和删除检查,以确保pos是有效位置