不死奇迹 2015-09-06 11:53 采纳率: 33.3%
浏览 2088
已采纳

以一个链表指针作参数传入一个函数里,若此时链表为空,则初始化链表,为什么在主函数中不能用?

#include
using namespace std;

typedef struct Node
{
int age;
struct Node* next;
}LIST;

void addList1(LIST* pHead,int x)//链表结尾加节点
{
LIST* pM=new LIST();//新节点
pM->age=x;
pM->next=NULL;
if(pHead==NULL)
{
cout<<"List is NULL"< cout pHead=new LIST();//重新分配内存,头节点
pHead->age=0;
pHead->next=pM;
}
else
{
LIST* pCur=pHead->next;//当前节点
while(pCur!=NULL)
{
pCur=pCur->next;
}
pCur=pM;
}
}
LIST* CreatList1()//创建节点
{
int data=0;
LIST* Phead=NULL;
LIST* Pm=NULL;
LIST* Pcur=NULL;
cout<<"Enter your data of node (-1 quit):";
scanf("%d", &data);
if(data!=-1)
{
Phead=(LIST*)malloc(sizeof(LIST));
Phead->age=0;
Phead->next=NULL;
Pcur=Phead;
}
while (data!=-1)
{
Pm=new LIST();
Pm->age=data;
Pm->next=NULL;
Pcur->next=Pm;
Pcur=Pcur->next;
cout<<"Enter your data of node (-1 quit):";
cin>>data;
}
return Phead;
}

void ListOut1(LIST* Phead)//输出节点
{
LIST* p=NULL;

if(Phead==NULL)
{
    cout<<"List is NULL\n";
}
else
{
    p=Phead->next;
    while(p!=NULL)
    {
        cout<<p->age<<endl;
        p=p->next;
    }
}

}

void main()
{
LIST* p1=CreatList1();
ListOut1(p1);
addList1(p1,100); //当输入链表为空时,初始化链表
ListOut1(p1); //此时输出为空? 为什么? 我不是在初始化时在堆中分配的内存 不是可以用吗?
system("pause");
}

  • 写回答

6条回答 默认 最新

  • threenewbee 2015-09-06 13:41
    关注
     // ConsoleApplication2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    using namespace std;
    
    typedef struct Node
    {
        int age;
        struct Node* next;
    }LIST;
    
    void addList1(LIST*& pHead, int x)//链表结尾加节点
    {
        LIST* pM = new LIST();//新节点
        pM->age = x;
        pM->next = NULL;
        if (pHead == NULL)
        {
            cout << "List is NULL" << endl;
            cout << "Creat New List" << endl;
            pHead = new LIST();//重新分配内存,头节点
            pHead->age = 0;
            pHead->next = pM;
        }
        else
        {
            if (pHead->next == NULL)
            {
                pHead->age = x;
            }
            else
            {
                LIST* pCur = pHead->next;//当前节点
                while (pCur->next != NULL)
                {
                    pCur = pCur->next;
                }
                pCur->next = pM;
            }
        }
    }
    
    LIST* CreatList1()//创建节点
    {
        int data = 0;
        LIST* Phead = NULL;
        LIST* Pm = NULL;
        LIST* Pcur = NULL;
        cout << "Enter your data of node (-1 quit):";
        scanf_s("%d", &data);
        if (data != -1)
        {
            Phead = (LIST*)malloc(sizeof(LIST));
            Phead->age = 0;
            Phead->next = NULL;
            Pcur = Phead;
        }
        while (data != -1)
        {
            Pm = new LIST();
            Pm->age = data;
            Pm->next = NULL;
            Pcur->next = Pm;
            Pcur = Pcur->next;
            cout << "Enter your data of node (-1 quit):";
            cin >> data;
        }
        return Phead;
    }
    
    void ListOut1(LIST* Phead)//输出节点
    {
        LIST* p = NULL;
    
        if (Phead == NULL)
        {
            cout << "List is NULL\n";
        }
        else
        {
            p = Phead->next;
            while (p != NULL)
            {
                cout << p->age << endl;
                p = p->next;
            }
        }
    
    }
    
    void main()
    {
        LIST* p1 = CreatList1();
        ListOut1(p1);
        addList1(p1, 100); //当输入链表为空时,初始化链表
        ListOut1(p1);      //此时输出为空?  为什么?  我不是在初始化时在堆中分配的内存  不是可以用吗?
        system("pause");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序