关闭
m0_74062574 2023-03-27 08:42 采纳率: 76.5%
浏览 50
已结题

关于#数据结构#的问题:这是为什么#include <iostream>(语言-c++)

我的程序没有报错,但是运行却没有输出,这是为什么

#include<iostream>
#define elementype int
#define headatatype char
#define MAXSIZE 20
using namespace std;

typedef struct lnode {       //链表的定义
    elementype data;
    lnode* next;

}*linklist;                     //定义链表linklist

typedef struct hnode {
    headatatype data;
    linklist first;
}*headnode;



bool initlist(linklist& l, headnode& e) {//链表初始化
    l->next = nullptr;
    e->first = l;
    cout << "请输入链表信息" << endl;
    cin >> e->data;
    return true;
}





bool isempty(const headnode& e) {  //判断链表是否为空
    if (e->first == nullptr)
        return true;
    else return false;
}

bool destroylist(linklist& l,headnode&e) {    //销毁链表
    if (isempty(e )) {
        cout << "链表为空!" << endl;
        return false;
    }
    while (l) {
        auto t = l->next;
        delete[]l;
        l = t;
    }
    return true;
}


int getlength(const linklist& l, headnode& e) {//统计链表长度
    int a = 0;
    lnode *p = l;
    if (isempty(e)) {
        return 0;
    }
    while (p->next == nullptr)
    {
        p = p->next;
        a++;
    }
    return a;

}


bool getelem(const linklist& l,const int&a,elementype &b) {//获取第i个数据
    if (a < 0) {
        cout << "位置错误" << endl;
        return false;
    }
    lnode* p = l;
    for (int i = 1; i < a+1; i++) {
        p = p->next;
        if (p->next == nullptr) {//??
            return false;
        }
    }
    b = p->data;
    return true;
}
//按值查找链表(返回下标)
int locateelem(linklist& l, elementype& e) {
    lnode* p = l;
    int a = 1;
    while (p->data != e&&p->next!=nullptr){
        p = p->next;
        a++;
}
    if (p->next == nullptr) {
        cout << "链表中没有该元素!" << endl;
        return 0;
    }
    else return a;
}


//按值查找链表(返回指针)
lnode* locateelem_l(linklist& l, elementype& e)
{
    lnode* p;
    p = l->next;
    while (p && p->data != e) {
        p = p->next;
    }
    return p;




}

bool eraselist(linklist& l, const int& a) {   //删除元素
    if (a < 0) {
        cout << "删除位置错误" << endl;
        return false;
    }
    lnode* p = l;
    for (int i = 0; i < a - 1; i++) {
        l = l->next;
    }
    if (l->next == nullptr) {
        cout << "删除位置错误" << endl;
        return false;
    }
    for (int i = 0; i < a; i++) {
        p = p->next;
    }
    
    l->next = p->next;
    return true;
}

bool insertlist(linklist& l, const int& i, const elementype&e){//插入数据
    lnode* p = l;
    int j = 0;
    for (j = 0; j < i; j++) {
        p = p->next;

    }
    if (!p || i < 0) {
        cout << "错误" << endl;
        return false;
    }
    lnode* insert = new lnode; //linklist与linklist是否等价
    insert->data = e;
    insert->next = p->next;
    p->next = insert;
    return true;    
}

//头插法创建链表
void creatlisthead(linklist& l, const int a,headnode&e){
    elementype b;
    cout << "请输入数据" << endl;
    for (int j = 0; j < a; j++) {
        cin >> b;
        lnode* p = new lnode;
        p->data = b;
        p->next = e->first;
        e->first = p;
    }
}




//尾插法创建链表
void creatlisttail(linklist& l, const int a, headnode& e) {
    cout << "请输入数据" << endl;
    lnode* r = l;
    int m=0;
    while (r->next != nullptr) {
        r = r->next;
        m++;
    }
    for (int i = 0; i < a; i++) {
        
        linklist p = new lnode;
        cin >> p->data;
        if (i==0&&m==0){
            e->first =p;
            p->next = nullptr;
        
        }
        else {
            r->next = p;
            p->next = nullptr;
            r = p;
        }
    }
}

//(没有头节点)
void creatlisttail(linklist& l, const int n) {
    lnode* r = l;
    for (int i = 0; i < n; i++)
    {
        lnode* p = new lnode;
        cin >> p->data;
        p->next = r->next;
        r->next = p;
        r = r->next;
    }
}


