数据结构2:单链表的排序

list.c

#include"list.h"

plink1 get_head()
{
	plink1 p=malloc(sizeof(link1));
	if(p==NULL)
	{
		printf("申请失败\n");
		return NULL;
	}
	p->len=0;
	p->next=NULL;
	return p;
}

int head(plink1 L,int e)
{
	if(L==NULL)
	{
		printf("单链表不存在\n");
		return -1;
	}
	plink1 p=malloc(sizeof(link1));
	p->data=e;
	p->next=L->next;
	L->next=p;
	L->len++;
}

int put(plink1 L)
{
	if(L==NULL||L->len<0)
	{
		printf("单链表不存在\n");
		return -1;
	}
	plink1 p=L;
	for(int i=0;i<L->len;i++)
	{
		p=p->next;
		printf("%d\t",p->data);
	}
}

int paixu(plink1 L)
{
	if(L==NULL||L->len<0)
	{
		printf("单链表不存在\n");
		return -1;
	}
	plink1 a;
	plink1 p;
	for (int i = 1; i <L->len; i++)
	{

		for (a=L,p=L->next;p&&p->next!=NULL;a=a->next)
		{


			if (p->data>p->next->data)
			{
				a->next=p->next;
				p->next=p->next->next;
				a->next->next=p;
			}
			else
			{
				p=p->next;
			}
			
			
		}
		
	}
	
}

list.h

#include<myhead.h>
#ifndef _LIST_H_
#define _LIST_H_

typedef struct node
{
	union{
		int len;
		int data;
		};
	struct node *next;
}link1,*plink1;

plink1 get_head();
int head(plink1 L,int e);
int put(plink1 L);
int paixu(plink1 L);
#endif

mymain.c

#include"list.h"
int main()
{
	plink1 L=get_head();
	int a[10]={1,3,5,6,33,21,22,11,9,8};
	for(int i=0;i<10;i++)
	{
		head(L,a[i]);
	}
	put(L);
	printf("\n");
	paixu(L);
	put(L);
	printf("\n");

	return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值