jarahard 2024-05-29 20:55 采纳率: 0%
浏览 3

单链表的合集练习问题

单链表练习的代码,怎么都有bug,帮帮忙

#include<iostream>
#include<string>
using namespace std;                                     //2024/5/27制作  计应232吴浩敵

typedef struct Lnode {
    string data;
    Lnode* next;

}Lnode,*LinkList;





bool InitList(LinkList &L);//链表的初始化
bool DListInsert(LinkList& L);//尾插法建立链表
bool UListInsert(LinkList& L);//头插法建立链表
void coutt(LinkList& L);
bool InsertList(LinkList&L,int i,string e);//按位序插入,在指定位置i插入指定元素e
bool InsertNextList(LinkList L, string e);//指定节点或指定数值的后插操作
bool InsertFrontList(LinkList L, string e);//指定节点或指定数值的前插操作
bool DeleteList(LinkList& L, int i, string& e);//删除指定位置i的数据,并用e返回删除的值
bool DeleteNumList(LinkList& L, string e);//删除指定数值i





int main()
{
    Lnode* L;
    int i;
    string e;
    bool ch = true;
    //初始化部分
    if (InitList(L)==true)
        cout << "初始化成功,可以继续操作。" << endl;
    else {
        cout << "初始化失败,正在退出主程序...";
        return 1;
    }

    //建立链表的部分
    cout << "选择你的链表建立方式:1、尾插法建立   2、头插法建立:";
    int c;
    cin >> c;
    while (c != 1 && c != 2) 
    {
        cout << "格式错误,请重新输入:";
        cin >> c;
    };
    if (c == 1)
    {
        if (DListInsert(L))
        {
            cout << "建立链表成功,请选择接下来的操作:";
        }
    }
    else if (c == 2)
    {
        if (UListInsert(L))
        {
            cout << "建立链表成功,请选择接下来的操作:";
        }
    }
    while (ch) {
        //选择操作部分
        int c2 = 0;
        cout << "请选择你要进行的操作:1、插入   2、删除   3、查找   4、检查   5、退出";
        cin >> c2;
        while (c2 != 1 && c2 != 2 && c2 != 3 && c2 != 4 && c2 != 5)
        {
            cout << "输入格式错误,请重新输入:";
            cin >> c2;
        }

        //插入操作
        if (c2 == 1)
        {
            cout << "选择插入方式:1、按位序插入   2、指定数值后插入   3、指定数值前插入:";
            int c3;
            cin >> c3;
            while (c3 != 1 && c3 != 2 && c3 != 3)
            {
                cout << "输入格式错误,请重新输入:";
                cin >> c3;
            }
            if (c3 == 1)//按位序插入的方式
            {
                bool a = true;
                while (a) {
                    cout << "请输入要插入的位置:";
                    cin >> i;
                    cout << "请输入要插入的元素:";
                    cin >> e;

                    if (InsertList(L, i, e))
                    {
                        cout << "插入成功,请选择接下来的操作:" << endl;
                        a = false;
                    }
                    else
                    {
                        cout << "格式错误,请重新选择:";
                        a = true;
                    }
                }
            }
            if (c3 == 2)//指定数值的后插操作
            {
                cout << "请问你要插入哪个数值的后方:";
                cin >> e;
                if (InsertNextList(L, e))
                {
                    cout << "插入成功,请选择接下来的操作:" << endl;
                }
                else cout << "插入失败";
            }
            if (c3 == 3)//指定数值的前插操作
            {
                cout << "请问你要插入哪个数值的前方:";
                cin >> e;
                if (InsertFrontList(L, e))
                {
                    cout << "插入成功,请选择接下来的操作" << endl;
                }
                else cout << "插入失败";
            }
        }
        //删除操作
        if (c2 == 2) {
            int c3;
            cout << "请选择你的删除方式:1、按位置删除   2、按数值删除";
            cin >> c3;
            while (c3 != 1 && c3 != 2)
            {
                cout << "输入格式错误,请重新输入:";
                cin >> c3;
            }
            if (c3 == 1) {
                cout << "请输入要删除的数值的所在位置:";
                cin >> i;

                if (DeleteList(L, i, e))
                {
                    cout << "该位置元素已成功删除,请选择接下来的操作:" << endl;
                }
                else cout << "并未查询到该位置";
            }
            if (c3 == 2)
            {
                cout << "请输入你要删除的数据:";
                cin >> i;
                if (DeleteNumList(L, e))
                {
                    cout << "该元素已经成功删除,请选择接下来的操作:" << endl;
                }
                else cout << "并未查询到有该元素";
            }
        }

        if (c2 == 4)
            coutt(L);
        if (c2 == 5)
        {
            system("pause");

            return 1;
        }
    }
}










