双向链表循环的快速排序调用函数

补充昨天的通讯录程序,对双向链表快速排序调用程序进行修改

双向链表循环的快速排序调用函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define T 1
#define F -1


typedef int bo;
typedef char Type;


struct Address
{
struct Address* next;
struct Address* prior;
int id;
Type name[20];
Type num[12];
};


void rank_num(address head, int begin, int end);
address unit(address head, int index);
void print(address head);

main函数过长且与主题关系不大,省略。

address unit(address head, int index)        /*address是struct Address* 的自定义类型,unit函数是将address类型变量head指向它的第index个除头结点的结点*/

{
int i;
address temp = head;


for (i = 0; i < index; i++)
{
temp = temp->next;
}


return (temp);
}


void rank_num(address head, int begin, int end)           //快速排序
{
if (begin  >= end)         //排错,加强执行效率
{
return;
}

int i = begin;           //i是开始的结点位数
int j = end;              //j是结束的结点位数
char name[20];       //name[20]和num[12]是暂存数据的过渡char类型数组
char num[12];


strcpy(name, unit(head, begin)->name);          //将开始的结点的数据暂存进name,num
strcpy(num, unit(head, begin)->num);

while (i < j)
{
while (i < j && (strcmp(unit(head, j)->num, num) >= 0))            /*当最后结点的num比暂存的num大或相等,最后结点向前推一位*/
{
j--;
}

strcpy(unit(head, i)->name, unit(head, j)->name);           //第i,j位结点的数据互换
strcpy(unit(head, i)->num, unit(head, j)->num);

while (i < j && strcmp(unit(head, i)->num, num) <= 0)          /*当最初结点的num比暂存的num小或相等,最初结点向前推一位*/
{
i++;
}

strcpy(unit(head, j)->name, unit(head, i)->name);                  //第i,j位结点数据互换
strcpy(unit(head, j)->num, unit(head, i)->num);
}


strcpy(unit(head, i)->name, name);                    //将暂存的数据返回最初结点
strcpy(unit(head, i)->num, num);


rank_num(head, begin, i - 1);            //对最初结点到i - 1位进行排序
rank_num(head, i + 1, end);             //对i + 1位到最后结点进行排序
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值