#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");
}