链表实现冒泡排序

单链表实现冒泡排序

  1. /************************************************************************/  
  2. /* 用单链表实现冒泡排序,输入为0-9的数字字符                               */  
  3. /************************************************************************/  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6.   
  7. #define PR  printf   
  8.   
  9. //定义一个单链表结构体  
  10. typedef struct node{  
  11.    long data;  
  12.    struct node * next;  
  13. }lklist;  
  14.   
  15. //快速排序,小的数据往前排,后的数据往后排  
  16. lklist* sort(lklist * head)  
  17. {  
  18.     if (head == NULL)  
  19.         return NULL;  
  20.     lklist* p = head->next;  
  21.     lklist* p_pre = p;  
  22.     bool flag = false;   //用于标记是否有交换,当数组有序的时候,提高判断效率  
  23.   
  24.     while(p_pre->next != NULL)  
  25.     {  
  26.         long temp = p_pre->data;  
  27.         p = p->next;  
  28.         while (p)  
  29.         {      
  30.             if(temp <= (p->data))  
  31.             {  
  32.                 p = p->next;  
  33.                 continue;  
  34.             }  
  35.             else  
  36.             {  
  37.                 long temp_change;  
  38.                 temp_change = p->data;  
  39.                 p->data = p_pre->data;  
  40.                 p_pre->data = temp_change;  
  41.                 p = p->next;  
  42.                 flag = true;  
  43.             }  
  44.             if (flag == false)  
  45.             {  
  46.                 return head;  
  47.             }  
  48.         }         
  49.         p_pre = p_pre->next;  
  50.         p = p_pre;  
  51.     }  
  52.     return head;  
  53.       
  54. }  
  55. //向含有一个头节点的单链表中插入数据  
  56. lklist* create_lklist(lklist *head)  
  57. {     
  58.     PR("please input the number of the data and end with q(Q):\n");  
  59.     while(1)  
  60.     {  
  61.         char ch[2];  
  62.         scanf("%s",ch);  
  63.         if(ch[0] == 'q' || ch[0] == 'Q')  //输入为q(Q)时停止输入  
  64.             break;  
  65.         else  
  66.         {  
  67.             long intData = strtol(ch,0,10);  
  68.             lklist* temp = NULL;  
  69.             temp = (lklist*)malloc(sizeof(struct node));  
  70.             temp->data = intData;  
  71.             temp->next = NULL;  
  72.       
  73.             temp->next = head->next;  
  74.             head->next = temp;  
  75.         }             
  76.     }  
  77.     return head;  
  78. }   
  79. //打印单链表中的数据信息  
  80. void printf_lklist(lklist* head)  
  81. {  
  82.     if (NULL == head)  
  83.     {  
  84.         return;  
  85.     }  
  86.     lklist * p = head->next;  
  87.     PR("the sorted number is:\n");  
  88.     while (p)  
  89.     {         
  90.         PR("%d  ",p->data);  
  91.         p = p->next;       
  92.     }  
  93.   
  94.     PR("\n");  
  95. }  
  96. //释放有数据的链表中的节点  
  97. void free_lklist(lklist* head)  
  98. {  
  99.     if (NULL == head)  
  100.     {  
  101.         return;  
  102.     }  
  103.     lklist * p = head->next;  
  104.     lklist *pre;  
  105.     while (p)  
  106.     {  
  107.         pre = p;  
  108.         p = p->next;  
  109.         free(pre);  
  110.     }  
  111. }  
  112.   
  113. void main()  
  114. {     
  115.     lklist *head = (struct node *)malloc(sizeof(struct node));  
  116.     head->next = NULL;  //头节点不存放数据  
  117.     head = create_lklist(head);  
  118.   
  119.     head = sort(head);  
  120.     printf_lklist(head);  
  121.     free_lklist(head);  
  122.     free(head); //释放申请的头节点  
  123. }  
/************************************************************************/
/* 用单链表实现冒泡排序,输入为0-9的数字字符                               */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#define PR  printf 

//定义一个单链表结构体
typedef struct node{
   long data;
   struct node * next;
}lklist;

//快速排序,小的数据往前排,后的数据往后排
lklist* sort(lklist * head)
{
	if (head == NULL)
		return NULL;
	lklist* p = head->next;
	lklist* p_pre = p;
	bool flag = false;   //用于标记是否有交换,当数组有序的时候,提高判断效率

	while(p_pre->next != NULL)
	{
		long temp = p_pre->data;
		p = p->next;
		while (p)
		{    
			if(temp <= (p->data))
			{
				p = p->next;
				continue;
			}
			else
			{
				long temp_change;
				temp_change = p->data;
				p->data = p_pre->data;
				p_pre->data = temp_change;
				p = p->next;
				flag = true;
			}
			if (flag == false)
			{
				return head;
			}
		}		
		p_pre = p_pre->next;
		p = p_pre;
	}
	return head;
	
}
//向含有一个头节点的单链表中插入数据
lklist* create_lklist(lklist *head)
{	
	PR("please input the number of the data and end with q(Q):\n");
	while(1)
	{
		char ch[2];
		scanf("%s",ch);
		if(ch[0] == 'q' || ch[0] == 'Q')  //输入为q(Q)时停止输入
			break;
		else
		{
			long intData = strtol(ch,0,10);
			lklist* temp = NULL;
			temp = (lklist*)malloc(sizeof(struct node));
			temp->data = intData;
			temp->next = NULL;
    
			temp->next = head->next;
			head->next = temp;
		}			
	}
	return head;
} 
//打印单链表中的数据信息
void printf_lklist(lklist* head)
{
	if (NULL == head)
	{
		return;
	}
	lklist * p = head->next;
	PR("the sorted number is:\n");
	while (p)
	{		
		PR("%d  ",p->data);
		p = p->next;		
	}

	PR("\n");
}
//释放有数据的链表中的节点
void free_lklist(lklist* head)
{
	if (NULL == head)
	{
		return;
	}
	lklist * p = head->next;
	lklist *pre;
	while (p)
	{
		pre = p;
		p = p->next;
		free(pre);
	}
}

void main()
{   
	lklist *head = (struct node *)malloc(sizeof(struct node));
	head->next = NULL;  //头节点不存放数据
    head = create_lklist(head);

	head = sort(head);
	printf_lklist(head);
	free_lklist(head);
	free(head); //释放申请的头节点
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值