求助一简单链表问题
我写的这个代码的ShowLink函数有问题,但是就是不知道怎么改。不运行show函数的时候程序还能正常运行,只要一运行就有问题。#include <iostream.h>
struct Node
{
int i;
Node *pt;
};
Node *head = NULL;
int n; //记录节点个数
Node* CreatLink()
{
Node *p, *next;
p = next = new Node;
cout<<"value"<<endl;
cin>>p->i;
if (head = NULL)
{
head = p;
}
else
{
while ( p->i )
{
next = new Node;
cout<<"value"<<endl;
cin>>next->i;
p->pt = next;
p = next;
n++;
}
}
p->pt = NULL;
return head;
}
void ShowLink (Node *ptp)
{
while (ptp->pt)
{
cout<<n<<endl;
cout<<ptp->i<<endl;
ptp=ptp->pt;
}
}
void main ()
{
CreatLink();
ShowLink(CreatLink());
cout<<n<<endl;
}