qq_43412960 2019-12-28 20:41 采纳率: 78.6%
浏览 360
已采纳

这是一个关于 数据结构链表结点指针赋值问题


#include<iostream>
#include<conio.h>
#include<string.h>
#define OK 1
#define MAXSIZE 100
#define ERROR -1
using namespace std;
typedef  int Status;
#define OK 1
typedef  struct  
{
char name[5];
float price;    
}Book;
typedef  Book ElemType;
typedef  struct  LNode 
{
Book data;
LNode *next;

}LNode,*List;

Status InitList(List &L)
{
  L=new LNode();
  /*if(!B)
      exit(-1);
  else
  */
    L->next=NULL;
  return OK;
}
Status GetElem(int i,List L,ElemType &e)
{

List P=L->next; int j=1;
while(P&&j<1)
{


P=P->next;
++j;
}
if(!P||j<i)
return ERROR;
e=P->data;

return OK;
}
Status QianCha(List Head,List L,List  shou)
{
if(Head->next=NULL)
{
    Head->next=L;
    L->next=shou;

}
else
{
   Head->next=L->next;
   L->next=NULL;
}   
   return 0;
}
void Q(List &L,int n)
{
L=new LNode();
L->next=NULL;
for(int i=0;i<n;i++)
{
LNode  *p=new LNode();
cin>>(p->data.price);
p->next=L->next;
L->next=p;
}

}

int main()
{

Book e;
/*
注释代码不用看!
LNode *tou=NULL;

List LL=NULL;
List MM=NULL;
InitList(tou);
List BBB=NULL;
InitList(BBB);
InitList(LL);
InitList(MM);
QianCha(tou,BBB,LL);
QianCha(tou,,BBB);
GetElem(1,tou,e);
*/
List haha;
Q(haha,4);

cout<<e.price;
getch();


return 0;
}

这是我写的代码,想测试 用前插法生成单链表,在单步调试过程中,
出现这个界面

图片说明

生成了结点L空间,给L的属性next指针赋值,调试工具中不能看到NULL(即000000地址)为什么呢?

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-12-28 20:55
    关注

    因为L->next是NULL,你自己摄制的,所以没有办法显示,这个很正常啊。

    你可以试试vs2010的环境,看着更清楚
    图片说明

    代码我给完善了下:

    // Q1049361.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include<iostream>
    #include<conio.h>
    #include<string.h>
    #define OK 1
    #define MAXSIZE 100
    #define ERROR -1
    using namespace std;
    typedef  int Status;
    #define OK 1
    typedef  struct  
    {
    char name[5];
    float price;    
    }Book;
    typedef  Book ElemType;
    typedef  struct  LNode 
    {
    Book data;
    LNode *next;
    
    }LNode,*List;
    
    Status InitList(List &L)
    {
      L=new LNode();
      /*if(!B)
          exit(-1);
      else
      */
        L->next=NULL;
      return OK;
    }
    Status GetElem(int i,List L,ElemType &e)
    {
    
    List P=L->next; int j=1;
    while(P&&j<1)
    {
    
    
    P=P->next;
    ++j;
    }
    if(!P||j<i)
    return ERROR;
    e=P->data;
    
    return OK;
    }
    Status QianCha(List Head,List L,List  shou)
    {
    if(Head->next=NULL)
    {
        Head->next=L;
        L->next=shou;
    
    }
    else
    {
       Head->next=L->next;
       L->next=NULL;
    }   
       return 0;
    }
    void Q(List &L,int n)
    {
    L=new LNode();
    L->next=NULL;
    for(int i=0;i<n;i++)
    {
    LNode  *p=new LNode();
    cout << "name?";
    cin>>(p->data.name);
    cout << "price?";
    cin>>(p->data.price);
    cout << endl;
    p->next=L->next;
    L->next=p;
    }
    
    }
    
    int main()
    {
    
    Book e;
    /*
    注释代码不用看!
    LNode *tou=NULL;
    
    List LL=NULL;
    List MM=NULL;
    InitList(tou);
    List BBB=NULL;
    InitList(BBB);
    InitList(LL);
    InitList(MM);
    QianCha(tou,BBB,LL);
    QianCha(tou,,BBB);
    GetElem(1,tou,e);
    */
    List haha;
    Q(haha,4);
    haha = haha->next;
    while (haha)
    {
        e = haha->data;
        cout<<e.name << "," << e.price<<endl;
        haha = haha->next;
    }
    getch();
    
    
    return 0;
    }
    
    

    name?a
    price?1

    name?b
    price?2

    name?c
    price?3

    name?d
    price?4

    d,4
    c,3
    b,2
    a,1

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

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式