qq_43412960 2019-12-25 00:26 采纳率: 78.6%
浏览 395
已采纳

这是数据结构一个链表初始化问题


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

typedef  struct  LNode 
{
Book Elem;
LNode *next;

}LNode,*List;

Status InitList(LNode *L)
{
  L=new LNode();
  if(!L)
      exit(-1);
  else
    L->next=NULL;
  return OK;
}

int main()
{
LNode *L;

List LL;
InitList(L);
cout<<L->next;
getch();


return 0;
}

为什么输出L->next的值不为NULL,反而溢出呢?

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-12-25 09:55
    关注

    Status InitList(LNode *L)
    说了好多次了,要双指针

    #include<iostream>
    #include<conio.h>
    #include<string.h>
    #define OK 1
    #define MAXSIZE 100
    using namespace std;
    typedef  int Status;
    #define OK 1
    typedef  struct  
    {
    char name[5];
    float price;    
    }Book;
    
    typedef  struct  LNode 
    {
    Book Elem;
    LNode *next;
    
    }LNode,*List;
    
    Status InitList(LNode **L)
    {
      *L=new LNode();
      if(!(*L))
          exit(-1);
      else
        (*L)->next=NULL;
      return OK;
    }
    
    int main()
    {
    LNode *L;
    
    List LL;
    InitList(&L);
    cout<<L->next;
    getch();
    
    
    return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员