数列有序 c语言,双向链表创造有序数列

双向链表创建有序数列

题目为创建一个双向链表,

并对双向链表进行排序,然后输出排过序的链表(递增)

我的思路是在建表的时候就做到排序。

先填一个节点,

如果第二次输入比上一次输入大,就接到后面,否则就接到前面。

下面是代码,没有输出,过一会儿程序自动结束了,求教!!!

void creatlist_double(list_double *&head,list_double *&rear,Element n)//has been done

{

list_double *s,*r,*temp=NULL;

int N=n;

list_double *k;

r=head;

int tag=1;

int x=0;

while(n--)

{

s=(list_double*)malloc(sizeof(list_double));

scanf("%d",&s->D);

if(tag==1)//填满一个节点

{

x=s->D;

r=s;

r->next=NULL;

r->front=NULL;

tag=0;

}

else if(s->D>=x)

{

x=s->D;

r->next=s;

temp=r;

r=r->next;

r->front=temp;

}

else

{

k=findmin(r,s->D,N);

if(k->front!=NULL)

{

s->next=k->next;

s->front=k;

k->next=s;

k->next->front=s;

}

else if(k->front==NULL)//k is a front plus

{

k->front=(list_double*)malloc(sizeof(list_double));

s->front=k->front;

s->next=k;

k->front->next=s;

k->front=s;

k=s;

k->front->front=NULL;

}

}

}

r->next=NULL;

}

------解决方案--------------------

仅供参考//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中

#include 

#include 

#include 

#include 

struct FB {

char fn[256];

size_t fl;

char *b;

struct FB *next;

struct FB *prev;

} *fh,*fb,*ft;

char ln[256];

char fpn[256];

FILE *af;

FILE *f;

int L,n;

int main() {

system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");

af=fopen("c:\\allfn.txt","r");

if (NULL==af) {

printf("Can not open file c:\\allfn.txt!\n");

return 1;

}

fh=NULL;

fb=NULL;

n=0;

while (1) {

if (NULL==fgets(ln,256,af)) break;

L=strlen(ln);

if ('\n'==ln[L-1]) ln[L-1]=0;

printf("read %s\n",ln);

strcpy(fpn,"c:\\tmp\\");

strcat(fpn,ln);

ft=(struct FB *)malloc(sizeof(struct FB));

if (NULL==ft) {

printf("Can not malloc ft!\n");

fclose(af);

return 2;//之前的malloc在main退出后由操作系统自动free

}

printf("ft[%d]==%p\n",n,ft);

strcpy(ft->fn,fpn);

f=fopen(fpn,"rb");

if (NULL==f) {

printf("Can not open file %s!\n",fpn);

fclose(af);

return 3;//之前的malloc在main退出后由操作系统自动free

}

ft->fl=_filelength(fileno(f));

ft->b=malloc(ft->fl);

if (NULL==ft->b) {

printf("Can not malloc ft->b!\n");

fclose(f);

fclose(af);

return 4;//之前的malloc在main退出后由操作系统自动free

}

printf("ft[%d]->b==%p\n",n,ft->b);

if (ft->fl!=fread(ft->b,1,ft->fl,f)) {

printf("fread error!\n");

fclose(f);

fclose(af);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值