链表创建排序

/*纯排序,没有其它的东西*/
#include<iostream>
#include<malloc.h>
using namespace std;
struct teacher
{
	char temp;
	char name[50]; 
	int count; 
	struct teacher *next;
};
/*创建链表*/
struct teacher *creat()
{
	struct teacher *p,*q,*head;
	p = (struct teacher *)malloc(sizeof(struct teacher));
	head = p;
	while(cin>>p->temp>>p->name>>p->count)
	{ 
		if(p->temp == 'v') p->count = p->count*30+3000; 
		if(p->temp == 'p') p->count = p->count*50+5000; 
		if(p->temp == 'l') p->count = p->count*20+2000;
		q = p; 
		p = (struct teacher *)malloc(sizeof(struct teacher));
		q->next = p;
	} 
	q->next = NULL;
	return head;
}
/*排序*/
struct teacher *play(struct teacher *head)
{ 
	struct teacher *p,*p1,*p2,*p3; 
	struct teacher h, t; 
	if (head == NULL) return NULL; h.next = head;
	p = &h;
	while (p->next != NULL)
	{
		p = p->next; 
	} 
	p = p->next=&t;
	while (p != h.next)
	{
		p3 = &h;
		p1 = p3->next;
		p2 = p1->next; 
		while (p2 != p) 
		{
			if ((p1->count)<(p2->count))/*此处开始排序*/
			{ 
				p1->next = p2->next;
				p2->next = p1;
				p3->next = p2; 
				p3 = p2; 
				p2 = p1->next;
			} 
			else 
			{ 
				p3 = p1; 
				p1 = p2;
				p2 = p2->next; 
			} 
		} 
		p = p1;
	}
	while (p->next != &t) 
	{
		p = p->next;
	} 
	p->next = NULL; 
	return h.next;
}
void out(struct teacher *head)
{
	struct teacher *p;
	p = head;
	while(p != NULL)
	{ 
		cout<<p->name<<' '<<p->count<<endl;
		p = p->next; 
	}
}
int main()
{
	struct teacher *head;
	head = creat(); 
	head = play(head);
	out(head); 
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值