问题遇到的现象和发生背景
将该段代码补充完整(如有错需修改)
并用上述基本方法,实现如下要求:
1)从键盘依次读入 10 个字符并保存在长度为 10 的字符数组中;
2)将数组第 6 个存储位置的字符删除;
3)在数组第 4 个存储位置之前插入一个字符 # ;
4)将数组中的所有字符打印输出;
使用上述顺序存储线性表的存储结构定义,实现一个一个时间复杂度为
O(n)、空间复杂度为 O(1)的算法,该算法可删除线性表中所有值为 e
的数据元素。自行定义函数名称、参数和返回值,并在 main 函数中加入
对这个函数的调用来将函数调试正确。
问题相关代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10 //最大长度
typedef char ElemType;
typedef struct {
ElemType* elem; //指向顺序存储的线性表的基地址
unsigned int uListLength; //线性表的当前长度
unsigned int uListSize; //线性表的最大长度
}SqList;
//线性表采用顺序存储实现,基本操作函数声明如下:
bool InitList(SqList& L, unsigned int uListSize);
bool DestroyList(SqList& L);
int ListLength(SqList L);
bool GetElem(SqList L, int i, ElemType &e);
bool ListInsert(SqList &L, int nPos, ElemType e);
bool ListDelete(SqList &L, int nPos, ElemType &e);
int main()
{
}
bool InitList(SqList& L, unsigned int uListSize)
{
L.elem = new ElemType[uListSize];
L.uListLength = 0;
L.uListSize = uListSize;
return true;
}
bool DestroyList(SqList& L)
{
delete[] L.elem;
L.elem = nullptr;
L.uListLength = 0;
L.uListSize = 0;
return true;
}
int ListLength(SqList L)
{
return (L. uLength);
}
int GetElem(SqList L,int i,ElemType &e)
{
if (i<1 || i>L.uLength) return 0;
e=L.elem[i-1];
return 1;
}
int LocateELem(SqList L,ElemType e)
{
for (i=0; i< L.uLength; i++)
if (L.elem[i]==e)
return i+1;
return 0;
}
bool ListDelete_Sq(SqList &L, int i)
{
if((i<1)||(i>L.uLength))
return false;
for (j=i;j<=L.uLength-1;j++)
L.elem[j-1]=L.elem[j];
--L.uLength;
return true;
}