artais 2022-04-04 20:42 采纳率: 61.1%
浏览 68
已结题

顺序存储线性表问题,如何将代码补充完整

问题遇到的现象和发生背景

将该段代码补充完整(如有错需修改)
并用上述基本方法,实现如下要求:
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;
}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-04-05 14:03
    关注

    修改补充如下,供参考:

    #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 InputList(SqList& L, ElemType e);
    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);
    void ListPrint(SqList L);
    int main()
    {
        SqList L;  int i;
        InitList(L,MAXSIZE);
        for (i = 0;i < MAXSIZE; i++)//读入10个字符并保存在长度为10的顺序表中
             InputList(L,i+'a');
        ListPrint(L);     //表中的所有字符打印输出
    
        ListDelete(L, 6); //将第6个存储位置的字符删除;
    
        ListInsert(L, 4, '#');//第 4 个存储位置之前插入一个字符 #
    
        ListPrint(L); //表中的所有字符打印输出
       
        return 0;
    }
    bool InitList(SqList& L, unsigned int uListSize)
    {
        L.elem = new ElemType[uListSize];
        L.uListLength = 0;
        L.uListSize = uListSize;
        return true;
    }
    bool InputList(SqList& L, ElemType e)
    {
        int i;
        if (L.uListLength >= L.uListSize)
            return 0;
        L.elem[L.uListLength] = e;
        L.uListLength++;
        return 1;
    }
    bool DestroyList(SqList& L)
    {
        delete[] L.elem;
        L.elem = NULL;
        L.uListLength = 0;
        L.uListSize = 0;
        return true;
    }
    int ListLength(SqList L)
    {
        return (L.uListLength);
    }
    bool GetElem(SqList L,int i,ElemType &e)
    {
        if (i<1 || i>L.uListLength) return 0;
        e=L.elem[i-1];
        return 1;
    }
    int LocateELem(SqList L,ElemType e)
    {
        int i;
        for (i=0; i< L.uListLength; i++)
            if (L.elem[i]==e)
                return i+1;
        return 0;
    }
    bool ListDelete(SqList &L, int nPos)
    {
        int i;
        if((nPos < 1)||(nPos > L.uListLength))
           return false;
        for (i=nPos;i <= L.uListLength-1;i++)
             L.elem[i-1]=L.elem[i];
        --L.uListLength;
        return true;
    }
    bool ListInsert(SqList &L, int nPos, ElemType e)
    {
        int i;
        if((nPos < 1)||(nPos > L.uListLength))
            return false;
        for(i = L.uListLength;i > nPos - 1;i--)
            L.elem[i] = L.elem[i - 1];
        L.elem[nPos-1] = e;
        L.uListLength++;
        return true;
    }
    void ListPrint(SqList L)
    {
        int i;
        for (i = 0;i < L.uListLength;i++)
             printf("%c ",L.elem[i]);
        printf("\n");
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 修改了问题 4月5日
  • 创建了问题 4月4日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效