//初始化链表
    bool InitList(LinkList & L){
    L = (Lnode*)malloc(sizeof(Lnode));
    if (L == NULL)
        return false;
    L->next = NULL;
    return true;
}
//头插法建立链表
//1.输入插入的值
//2.将新结点指向NULL代表表的末尾
bool DListInsert(LinkList& L)
{
    string x;
        int num=1;//x代表要插入的值data,num代表表长
    Lnode*s=L;//指针s代表新的结点的部分,r代表遍历后的循环结点;
    Lnode* t=L;
    cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
    cin >> x;
    while (x!= "9999")
    {
        s = (Lnode*)malloc(sizeof(Lnode));
        s->data = x;
        t->next = s;//将新结点接入t后
        t = s;//将t也调动到最后的结点
        num++;
        cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
        cin >> x;
    }
    t->next = NULL;
    return true;
    
}

//头插法建立链表
bool UListInsert(LinkList& L)
{
    string x;int num = 1;
    Lnode* s, * t = L;
    cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
    cin >> x;
    while (x != "9999")
    {
        s = (Lnode*)malloc(sizeof(Lnode));
        s->data = x;
        s->next=L->next;
        L->next = s;
        num++;
        cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
        cin >> x;
    }
    return true;
}
//在表L中指定位置i插入指定数值e
bool InsertList(LinkList& L, int i, string e)
{
    if (i < 1)//小于最小节点位置,输入格式错误
        return false;
    Lnode* p;
    p = L;//p代表l接受遍历查找;
    int j = 0;
    while (p != NULL && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
    {
        return false;
    }
    Lnode* s;//代表插入的元素;
    s = (Lnode*)malloc(sizeof(Lnode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}

//在指定数值e后插入指定数据
bool InsertNextList(LinkList L, string e)
{
    Lnode* p;
    p = L;
    int j = 0;
    while (p != NULL)
    {
        p = p->next;
        if (p->data == e) {
            cout << "已找到该结点,可进行后插操作" << endl;
            break;
        }
    }
    if (p == NULL)
        return false;
    string a;
    cout << "请输入你要插入的数据:";
    cin >> a;
    Lnode* s;
    s = (Lnode*)malloc(sizeof(Lnode));
    s->data = a;
    s->next = p->next;
    p->next = s;
    return true;

}
//指定结点的前插操作
bool InsertFrontList(LinkList L, string e)
{
    Lnode* p;
    p = L;
    int j = 0;
    while (p != NULL)
    {
        p = p->next;
        if (p->data == e) {
            cout << "已找到该结点,可进行前插操作" << endl;
            break;
        }
    }
    if (p == NULL)
        return false;
    string a;
    cout << "请输入你要插入的数据:";
    cin >> a;
    Lnode* s;
    s = (Lnode*)malloc(sizeof(Lnode));
    s->data = p->data;
    s->next = p->next;
    p->next = s;
    p->data = a;
    return true;

}

//删除数据操作,按照位置删除;
bool DeleteList(LinkList& L, int i, string& e)
{
    if (i < 1)
        return false;
    int j = 0;
    Lnode* p;
    p = L;
    while (p != NULL && j < i-1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
        return false;
    if (p->next == NULL)
        return false;
    Lnode* q = p->next;
    e = q->data;
    p->next = q->next;
    free(q);
    return true;
}

//删除指定元素
bool DeleteNumList(LinkList& L, string e)
{
    Lnode* p = L;
    int j = 0;
    while (p != NULL)
    {
        if (p->next->data == e)
        {
            Lnode* s;
            s = p->next;
            p->next = p->next->next;
            free(s);
            return true;
            
        }
        p = p->next;


    }
    return false;
    
}

void coutt(LinkList& L)
{
    Lnode* t=L;
    while (t->next != NULL)
    {
        t = t->next;
        cout << t->data;
    }
    cout << endl;
}

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-05-29 20:55
    关注

    引自免费微信小程序:皆我百晓生

    经过分析,您的代码存在一些逻辑错误和内存管理问题。下面是修复后的代码,我已经对有问题的地方进行了注释。

    #include<iostream>
    #include<string>
    using namespace std;
    
    typedef struct Lnode {
        string data;
        Lnode* next;
    
    }Lnode,*LinkList;
    
    // 初始化链表
    bool InitList(LinkList & L) {
        L = new Lnode(); // 使用new替代malloc,便于C++中的资源管理
        if (L == nullptr)
            return false;
        L->next = nullptr;
        return true;
    }
    
    // 尾插法建立链表
    bool DListInsert(LinkList& L) {
        string x;
        int num = 1;
        Lnode* s = L;
        Lnode* t = L;
        cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
        cin >> x;
        while (x != "9999") {
            s = new Lnode(); // 使用new替代malloc
            s->data = x;
            t->next = s; // 将新结点接入t后
            t = s; // 将t也调动到最后的结点
            num++;
            cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
            cin >> x;
        }
        t->next = nullptr;
        return true;
    }
    
    // 头插法建立链表
    bool UListInsert(LinkList& L) {
        string x;
        int num = 1;
        Lnode* s;
        Lnode* t = L;
        cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
        cin >> x;
        while (x != "9999") {
            s = new Lnode(); // 使用new替代malloc
            s->data = x;
            s->next = L->next;
            L->next = s;
            num++;
            cout << "请输入第" << num << "个结点的数据,输入9999代表结束:";
            cin >> x;
        }
        return true;
    }
    
    // 在表L中指定位置i插入指定数值e
    bool InsertList(LinkList& L, int i, string e) {
        if (i < 1) // 小于最小节点位置,输入格式错误
            return false;
        Lnode* p;
        p = L;
        int j = 0;
        while (p != nullptr && j < i - 1) {
            p = p->next;
            j++;
        }
        if (p == nullptr)
            return false;
        Lnode* s;
        s = new Lnode(); // 使用new替代malloc
        s->data = e;
        s->next = p->next;
        p->next = s;
        return true;
    }
    
    // 在指定数值e后插入指定数据
    bool InsertNextList(LinkList L, string e) {
        Lnode* p;
        p = L;
        int j = 0;
        while (p != nullptr) {
            p = p->next;
            if (p->data == e) {
                cout << "已找到该结点,可进行后插操作" << endl;
                break;
            }
        }
        if (p == nullptr)
            return false;
        string a;
        cout << "请输入你要插入的数据:";
        cin >> a;
        Lnode* s;
        s = new Lnode(); // 使用new替代malloc
        s->data = a;
        s->next = p->next;
        p->next = s;
        return true;
    }
    
    // 指定结点的前插操作
    bool InsertFrontList(LinkList L, string e) {
        Lnode* p;
        p = L;
        int j = 0;
        while (p != nullptr) {
            p = p->next;
            if (p->data == e) {
                cout << "已找到该结点,可进行前插操作" << endl;
                break;
            }
        }
        if (p == nullptr)
            return false;
        string a;
        cout << "请输入你要插入的数据:";
        cin >> a;
        Lnode* s;
        s = new Lnode(); // 使用new替代malloc
        s->data = a;
        s->next = p;
        p->prev = s; // 添加前驱指针,方便后续处理
        return true;
    }
    
    // 删除数据操作,按照位置删除;
    bool DeleteList(LinkList& L, int i, string& e) {
        if (i < 1)
            return false;
        int j = 0;
        Lnode* p;
        p = L;
        while (p != nullptr && j < i - 1) {
            p = p->next;
            j++;
        }
        if (p == nullptr || p->next == nullptr)
            return false;
        Lnode* q = p->next;
        e = q->data;
        p->next = q->next;
        delete q; // 使用delete替代free
        return true;
    }
    
    // 删除指定元素
    bool DeleteNumList(LinkList& L, string e) {
        Lnode* p = L;
        while (p != nullptr) {
            if (p->data == e) {
                Lnode* s = p;
                p = p->next;
                delete s; // 使用delete替代free
                return true;
            }
            p = p->next;
        }
        return false;
    }
    
    // 打印链表
    void coutt(LinkList& L) {
        Lnode* t = L;
        while (t != nullptr) {
            cout << t->data;
            t = t->next;
        }
        cout << endl;
    }
    
    int main() {
        Lnode* L = nullptr; // 更改L的初始值为nullptr
        int i;
        string e;
        bool ch = true;
        // 初始化部分
        if (InitList(L)) {
            cout << "初始化成功,可以继续操作。" << endl;
        } else {
            cout << "初始化失败,正在退出主程序...";
            return 1;
        }
    
        // 建立链表的部分
        cout << "选择你的链表建立方式:1、尾插法建立   2、头插法建立:";
        int c;
        cin >> c;
        while (c != 1 && c != 2) {
            cout << "格式错误,请重新输入:";
            cin >> c;
        };
        if (c == 1) {
            if (DListInsert(L)) {
                cout << "建立链表成功,请选择接下来的操作:";
            }
        } else if (c == 2) {
            if (UListInsert(L)) {
                cout << "建立链表成功,请选择接下来的操作:";
            }
        }
        while (ch) {
            // 选择操作部分
            int c2 = 0;
            cout << "请选择你要进行的操作:1、插入   2、删除   3、查找   4、检查   5、退出";
            cin >> c2;
            while (c2 != 1 && c2 != 2 && c2 != 3 && c2 != 4 && c2 != 5) {
                cout << "输入格式错误,请重新输入:";
                cin >> c2;
            }
    
            // 插入操作
            if (c2 == 1) {
                cout << "选择插入方式:1、按位序插入   2、指定数值后插入   3、指定数值前插入:";
                int c3;
                cin >> c3;
                while (c3 != 1 && c3 != 2 && c3 != 3) {
                    cout << "输入格式错误,请重新输入:";
                    cin >> c3;
                }
                if (c3 == 1) // 按位序插入的方式
                {
                    bool a = true;
                    while (a) {
                        cout << "请输入要插入的位置:";
                        cin >> i;
                        cout << "请输入要插入的元素:";
                        cin >> e;
    
                        if (InsertList(L, i, e))
                        {
                            cout << "插入成功,请选择接下来的操作:" << endl;
                            a = false;
                        }
                        else
                        {
                            cout << "格式错误,请重新选择:";
                            a = true;
                        }
                    }
                }
                if (c3 == 2) // 指定数值的后插操作
                {
                    cout << "请问你要插入哪个数值的后方:";
                    cin >> e;
                    if (InsertNextList(L, e))
                    {
                        cout << "插入成功,请选择接下来的操作:" << endl;
                    }
                    else cout << "插入失败";
                }
                if (c3 == 3) // 指定数值的前插操作
                {
                    cout << "请问你要插入哪个数值的前方:";
                    cin >> e;
                    if (InsertFrontList(L, e))
                    {
                        cout << "插入成功,请选择接下来的操作" << endl;
                    }
                    else cout << "插入失败";
                }
            }
            // 删除操作
            if (c2 == 2) {
                int c3;
                cout
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月29日