Cons. Marx 2021-08-31 23:12 采纳率: 92.3%
浏览 49
已结题

请问这个链表标红的那一行有什么问题吗?调试了程序卡在这

img

#include <iostream>
using namespace std;
struct List{
    int value;
    struct List* next;
    struct List* last;
};//定义一个链表结构 

int main()
{
    int n;
    cin>>n; 
    List* head = new List();//定义链表的头部 
    head->value=0;
    head->next=NULL;
    head->last=NULL;
    List* t=head;//定义临时节点t 
    for(int i=0;i<n;i++)
    {
        List* p = new List;
        cin>>p->value;
        p->next=NULL;
        p->last=t;
        t->next=p;
        t=p;
    }//每次循环创造一个新的节点,并获取value。 
    List* end=new List;//定义尾部节点 
    end->last=t;
    end->next=NULL;
    end->value=0;
    t->last=end;
    t=head->next;
    while(t->next!=NULL)//遍历链表中的value 
    {
        List* temp = new List;//定义一个节点,使其指向t节点的下一个 
        temp = t->next;
        while(temp->next!=NULL)//遍历temp节点,使t节点右边的value全大于t节点的value 
        {
            if(temp->value<t->value)//如果temp节点的value小于于t节点的value,则删除temp节点 
            {
                temp->next->last=temp->last;
                temp->last->next=temp->next;
            }
            temp =temp->next;
        }
        t = t->next;
    }
    t=head->next;
    while(t!=NULL)//遍历输出链表,检验程序至此的正确性 
    {
        cout<<t->value<<" ";
        t=t->next;
    }
    t=end;
    while(t->last!=NULL)//再来一边,使t节点左边的值全为小于t节点value的值 
    {
        List* temp = new List;
        temp = t->last;//定义temp节点,使其指向t节点的上一个(last) 
        while(temp->last!=NULL)
        {
            if(temp->value>t->value)//若temp的value大于t的value,则删除temp节点 
            {
                temp->last->next=temp->next;
                temp->next->last=temp->last;
            }
            temp =temp->last;
        }
        t = t->last;
    }
    t=head;
    while(t->next!=NULL)//遍历输出链表 
    {
        cout<<t->value;
        t=t->next;
    }
    
}

感觉应该是下标有问题,请指教

  • 写回答

2条回答 默认 最新

  • orange4reg 2021-09-01 00:19
    关注

    看图片的话,是不是因为你在那一行设置了断点,所以它停在那里。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月10日
  • 已采纳回答 9月2日
  • 创建了问题 8月31日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器