//双向链表
typedef struct dulnode {      //双向链表的定义
    elementype data;
    dulnode* prior, * next;
}*dulinklist;

void dinitlist(dulinklist& l) { //双向链表的初始化
    l = new dulnode;
    l->prior = nullptr;
    l->next = nullptr;
}

void dcreatlisthead(dulinklist&l,const int n) {//头插法建立双向链表
    for (int i = 0; i < n; i++) {
        dulnode* p = new dulnode;
        cin >> p->data;
        p->prior = l;
        p->next = l->next;
        l->next = p;
    }
}

void dcreatlisttail(dulinklist& l, const int n) {//尾插法建立双向链表
    dulnode* r = l;
    for (int i = 0; i < n; i++) {
        dulnode* p = new dulnode;
        cin >> p->data;
        p->prior = r;
        p->next = r->next;
        r->next = p;
        r = p;
    }
}

bool dulistinsert(dulinklist&l,const elementype m,const int n) {//在位置n前插入一个数据
    dulnode* p = l->next;
    int j = 1;
    for ( j = 1; j < n&&p->next!=nullptr; j++) {
        p = p->next;
    }
    if (j < n || p->next == nullptr) {
        return false;
    }
    dulnode*a = new dulnode;
    a->data = m;
    a->next = p->next;
    p->next = a; 
    a->prior = p;
    p->next->prior = a;
    return true;
}

bool dulisterase(dulinklist& l, const int a) {//删除位置a上的节点
    dulnode* p = l->next;
    for (int i = 1; i < a && p->next != nullptr; i++) {
        p = p->next;
    }
    if (a < 0 || !p->next) {
        return false;
    }
    if (p->next->next == nullptr) { //删除节点在尾部
        dulnode* s = p->next;
        p->next = nullptr;
        delete[]s;
        return true;
    }
    else {
        dulnode* q = p->next;  //删除节点不在尾部
        p->next = q->next;
        q->next->prior = q->prior;
        delete[]q;
        return true;
    }
}



int main() {
 headnode m;
 linklist n;
 bool t = initlist(n, m);
    if (t) {
        cout << "链表初始化成功!" << endl;
    }
    else cout << "链表初始化失败" << endl;


    int a;
    cout << "请输入链表数据个数" << endl;
    cin >> a;
    creatlisthead(n, a, m);




    return 0;
}

展开全部

  • 写回答

