| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 538 人关注过本帖
标题:谁能帮我看看这个c++程序的问题(关于单链表)
只看楼主 加入收藏
xph4444
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-10-21
收藏
 问题点数:0 回复次数:0 
谁能帮我看看这个c++程序的问题(关于单链表)
原题要求从终端输入一个公司的若干人员的信息,包括员工的编号、姓名、年龄和工资总数。按照编号建立员工有序单链表输出。
#include <iostream>
#include <string>
using namespace std;
struct Person
{
    int no;
    string name;
    int age;
    int salary;
    Person *next;
};
Person *head;
Person *insert(Person *f)
{
    Person *s,*p,*q;
    s=new Person;
    s=f;
    s->next=NULL;
    if(head==NULL)
    {
        head=s;
        return head;
    }
    if(head->no>s->no)
    {
        s->next=head,head=s;
        return head;
    }
    for(q=head,p=head->next;p;q=p,p=p->next)
       if(p->no>s->no)
       {
           s->next=p;
           q->next=s;
           return head;
       }
       q->next=s;//p是否也行?
       return head;
}
void display(const Person *head)//不用const是否可以?
{
    cout<<"排序后的员工信息如下:"<<endl;
    while(head)
    {
        cout<<head->no<<'\t'<<head->name<<'\t'<<head->age<<'\t'<<head->salary<<endl;
        head=head->next;
    }
}
main()
{
    Person *staff=new Person;
    head=NULL;
    cin>>staff->no>>staff->name>>staff->age>>staff->salary;
    while(staff->no!=0)
    {
        head=insert(staff);//漏了head=
        cin>>staff->no>>staff->name>>staff->age>>staff->salary;
    }
    display(head);
    return 0;
}
请问错在哪里,应该怎么改??
搜索更多相关主题的帖子: 单链 
2008-10-21 16:39
快速回复:谁能帮我看看这个c++程序的问题(关于单链表)
数据加载中...
 
   



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

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