首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

无法输出链表元素解决方案

2012-03-09 
无法输出链表元素#includeiostream#includeconio.h#defineIS_FULL(ptr)(!ptr)usingnamespacestdtyped

无法输出链表元素
#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;
}

热点排行