5条回答 默认 最新

  • 关注

    根本原因在于:你main函数中的两个变量没有申请内存空间,还有就是你没有输出函数!

    headnode m = new hnode;
    linklist n = new lnode;
    
    //遍历链表输出
    
    

    这样虽然能解决问题,但是代码不太规范,给你整体修改了一下。
    你main函数中只使用了initlist、creatlisthead两个函数啊,只给你改了这两个函数和main函数,而且,你main函数中没有显示链表。运行结果:

    img

    initlist函数:

    
    bool initlist(headnode& e) {//链表初始化
        e = new hnode; //申请内存空间
        //linklist l = new lnode;  //申请内存空间
        //l->next = nullptr;
        e->first = 0;
        cout << "请输入链表信息" << endl;
        cin >> e->data;
        return true;
    }
    
    

    creatlisthead函数:

    //头插法创建链表
    void creatlisthead(const int a, headnode& e) {
        elementype b;
        cout << "请输入数据" << endl;
        for (int j = 0; j < a; j++) {
            cin >> b;
            lnode* p = new lnode;
            p->data = b;
            p->next = e->first;
            e->first = p;
        }
    }
    
    

    main函数:

    
    int main() {
        headnode m;
        linklist n;  
        bool t = initlist(m);
        if (t) {
            cout << "链表初始化成功!" << endl;
        }
        else cout << "链表初始化失败" << endl;
    
    
        int a;
        cout << "请输入链表数据个数" << endl;
        cin >> a;
        creatlisthead(a, m);
    
        //遍历链表
        cout << "遍历链表:" << endl;
        n = m->first;
        while (n)
        {
            cout << n->data << " ";
            n = n->next;
        }
    
    
        return 0;
    }
    
    
    

    展开全部

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

    改一下给你

    回复

    因为你main函数中只调用了 initlist、creatlisthead两个函数,所以只给你改了这两个函数和main函数,修改后的代码和运行结果贴在上面了,如有帮助,望采纳,多谢。

    回复

    另外,你的头插法代码中,明显没有哨兵节点,但是你的其它函数中基本都是按有哨兵节点来写的,给你改了一部分(单向链表部分基本都改了):

    
    
    
    bool initlist(headnode& e) {//链表初始化
        e = new hnode; //申请内存空间
        //linklist l = new lnode;  //申请内存空间
        //l->next = nullptr;
        e->first = 0;
        cout << "请输入链表信息" << endl;
        cin >> e->data;
        return true;
    }
    
    
    
    
    
    bool isempty(const headnode& e) {  //判断链表是否为空
        if (e->first == nullptr)
            return true;
        else return false;
    }
    
    bool destroylist(headnode& e) {    //销毁链表
        if (isempty(e)) {
            cout << "链表为空!" << endl;
            return false;
        }
        linklist l = e->first;
        while (l) {
            linklist t = l->next;
            delete l;
            l = t;
        }
        //销毁e
        e->first = 0;
        delete e;
        e = 0;
        return true;
    }
    
    
    int getlength(headnode& e) {//统计链表长度
        int a = 0;
        lnode* p = e->first;
        if (isempty(e)) {
            return 0;
        }
        while (p == nullptr) //因为链表没有哨兵节点,所以直接用p就可以了,不应该用p->next
        {
            p = p->next;
            a++;
        }
        return a;
    
    }
    
    
    bool getelem(const linklist& l, const int& a, elementype& b) {//获取第i个数据
        if (a < 0) {
            cout << "位置错误" << endl;
            return false;
        }
        lnode* p = l;
        for (int i = 1; i < a + 1; i++) {
            p = p->next;
            if (p == nullptr) {//没有哨兵节点,直接用p就可以,这里防止超出链表长度
                return false;
            }
        }
        b = p->data;
        return true;
    }
    //按值查找链表(返回下标)
    int locateelem(linklist& l, elementype& e) {
        lnode* p = l;
        int a = 0; //没有哨兵节点,从0开始,下标从0开始(如果需要从1开始,就吧a初始化为1)
        while (p != nullptr && p->data != e ) {
            p = p->next;
            a++;
        }
        if (p == nullptr) {
            cout << "链表中没有该元素!" << endl;
            return 0;
        }
        else return a;
    }
    
    
    //按值查找链表(返回指针)
    lnode* locateelem_l(linklist& l, elementype& e)
    {
        lnode* p;
        p = l; //没有哨兵节点,从l开始即可
        while (p && p->data != e) {
            p = p->next;
        }
        return p;
    }
    
    bool eraselist(headnode& e, const int& a) {   //删除元素
        if (a < 0) {
            cout << "删除位置错误" << endl;
            return false;
        }
        lnode* p = e->first;
        lnode* l = e->first;
        if (e->first == 0)
            return false;
        if (a == 0)
        {
            l = e->first;
            e->first = l->next;
            delete l;
            l = 0;
            return true;
        }
    
    
        for (int i = 0; i < a - 1; i++) {
            l = l->next;
        }
        if (l->next == nullptr) {
            cout << "删除位置错误" << endl;
            return false;
        }
        p = l->next;
        l->next = p->next;
    
        delete p;
        p = 0;
        return true;
    }
    
    bool insertlist(linklist& l, const int& i, const elementype& e) {//插入数据
        lnode* p = l;
        int j = 0;
        for (j = 0; j < i; j++) {
            p = p->next;
    
        }
        if (!p || i < 0) {
            cout << "错误" << endl;
            return false;
        }
        lnode* insert = new lnode; //linklist与linklist是否等价
        insert->data = e;
        insert->next = p->next;
        p->next = insert;
        return true;
    }
    
    //头插法创建链表
    void creatlisthead(const int a, headnode& e) {
        elementype b;
        cout << "请输入数据" << endl;
        for (int j = 0; j < a; j++) {
            cin >> b;
            lnode* p = new lnode;
            p->data = b;
            p->next = e->first;
            e->first = p;
        }
    }
    
    
    
    
    //尾插法创建链表
    void creatlisttail(const int a, headnode& e) {
        cout << "请输入数据" << endl;
        lnode* r = e->first;
        int m = 0;
        
        for (int i = 0; i < a; i++) {
    
            linklist p = new lnode;
            p->next = nullptr;
            cin >> p->data; //放在这里
           
            if (i == 0 && r == 0) {
                e->first = p;
                r = p;
            }
            else {
                r->next = p;
                r = p;
            }
        }
    }
    

    回复
查看更多回答(4条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月27日
  • 创建了问题 3月27日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部