SqList* a, * b, * c; bool Listinsert(SqList* L, int i, ElemType e); Listinsert(&a,i,e);
时间: 2024-01-25 20:04:04 浏览: 37
根据您提供的代码,出现错误的可能原因是 `Listinsert` 函数的调用方式不正确。
在调用函数时,需要按照函数的参数列表依次传递参数,而您的调用方式是 `Listinsert(&a,i,e)`,并没有指定参数的具体位置,因此编译器无法确定哪个参数应该传递给哪个形参,导致出现编译错误。
正确的调用方式应该是:
```c++
Listinsert(a, i, e);
```
其中,第一个参数 `a` 是待插入元素的顺序表,第二个参数 `i` 是插入位置,第三个参数 `e` 是待插入的元素。
另外,请注意 `Listinsert` 函数的返回值类型为 `bool`,需要根据函数返回值判断插入操作是否成功。
相关问题
顺序表定义如下,分别写出在顺序表中第i个位置插入数据元素e和删除数据元素e的算法。 Typedef struct{ ElemType *elem; int length; int listsize; }SqList; bool ListInsert(SqList &L, int i, ElemType e){ bool ListDelete(SqList &L, int i, ElemType &e){
在顺序表中第i个位置插入数据元素e的算法:
```
bool ListInsert(SqList &L, int i, ElemType e){
if(i < 1 || i > L.length + 1 || L.length == L.listsize){ //i小于1或大于表长加1或表满,插入无效
return false;
}
for(int j = L.length; j >= i; j--){ //将第i个位置及之后的元素依次向后移动一个位置
L.elem[j] = L.elem[j-1];
}
L.elem[i-1] = e; //插入新元素
L.length++; //表长加1
return true;
}
```
删除数据元素e的算法:
```
bool ListDelete(SqList &L, int i, ElemType &e){
if(i < 1 || i > L.length){ //i小于1或大于表长,删除无效
return false;
}
e = L.elem[i-1]; //将被删除的元素赋值给e
for(int j = i; j < L.length; j++){ //将第i个位置之后的元素依次向前移动一个位置
L.elem[j-1] = L.elem[j];
}
L.length--; //表长减1
return true;
}
```
Status listInsert(Sqlist& l, Elemtype e) { if (l.length == MAX_DICT_SIZE) return ERROR; l.elem[l.length] = e; l.length++; return OK; } bool isWordExist(Sqlist& l, const Elemtype& word) { for (int i = 0; i < l.length; i++) { if (l.elem[i] == word) return true; } return false; }分别逐行分析这两段代码谢谢
好的,我来逐行分析这两段代码。
第一段代码:
```
Status listInsert(Sqlist& l, Elemtype e) {
if (l.length == MAX_DICT_SIZE)
return ERROR;
l.elem[l.length] = e;
l.length++;
return OK;
}
```
该函数是一个在顺序表中插入元素的函数。其中,`Sqlist` 是一个结构体类型,`Elemtype` 是一个字符串类型,`MAX_DICT_SIZE` 是顺序表最大长度的常量。
第1行:函数名为 `listInsert`,输入参数有两个,`Sqlist& l` 表示输入参数是一个引用类型的顺序表,`Elemtype e` 表示输入参数是一个字符串类型的元素。
第2行:如果当前顺序表的长度已经达到最大长度,即 `l.length` 等于 `MAX_DICT_SIZE`,则返回一个错误。
第4行:将元素 `e` 插入到顺序表的最后一个位置,即 `l.elem[l.length]`。
第5行:将顺序表的长度加1。
第6行:返回一个成功的状态。
第二段代码:
```
bool isWordExist(Sqlist& l, const Elemtype& word) {
for (int i = 0; i < l.length; i++) {
if (l.elem[i] == word)
return true;
}
return false;
}
```
该函数是一个查找字符串在顺序表中是否存在的函数。
第1行:函数名为 `isWordExist`,输入参数有两个,`Sqlist& l` 表示输入参数是一个引用类型的顺序表,`const Elemtype& word` 表示输入参数是一个字符串类型的关键字。
第2行:使用循环遍历整个顺序表。
第3-4行:如果找到了和输入关键字相同的字符串元素,则返回 true。
第6行:如果遍历整个顺序表都没有找到和输入关键字相同的元素,则返回 false。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)