64,683
社区成员
发帖
与我相关
我的任务
分享
//这地址不是一样吗?
//运行结果:
Get p_L->head Address in Create***00AA07E0
1
Get pl->head Address in Get***00AA07E0
1***1
Press any key to continue
#include <iostream>
using namespace std;
typedef struct Node
{
int data;
int ID;
struct Node* next;
}*PNode;
typedef struct LinkList
{
PNode head;
int len;
}*PLink;
PLink CreateLinkList(int L)
{
PLink p_L=new LinkList;
p_L->head=new Node;
cout <<"Get p_L->head Address in Create***" <<p_L->head <<endl; //这里获得头结点的地址。
if(!p_L)
cout <<"Apply Memory for LinkList fail\n";
if(!p_L->head)
cout <<"Apply Memory for Node fail\n";
p_L->head->ID=0;
p_L->len=L;
p_L->head->next=NULL;
PNode p;
if(L>0)
{
while(L)
{
p=new Node;
cin>>p->data;
p->ID=L;
p->next=p_L->head->next;
p_L->head->next=p;
--L;
}
}
else
{
p=NULL;
p_L->head->next=p;
}
return (p_L);
}
int GetElem(PLink pl)
{
if (!pl)
{
cout<<"NULL";
return 0;
}
cout <<"Get pl->head Address in Get***" <<pl->head <<endl;//这里获得的头结点地址与前面获得不一样,差一个Node单元。
PNode p;
p=pl->head->next;
while(p)
{
cout <<p->ID <<"***" <<p->data <<endl;
p=p->next;
}
return 0;
}
void main()
{
PLink p=CreateLinkList(1);
GetElem(p);
}