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

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

#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条回答 默认 最新

  • caozhy
    已采纳
     // 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");
    }
    
    点赞 评论
  • qq_28107929
    不死奇迹 2015-09-06 11:59

    用new分配的内存如果没有用delete收不是一直存在吗? 难道是因为在被调用函数里重新定义的链表,所以在主调函数里不能使用?

    点赞 评论
  • caozhy
     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
        {
            LIST* pCur = pHead->next;//当前节点
            while (pCur->next != NULL)
            {
                pCur = pCur->next;
            }
            pCur->next = pM;
        }
    }
    
    点赞 评论
  • caozhy

    你的程序没有问题,只是添加节点不对,节点没有被添加到链表上。我修改了下

     Enter your data of node (-1 quit):1
    Enter your data of node (-1 quit):2
    Enter your data of node (-1 quit):3
    Enter your data of node (-1 quit):4
    Enter your data of node (-1 quit):5
    Enter your data of node (-1 quit):6
    Enter your data of node (-1 quit):-1
    1
    2
    3
    4
    5
    6
    1
    2
    3
    4
    5
    6
    100
    Press any key to continue . . .
    
    点赞 评论
  • anjing1038727095
    anjing1038727095 2015-09-06 12:12

    应该是传参有问题吧。应该是这样listadd1(&p1,100)

    点赞 评论
  • a1193561652
    纵横车 2015-09-06 12:27

    主要是你在addList1()里面的“cout<<"List is NULL"< cout pHead=new LIST();//重新分配内存,头节点”,这里改变的pHead是局部变量,所以不会对主函数里的p1造成影响。如果想在子函数里改变指针,那么就要用2级指针把指针的地址传进去。

    点赞 评论