用双链表实现简单的数据排序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
typedef struct tagNode
{
int num;
struct tagNode*next;
struct tagNode*point;
}Node_t;
typedef struct
{
Node_t *head;
Node_t *foot;
}List_t;
List_t *tlist=NULL;/*创建一个新的空链表*/
void main()
{
Node_t *ptNode=NULL;
Node_t *ptNode1=NULL;
Node_t *ptNode2=NULL;
Node_t *ptNode3=NULL;
tlist=(List_t*)malloc(sizeof(List_t));
ptNode=(Node_t*)malloc(sizeof(Node_t));
scanf("%d",&ptNode->num);
//ptNode->num=1;
ptNode->next=NULL;
ptNode->point=NULL;
tlist->head=ptNode;
//tlist->foot=ptNode;
ptNode1=(Node_t*)malloc(sizeof(Node_t));
scanf("%d",&ptNode1->num);
//ptNode1->num=7;
ptNode1->next=NULL;
ptNode1->point=ptNode;
ptNode->next=ptNode1;
//tlist->foot=ptNode1;
ptNode2=(Node_t*)malloc(sizeof(Node_t));
scanf("%d",&ptNode2->num);
//ptNode2->num=2;
ptNode2->next=NULL;
ptNode2->point=ptNode1;
ptNode1->next=ptNode2;
tlist->foot=ptNode2;
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
printf("\n");
ptNode3=tlist->foot;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->point;
printf(" ");
}
printf("\n");
//ptNode=tlist.head;
//ptNode1=ptNode->next;
//ptNode2=ptNode->next->next;
if(ptNode->num > ptNode1->num)
{
if(ptNode->num > ptNode2->num)
{
if(ptNode1->num > ptNode2->num)
{
ptNode2->next=ptNode1;
ptNode2->point=NULL;
ptNode1->next=ptNode;
ptNode1->point=ptNode2;
ptNode->next=NULL;
ptNode->point=ptNode1;
tlist->head=ptNode2;
tlist->foot=ptNode;
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
}
else
{
ptNode1->next=ptNode2;
ptNode1->point=NULL;
ptNode2->next=ptNode;
ptNode2->point=ptNode1;
ptNode->next=NULL;
ptNode->point=ptNode2;
tlist->head=ptNode1;
tlist->foot=ptNode;
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
}
}
else
{
ptNode1->next=ptNode;
ptNode1->point=NULL;
ptNode->next=ptNode2;
ptNode->point=ptNode1;
ptNode2->next=NULL;
ptNode2->point=ptNode;
tlist->head=ptNode1;
tlist->foot=ptNode;
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
}
}
else
{
if(ptNode1->num > ptNode2->num)
{
if(ptNode2->num > ptNode->num)
{
ptNode->next=ptNode2;
ptNode2->next=ptNode1;
ptNode2->point=ptNode;
ptNode1->next=NULL;
ptNode1->point=ptNode2;
tlist->head=ptNode;
tlist->foot=ptNode1;
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
}
else
{
ptNode2->next=ptNode;
ptNode2->point=NULL;
ptNode->next=ptNode1;
ptNode->point=ptNode2;
ptNode1->next=NULL;
ptNode1->point=ptNode;
tlist->head=ptNode2;
tlist->foot=ptNode1;
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
}
}
else
{
ptNode3=tlist->head;
while(ptNode3!=NULL)
{
printf("%d",ptNode3->num);
ptNode3=ptNode3->next;
printf(" ");
}
}
}
}