无法输出链表元素
#include <iostream>
#include <conio.h>
#define IS_FULL(ptr)(!ptr)
using namespace std;
typedef struct node
{
int element;
struct node*link;
}Node;
Node*Newnode()
{
Node*p=(Node*)malloc(sizeof(Node));
if(IS_FULL(p))
cout < < "it has error! " < <endl;
cout < < "input the element " < <endl;
int n;
cin> > n;
p-> element=n;
p-> link=NULL;
return p;
}
Node*biuldlist()
{
Node*first=Newnode();
if(IS_FULL(first))
cout < < "it has error! " < <endl;
Node*p=first;
while(1)
{
cout < < "do you want continue?y/n " < <endl;
if(getch()== 'y '||getch()== 'Y ')
{
p=p-> link;
p=Newnode();
cout < <p-> element < <endl;
}
else
break;
}
return first;
}
void output(Node*first)
{
if(IS_FULL(first))
cout < < "it has error! " < <endl;
for(;first;first=first-> link)
cout < <first-> element < <endl;
}
int main()
{
Node*p=biuldlist();
output(p);
return 1;
}
[解决办法]
链表连接有问题
p=p-> link;
p=Newnode();
这两句改为
p-> link=Newnode();
p=p-> link;
修改后buildlist函数为
Node*biuldlist()
{
Node*first=Newnode();
if(IS_FULL(first))
cout < < "it has error! " < <endl;
Node*p=first;
while(1)
{
cout < < "do you want continue?y/n " < <endl;
if(getch()== 'y '||getch()== 'Y ')
{
// p=p-> link;
p-> link=Newnode();
p=p-> link;
cout < <p-> element < <endl;
}
else
break;
}
return first;
}