| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1783 人关注过本帖
标题:谁能提供一个链表排序的程序吗?
取消只看楼主 加入收藏
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
 问题点数:0 回复次数:3 
谁能提供一个链表排序的程序吗?

谁能提供一个单链表排序程序?谢谢,

搜索更多相关主题的帖子: 链表 单链 
2006-05-06 20:03
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
得分:0 
[CODE]

并不是我没看呀,我是编了,但总的程序运行得不到我想要的结果.我把程序发上来你们看看.

[/CODE]#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct term
{
float coef;
int expn;
struct term * next;
};

struct term *Creat(char ch)
{
struct term *p, *s,*r;
float x; int y;
p=(struct term*)malloc(sizeof(struct term));
p->next=NULL;
printf("\n",ch);
scanf("%f, %d",&x,&y);
while(x!=0)
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=x;
s->expn=y;
s->next=NULL;
if(p->next==NULL)
{
p->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
scanf("%f, %d",&x,&y);
}
return(p);
}
void sort(struct term *La)
{ struct term *p,*q,*s;
for(p=La->next;p;p=p->next)
{ for(q=p->next;q;q=q->next)
{if(p->expn>q->expn)
{s=(struct term*)malloc(sizeof(struct term));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q->coef=p->coef;
q->expn=p->expn;
p->coef=s->coef;
p->expn=s->expn;}
else
if(p->expn==q->expn)
{s=(struct term*)malloc(sizeof(struct term));
s->coef=p->coef+q->coef;
s->expn=p->expn;
s->next=NULL; 就这个排序函数不问题,
p=s;
s->next=p->next;
free(p);
free(q);}
else
{return;}
}
}
}

struct term *Add(struct term *f,struct term *g)
{
struct term *fg;
struct term *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(struct term*)malloc(sizeof(struct term));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn)
{
m=t->coef+q->coef;
if(m!=0)
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else
if(t->expn<q->expn)
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(struct term*)malloc(sizeof(struct term));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}

if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t? t:q;
return (fg);
}


void print(struct term *f)
{
struct term *t;
t=f->next;
if(!f->next){
printf("\n"); return;
}
while(t)
{ if(t->coef>0&&f->next!=t) printf("+");
if(t->expn==0)
printf("%f",t->coef);
else
printf("%f*X^%d",t->coef,t->expn);
t=t->next;
}
printf("\n");
}
struct term *Mul(struct term *f,struct term *g)
{
struct term *h;
struct term *t,*q,*s,*r;
h=(struct term*)malloc(sizeof(struct term));
h->next=NULL;
r=(struct term*)malloc(sizeof(struct term));
r->next=NULL;
for(t=f->next;t;t=t->next)
{
for(q=g->next;q;q=q->next)
{
s=(struct term*)malloc(sizeof(struct term));
r->next=s;
s->coef=q->coef*t->coef;
s->expn=q->expn+t->expn;
s->next=NULL;
h=Add(r,h);
}

}
return(h);
}

void Open()
{
printf("*****************************\n");
printf(" phoyle's add and mulfiple \n");
printf("yun nan cai jing university ji ke 04-1 ban weiyinggui\n");
printf("*****************************\n");
printf("please choose operation:\n");
printf("0.quit\n");
printf("1.addtion of two phoyle\n");
printf("2.mutiple of two phoyle\n");
printf("3.user operate\n");
}
void Readme()
{
printf("**********user operation***********\n");
printf("1.when input only input phoyle's coef and expn.\n");
printf("2.please input as rise form.\n");
printf("3.for example \"1, 1 2, 2 0, 0\" meaning\"1*X^1+2*X^2\"\n");
printf("4.thank you for using!haha^_^\n");
}


void main()
{ struct term *f,*g,*fg;
int i;
i=-1;
Open();

while(i!=0)
{
scanf("%d",&i);
getchar();
switch(i)
{
case 0:
return;
case 1:
clrscr();
printf("please input A\n");
f=Creat('A');
sort(f);
printf("A=");
print(f);
printf("please input B\n");
g=Creat('B');
sort(g);
printf("B=");
print(g);
printf("A+B=");
fg=Add(f,g);
print(fg);
break;
case 2:
clrscr();
printf("please input A\n");
f=Creat('A');
sort(f);
printf("A=");
print(f);
printf("please input B\n");
g=Creat('B');
sort(g);
printf("B=");
print(g);
printf("A*B=");
fg=Mul(f,g);
print(fg);
break;
case 3:
clrscr();
Readme();
break;
default:
clrscr();
Open();
}
}

}


2006-05-06 22:48
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
得分:0 
对呀,基础肯定是要抓了,但我的意思是,让你们写个链表排序函数我来看看你们是怎么个思想?可能咱们的思路上一致的,但我那出现了一点点小错误吧
2006-05-06 23:14
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
得分:0 
就那我上面的程序来说吧.我总觉得我的排序函数是对的,但运行结果却跟我没加排序函数的一样呀,大哥大姐们,帮忙看看吧
2006-05-06 23:18
快速回复:谁能提供一个链表排序的程序吗?
数据加载中...
 
   



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

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