Jamesyoung36 2017-03-17 14:13 采纳率: 21.1%
浏览 964

今天试了一下老师的例子,有个问题不明白


 #include<iostream>
using namespace std;

struct node
{
    int data;
    node* link;
};

//初始化
node* initll()
{
    node* temp = new node;
    temp->link = NULL;
    temp->data = 0;//头结点的数据域表示链表的长度
    return temp;
}
//输出链表
void output(node* tou)
{
    cout << "当前链表长度为:" << tou->data << endl;
    if (tou->data > 0)
    {
        cout << "当前链表内容为:";
        node* temp = tou->link;
        for (int i = 0; i < tou->data; i++)
        {
            cout << temp->data << ","<<endl;
            temp = temp->link;
        }
    }
}

//单链表的插入(书上的步骤)
void insert(node* tou,int pos,int value)
{
    node* p = new node;
    p->data = value;
    node* s = tou;
    //定位
    for (int i = 1; i < pos; i++)
    {
        s = s->link;
    }
    p->link = s->link;
    s->link = p;
    tou->data++;
}

//pos--位置
//value--保存被删除的数值
//删除链表
void deletell(node* tou,int pos,int value)
{
    node* p = tou;
    for (int i = 1; i < pos; i++)
    {
        p = p->link;
    }
    node* temp = p->link;
    value = temp->data;
    p->link = temp->link;
    delete temp;
    tou->data--;
}

//清空链表的长度
void clear(node* tou)
{

    if (tou->data > 0)
    {
        int value=0;
        int length = tou->data;
        for (int i = 0; i < length; i++)
        {
            deletell(tou, 1,  value);
        }
    }
}

void main()
{
    node* head;
    head = initll();
    output(head);
    int value;
    for (int i = 0; i < 50; i++)
    {
        deletell(head, i, value);
        cout << "删除:" << value << endl;
    }
    cout << "链表长度为:" << head->data << endl;
    output(head);
    clear(head);
    output(head);
    cout << endl;
}


在运行过程中程序报错说是主函数中deletell(head, i, value);中的value没有初始化,我都初始化为0程序又运行中止,我应该怎么改?

  • 写回答

1条回答 默认 最新

  • kidknight 2017-03-17 15:56
    关注

    void deletell(node* tou,int pos,int value)
    void deletell(node* tou,int pos,int &value)
    请确认你有没有抄错,我认为你那一行应该是少了一个'&'符号

    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch