| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 578 人关注过本帖
标题:求助,链表问题
只看楼主 加入收藏
dlptiger
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-3-3
收藏
 问题点数:0 回复次数:4 
求助,链表问题
#include<iostream.h>
class student
{
public:
    char *name;
    char *number;
    int score;
    student *next;
};

void main()
{
    student *p=new student;
    cin>>p->name>>p->number>>p->score;
    student *head=p;
    for(int i=0;i<1;i++)
    {
        student *q=new student;
        cin>>q->name>>q->number>>q->score;
        p->next=q;
        p=p->next;
    }
    p=head;
    while(p!=0)
    {
        cout<<p->name<<"  ";
        cout<<p->number<<"  ";
        cout<<p->score<<"  ";
        cout<<endl;
        p=p->next;
    }
}
运行不通过,请高手指点一下……
搜索更多相关主题的帖子: 链表 
2009-09-15 09:45
沼泽
Rank: 4
等 级:业余侠客
威 望:8
帖 子:291
专家分:228
注 册:2008-9-15
收藏
得分:0 
#include<iostream.h>
 
class student
{
public:
    char name[20];
    char number[10];
    int score;
    student *next;
};
 
void main()
{
    student *p=new student;
    //对next进行初始化,不然后面的p!=0就没有意义,
    //因为它存储的是一个不确定的地址,肯定不为NULL
    p->next=NULL;
    //要为输入的数据分配内存,才能存储
    cin >> p->name >> p->number >> p->score;
     
    student *head=p;
    for(int i=0;i<1;i++)
    {
        student *q=new student;
        //原因同上
        q->next=NULL;
        cin >> q->name >> q->number >> q->score;
        p->next=q;
        p=p->next;
    }
 
    p=head;
    while (p!=0)
    {
        cout << p->name << "  ";
        cout << p->number <<"  ";
        cout << p->score <<"  ";
        cout << endl;
        p=p->next;
    }
}
2009-09-16 10:06
a2696026
Rank: 2
等 级:论坛游民
帖 子:21
专家分:33
注 册:2009-9-9
收藏
得分:0 
   p=head;


这个怎么解释?

指针头?
2009-09-16 15:04
helloabiao
Rank: 2
等 级:论坛游民
帖 子:6
专家分:25
注 册:2009-9-9
收藏
得分:0 
#include <iostream.h>
#include <stdlib.h>
 
class student
{
public:
    char* name;
    char* number;
    int score;
    student* next;
 
public:
        student();
};
 
 
student::student()
{
     
    if (NULL == (name=(char*)malloc(24*sizeof(char*)))) //指针需要分配空间大小
    {
        cout<<"内存分配失败!";
    }
    if (NULL == (number=(char*)malloc(15*sizeof(char*))))
    {
        cout<<"内存分配失败!";
    }
     
}
 
 
int main(int argc, char* argv[])
{
    student* p=new student;
    p->next=NULL;
 
    cin>>p->name>>p->number>>p->score;
    student* head=p;
 
    for (int i=0;i<1;++i)
    {
        student* q=new student;
        q->next=NULL;
        cin>>q->name>>q->number>>q->score;
        p->next=q;
        p=p->next;     
    }
     
    p=head;
 
    while (NULL != p)
    {
        cout<<p->name<<p->number<<p->score<<"\n";
        p=p->next;
    }

    return 1;
}

[ 本帖最后由 helloabiao 于 2009-9-17 09:57 编辑 ]
2009-09-17 09:44
步兵
Rank: 1
等 级:新手上路
帖 子:11
专家分:3
注 册:2008-9-14
收藏
得分:0 
回复 3楼 a2696026
应该是将指针p指向头指针以便于接下来的输出!!
2009-09-17 13:22
快速回复:求助,链表问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014614 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved