| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 470 人关注过本帖
标题:我的这个关于链表的程序不能排序,只能输入数不能输出排好的数。麻烦帮我改 ...
只看楼主 加入收藏
安静的高调
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-3-26
结帖率:100%
收藏
 问题点数:0 回复次数:4 
我的这个关于链表的程序不能排序,只能输入数不能输出排好的数。麻烦帮我改一下。谢谢啦!
#include<iostream>
using namespace std;
#define NULL 0
struct su
{int num;
su *next;
};
int main()
{su *creat(void);
su *insert(su*,su*);
su *del(su*,int);
void print(su*head);
su*head,*stu;
int del_num;
cout<<"input records:"<<endl;
head=creat();
print(head);
cin>>"the records";
print(head);
cout<<endl<<"input the inserted record:";
stu=new su;
cin>>stu->num;
while(stu->num!=0)
{head=insert(head,stu);
print(head);
cout<<endl<<"input the inserted record:";
stu=new su;
cin>>stu->num;
}

cout<<endl<<"input the deleted number:";
cin>>del_num;
while(del_num!=0)
{head=del(head,del_num);
print(head);
cout<<"input the deleted number:";
cin>>del_num;
}

return 0;
}
/*   ..............建立链表..............*/
su *creat(void)         
{su *head,*p,*q;

    p=q=new su;
    cin>>p->num;

    while(p->num!=0)
    {
        if (head==NULL)
        {
            head=p;
            q=head;
        }
        else
        {
            q->next=p;
            q->next=NULL;
        }

        q=p;
        p=new su;
        cin>>p->num;
    }
    //q->next=NULL;
 return(head);
}

/*......................插入.........................*/


su *insert(su*head,su*stu)
{su*p0,*p1,*p2;
    p1=head;
    p0=stu;
    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;}
    }
return (head);
}


/*......................删除......................*/
 


su *del(su *head,int num)     
{su *p1,*p2;
    if(head=NULL)
        cout<<"list null"<<endl;
p1=head;
while(num!=p1->num&&p1->num!=NULL)
{
    p2=p1;p1=p1->next;
}
if(num==p1->num)
{
    if(p1==head)head=p1->next;
    else p2->next=p1->next;
cout<<"deleted:"<<num<<endl;

}
else cout<<"no find"<<num;
return (head);

}
 

/*..............输出...................*/

void print(su*head)   
{ su *p1,*p2;
  int temp;
 //p1=head;
for( p1=head ;p1!=NULL;p1=p1->next)
    for(p2=p1->next; p2!=NULL;p2=p2->next)
        if(p1->num>p2->num)

        {
            temp=p1->num;
            p1->num=p2->num;
            p2->num=temp;
        }


//{cout<<p1->num<<endl;
//p1=p1->next;}
    p1=head;
    do
    {
        cout<<p1->num<<endl;
        p1=p1->next;
    } while (p1!=NULL);

 }
我的这个关于链表的程序不能排序,只能输入数不能输出排好的数。
搜索更多相关主题的帖子: include records insert 
2011-04-02 23:16
huiming
Rank: 2
等 级:论坛游民
帖 子:31
专家分:36
注 册:2010-4-8
收藏
得分:0 
do
{}while (p1!=NULL);   //该处条件应该应为假

收到的鲜花
2011-04-02 23:44
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
最近看链表看多了,就烦了!

   唯实惟新 至诚致志
2011-04-03 09:17
安静的高调
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-3-26
收藏
得分:0 
麻烦在线的高手帮忙改一下,呵呵……
2011-04-04 23:00
fhqbgmqj
Rank: 2
等 级:论坛游民
帖 子:26
专家分:57
注 册:2010-5-7
收藏
得分:0 
你插入链表的时候的判断表头的if(head = NULL)这个语句错了,if里边写成赋值操作了。。。所以你表头指向NULL了,所以后边输出的时候第一次循环就为假。你delete里边的if也写错了。。。赋值操作“=”和判断操作“=”别弄混了,太粗心了。呵呵……

[ 本帖最后由 fhqbgmqj 于 2011-4-4 23:36 编辑 ]
收到的鲜花
2011-04-04 23:34
快速回复:我的这个关于链表的程序不能排序,只能输入数不能输出排好的数。麻烦帮 ...
数据加载中...
 
   



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

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