双向链表创建有序数列
题目为创建一个双向链表,
并对双向链表进行排序,然后输出排过序的链表(递增)
我的思路是在建表的时候就做到排序。
先填一个节点,
如果第二次输入比上一次输入大,就接到后面,否则就接到前面。
下面是代码,没有输出,过一会儿程序自动结束了,求教!!!
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);
3005

被折叠的 条评论
为什么被折叠?



