| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1365 人关注过本帖
标题:吖[原创]裢表的插入排序
只看楼主 加入收藏
awindy
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-5-4
收藏
 问题点数:0 回复次数:20 
吖[原创]裢表的插入排序


运行的时候怎么样一下运行正确一下又错误呢?

#include <stdio.h>
#define NULL 0
typedef struct DuLNode
{
int date;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode;

void InitNode(DuLNode *L)
{
L->prior=L->next=NULL;
L->date=NULL;
}

void PrintList(DuLNode *L)
{
while(L)
{
printf(" %d",L->date);
L=L->next;
}
}

void CreatList(DuLNode *L,int m)
{
DuLNode *S,*T; int i;
T=L;
scanf("%d",&T->date);
for(i=2;i<=m;i++)
{
S=(DuLNode *)malloc(sizeof(DuLNode));
InitNode(S);
scanf("%d",&S->date);
S->prior=T;
T->next=S;
T=S;
}
}

void InsertSort(DuLNode *L)
{
int u; DuLNode *T,*M; T=L;
while(T->next)
{
if(T->next->date<T->date)
{
M=T->next;
u=T->next->date;
T->next->date=T->date;
T=T->prior;
while(u<T->date)
{
T->next->date=T->date;
T=T->prior;
}
T->next->date=u; T=M;
}
else T=T->next;
}
}

main()
{
int n; DuLNode *P;
P=(DuLNode *)malloc(sizeof(DuLNode));
P->prior=P;P->next=NULL;
puts("Please input the numbers:\n");
scanf("%d",&n);
puts("Please input the elements one by one:\n");
CreatList(P,n);
puts("the sequence you input is:\n");
PrintList(P);
puts("\nThe sequence after insert_sort is:\n");
InsertSort(P);
PrintList(P);
puts("\nPress any key to quit....");
getch();
}

搜索更多相关主题的帖子: DuLNode void next NULL date 
2006-05-06 22:16
feng1256
Rank: 4
等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
收藏
得分:0 

仔细对照下,也没必要严格按照书上的定义写链表
[CODE]
#include "stdio.h"
#include "malloc.h"

typedef struct DuLNode
{
int date;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode;

void PrintList(DuLNode *L)
{
while(L)
{
printf("%d ",L->date);
L=L->next;
}
}

void CreatList(DuLNode *L,int m)
{
DuLNode *S,*T; int i;

T=L;
scanf("%d",&T->date);
for(i=2;i<=m;i++)
{
S=(DuLNode *)malloc(sizeof(DuLNode));
scanf("%d",&S->date);
S->prior=T;
S->next=NULL;
T->next=S;
T=S;
}

}

void InsertSort(DuLNode *L)
{
int u;
DuLNode *T=L,*M;

while(T->next)
{
M=T->next;
u=T->next->date;
while(u < T->date)
{
T->next->date=T->date;
if( !(T=T->prior) )
break;
}
if(!T)
L->date=u;
else
T->next->date=u;
T=M;
}
}

void Des_Linklist(DuLNode *head)
{
DuLNode *tail;

while(head)
{
tail=head;
head=head->next;
free(tail);
}
}

int main()
{
int n;
DuLNode *head;

head=(DuLNode *)malloc(sizeof(DuLNode));
head->prior=head->next=NULL;
printf("Please input the numbers:\n");
scanf("%d",&n);
printf("\nPlease input the elements one by one:\n");
CreatList(head,n);
printf("\nThe sequence you input is:\n");
PrintList(head);
printf("\nThe sequence after insert_sort is:\n");
InsertSort(head);
PrintList(head);
printf("\n");
Des_Linklist(head);

return 0;
}

[/CODE]


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-05-07 01:20
awindy
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-5-4
收藏
得分:0 

feng1256,你改的那个在TC下运行不了,WHY?
我觉得我的没有什么不对的呀,怎么就运行不了/
有谁能帮我运行一下看看/


你的过去不是你的潜力,伟大的成就是干出来的,而不是想出来的!
2006-05-07 15:35
白水
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-5-4
收藏
得分:0 

少头文件
#include<stdlib.h>
#include<malloc.h>
#include<stdio.h>

2006-05-07 18:00
白水
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-5-4
收藏
得分:0 

main()
{
int n; DuLNode *P;
P=(DuLNode *)malloc(sizeof(DuLNode));
P->prior=P;P->next=NULL;
puts("Please input the numbers:\n");
scanf("%d",&n);
puts("Please input the elements one by one:\n");
CreatList(P,n);
puts("the sequence you input is:\n");
PrintList(P);
puts("\nThe sequence after insert_sort is:\n");
InsertSort(P);
PrintList(P);
puts("\nPress any key to quit....");
getch(); // getchar();
} //没有箭头吧

2006-05-07 18:05
awindy
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-5-4
收藏
得分:0 
加了还是运行不了!!
我对这些头文件不太了解,要怎么样才能熟练掌握?有没有有相关的学习资料/
我学了一本谭浩强的C语言书<第二版>,就只知道math.h/ stdio.h / ctype.h

你的过去不是你的潜力,伟大的成就是干出来的,而不是想出来的!
2006-05-07 18:40
awindy
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-5-4
收藏
得分:0 
以下是引用白水在2006-5-7 18:05:00的发言:

main()
{
int n; DuLNode *P;
P=(DuLNode *)malloc(sizeof(DuLNode));
P->prior=P;P->next=NULL;
puts("Please input the numbers:\n");
scanf("%d",&n);
puts("Please input the elements one by one:\n");
CreatList(P,n);
puts("the sequence you input is:\n");
PrintList(P);
puts("\nThe sequence after insert_sort is:\n");
InsertSort(P);
PrintList(P);
puts("\nPress any key to quit....");
getch(); // getchar();
} //没有箭头吧

这个是对的,那个箭头是复制的时候弄上去的/
你可以去运行下看看/


你的过去不是你的潜力,伟大的成就是干出来的,而不是想出来的!
2006-05-07 18:42
白水
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-5-4
收藏
得分:0 


1 加头文件,
2 getchar() ;不是 getch();
改后你的程序我在我的机器上可以运行():

2006-05-07 21:27
feng1256
Rank: 4
等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
收藏
得分:0 

2楼程序C-Free下运行


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-05-07 23:06
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
哦哦,你也用C-Free了啊,看来我又多了个同伴

淘宝杜琨
2006-05-07 23:33
快速回复:吖[原创]裢表的插入排序
数据加载中...
 
   



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

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