标题:高手帮我改下程序,关于动态链表
只看楼主
秋秋000
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-3-20
结帖率:100%
已结贴  问题点数:20 回复次数:3 
高手帮我改下程序,关于动态链表
要求是输入一串数字,按大小依次存入自定义的链表中,以输入0结束,最后输出链表
#include <iostream>
using namespace std;
#define NULL 0

int main()
{struct shu
{
    long num;
    shu *next;
};
    cout<<"input the num:";
    int n;
    shu *p0,*p1,*p2,*p;
    shu *head;
    head=NULL;
    p0=new shu;
    cin>>p0->num;
    p1=head;
    while(p0->num!=0)
    {
        if(head==NULL)
        {
            head=p0;
            p0->next=NULL;
        }
        else
        {
            while ((p0->num>p1->num)&&(p1->next!=NULL))
            {
                p2=p1;
                p1=p1->next;
            }
            if(p0->num<p1->num)
            {
                if(head==p1)head=p0;
                else p2->next=p0;
                p0->next=p1;
            }
            else
            {
                p1->next=p0;
                p0->next=NULL;
            }
        }
        n=n+1;
        
    }
    cout<<"there are:";
    p=head;
    do
    {
        cout<<p->num<<" ";
        p=p->next;
    }
    while(p!=NULL);
return 0;
}
搜索更多相关主题的帖子: input long 
2011-03-20 16:59
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
得分:20 
程序代码:
#include <iostream>
using namespace std;

#define NULL 0

int main()
{
    struct shu
    {
        long num;
        struct shu *next;
    };
    int n;
    shu *head = NULL;
    shu *p = NULL;
    shu *q = NULL;
    shu *fq = NULL;

    cout<<"\t每次输入数字并按enter进行下次输入  最终以零结束\n";
    while( cin >> n, 0 != n )
    {
        p = new struct shu;
        p->num = n;
        q = head;
        while( NULL != q && q->num < p->num )
        {
            fq = q;
            q = q->next;
        }

        if( NULL == fq )
        {
            p->next = head;
            head = p;
        }
        else
        {
            p->next = fq->next;
            fq->next = p;
        }
    }
    cout<<"there are:";
    p = head;
    while( NULL != p )
    {
        cout << p->num << ' ';
        p = p->next;
    }
    cout << endl;

    return 0;
}
2011-03-20 18:26
秋秋000
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-3-20
得分:0 
回复 2楼 诸葛修勤
谢谢
2011-03-20 18:38
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
得分:0 
回复 3楼 秋秋000
不用客气
2011-03-20 21:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-334237-1-1.html




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

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