wildhabit 2021-11-25 02:25 采纳率: 66.7%
浏览 24
已结题

谁能帮我改一改这个代码(关于C++顺序表的)?

需要实现
创建顺序表1:a b c d
值为a在表中的位置为:1
位置4的值为:d
删除第二个结点后顺序表:a c d
创建顺序表2:h i j k
合并后的表为:a c d h i j k
但我的代码实际实现的是:(附图)

img

#include<iostream>
using namespace std;
#define MAXSIZE 100
#define ERROR 0
typedef struct {
    int* elem;
    int length;
}S;

void InitList(S& L)
{
    L.elem = new int[MAXSIZE];
    L.length = 0;
}

int ETget(S L, int i)
{
    return L.elem[i - 1];
}

void Insert(S& L, char e, int i)
{
    for (int j = L.length - 1; j > i - 1;j--)
        L.elem[j + 1] = L.elem[j];
    L.elem[i] = e;
    ++L.length;
}

void Delete(S& L, int i)
{
    for (int j = i - 1; j < L.length;j++)
        L.elem[j] = L.elem[j + 1];
    --L.length;
}

void display(S& L)
{
    for (int i = 0;i < L.length;i++)
        cout << L.elem[i] << " ";
    cout << endl;
}

int Length(S L)
{
    return L.length;
}

int locate(S L, char e)
{
    for (int i = 0;i <= 3;i++)
        if (e == L.elem[i])
            return i;
}

void merg(S L1, S L2, S &L)
{
    L.length = L1.length + L2.length;
    for (int i = 0;i <= 2;i++)
        Insert(L, L1.elem[i],i);
    for (int j = 0;j <= 3;j++)
        Insert(L, L2.elem[j],j+3);
}

int main()
{
    S L1, L2, L;
    cout << "创建顺序表1:";
    InitList(L1);
    Insert(L1, 'a', 0);
    Insert(L1, 'b', 1);
    Insert(L1, 'c', 2);
    Insert(L1, 'd', 3);
    display(L1);
    cout << "值为a在表中的位置为:"<<locate(L1,'a') << endl;
    cout << "位置4的值为:"<<ETget(L1, 3)<<"\n";
    cout << "删除第二个结点后顺序表:";
    Delete(L1, 2);
    display(L1);
    cout << "创建顺序表2:";
    InitList(L2);
    Insert(L2, 'h', 0);
    Insert(L2, 'i', 1);
    Insert(L2, 'j', 2);
    Insert(L2, 'k', 3);
    display(L2);
    InitList(L);
    cout << "合并后的表为:";
    merg(L1, L2, L);
    display(L);

}

  • 写回答

1条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    #include<iostream>
    using namespace std;
    #define MAXSIZE 100
    #define ERROR 0
    typedef struct {
        char* elem;
        int length;
    }S;
    void InitList(S& L)
    {
        L.elem = new char[MAXSIZE];
        L.length = 0;
    }
    char ETget(S L, int i)
    {
        return L.elem[i];
    }
    void Insert(S& L, char e, int i)
    {
        for (int j = L.length - 1; j > i-1 ;j--)
            L.elem[j + 1] = L.elem[j];
        L.elem[i] = e;
        ++L.length;
    }
    void Delete(S& L, int i)
    {
        for (int j = i; j < L.length;j++)
            L.elem[j] = L.elem[j + 1];
        --L.length;
    }
    void display(S& L)
    {
        for (int i = 0;i < L.length;i++)
            cout << L.elem[i] << " ";
        cout << endl;
    }
    int Length(S L)
    {
        return L.length;
    }
    int locate(S L, char e)
    {
        for (int i = 0;i < L.length;i++)
            if (e == L.elem[i])
                return i;
    }
    void merg(S L1, S L2, S &L)
    {
        for (int i = 0;i < L1.length;i++)
            Insert(L, L1.elem[i],i);
        for (int j = 0;j < L2.length;j++)
            Insert(L, L2.elem[j],j+L1.length);
    }
    int main()
    {
        S L1, L2, L;
        cout << "创建顺序表1:";
        InitList(L1);
        Insert(L1, 'a', 0);
        Insert(L1, 'b', 1);
        Insert(L1, 'c', 2);
        Insert(L1, 'd', 3);
        display(L1);
        cout << "值为a在表中的位置为:"<<locate(L1,'a')+1 << endl;
        cout << "位置4的值为:"<<ETget(L1, 3)<<"\n";
        cout << "删除第二个结点后顺序表:";
        Delete(L1, 1);
        display(L1);
        cout << "创建顺序表2:";
        InitList(L2);
        Insert(L2, 'h', 0);
        Insert(L2, 'i', 1);
        Insert(L2, 'j', 2);
        Insert(L2, 'k', 3);
        display(L2);
        InitList(L);
        cout << "合并后的表为:";
        merg(L1, L2, L);
        display(L);
    }
    

    img

    如有帮助,望采纳!谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月25日

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致