数据结构中的函数调用
我想删除序列表中的第i个元素,并通过在主函数中调用我自己定义的DelList函数将其删除,但是编译后,我无法按预期打印出删除的元素的值。 第48行之前的代码工作正常,但似乎无法调用DelList函数,从而导致没有删除的元素被打印。 调用DelList函数是否有问题?还是DelList函数的返回有问题? 谢谢
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef int ElemType; /*Assume that the data elements in the sequence table
are integers*/
typedef struct {
ElemType elem[MAXSIZE];
int last;
}SeqList;
int DelList(SeqList *L, int i, ElemType *e)
/*The i-th data element is deleted in the sequence table L, and its value is returned with the pointer parameter e. The legal value of i is 1 ≤ i ≤ L. last +1 */
{
int k;
if ((i < 1) || (i > L->last + 1))
{
printf("Deleting the location is not legal!");
return(ERROR);
}
*e = L->elem[i - 1]; /* Store the deleted element in the variable pointed to by e*/
for (k = i; i <= L->last; k++)
L->elem[k - 1] = L->elem[k]; /*Move the following elements forward*/
L->last--;
return(OK);
}
void main()
{
SeqList *l;
int p, r;
int *q;
int i;
l = (SeqList*)malloc(sizeof(SeqList));
q = (int*)malloc(sizeof(int));
printf("Please enter the length :");
scanf("%d", &r);
l->last = r - 1;
printf("Please enter the value of each element:\n");
for (i = 0; i <= l->last; i++)
{
scanf("%d", &l->elem[i]);
}
printf("Please enter the location of the element you want to delete:\n");
scanf("%d", &p);
DelList(l, p, q);
printf("The deleted element value is:%d\n", *q);
}
编译可以通过,但不能得到我想要的结果
答案 0 :(得分:3)
有一个小错字会引起悲伤:
for (k = i; i <= L->last; k++) {
// ^
此处,k
索引正在递增,但未针对数组末尾进行测试。在数组末尾写完之后,行为是不确定的。
将此行更改为:
for (k = i; k <= L->last; k++) {
// ^
其他说明:
SeqList.length
而不是SeqList.last
。从i <= last
进行迭代要比i < length
自然得多。一索引增加了认知负担-您可以在将用户输入发送到函数之前减少用户输入以对其进行规范化,然后可以对零索引数组进行操作。l
,p
,r
,q
只会挫败调试工作。#include <stdbool.h>
代替#DEFINE OK 1
。malloc()
(尽管我意识到这个问题始于标记为C ++)。free
分配内存。您可以在堆栈上声明l
和q
,然后使用&
引用运算符将地址传递给函数。int main()
and return 0;
通知外壳程序您的程序已成功终止。以下是可能的重写方法,可以解决其中的一些问题:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data;
int length;
} SeqList;
bool list_delete(SeqList *list, int idx_to_remove, int *removed_element) {
if (idx_to_remove < 0 || idx_to_remove >= list->length) {
return false;
}
*removed_element = list->data[idx_to_remove];
list->length--;
for (int i = idx_to_remove; i < list->length; i++) {
list->data[i] = list->data[i+1];
}
return true;
}
int main() {
SeqList list;
int deleted_element;
int idx_to_delete;
printf("Please enter the length: ");
scanf("%d", &list.length);
list.data = malloc(sizeof(int) * list.length);
for (int i = 0; i < list.length; i++) {
printf("Enter the value for element %d: ", 1 + i);
scanf("%d", &list.data[i]);
}
do {
printf("Please enter the index of the element you want to delete: ");
scanf("%d", &idx_to_delete);
} while (!list_delete(&list, idx_to_delete - 1, &deleted_element));
printf("The deleted element value is: %d\n", deleted_element);
puts("The elements left in the list are:");
for (int i = 0; i < list.length; i++) {
printf("%d ", list.data[i]);
}
puts("");
free(list.data);
return 0;
}
样品运行:
Please enter the length: 4
Enter the value for element 1: 11
Enter the value for element 2: 22
Enter the value for element 3: 33
Enter the value for element 4: 44
Please enter the index of the element you want to delete: 6
Please enter the index of the element you want to delete: -1
Please enter the index of the element you want to delete: 5
Please enter the index of the element you want to delete: 4
The deleted element value is: 44
The elements left in the list are:
11 22 33