文章

37

粉丝

168

获赞

13

访问

269.7k

头像
P1015 解题思路分享
P1015 贵州大学机试题
发布于2021年3月19日 14:21
阅读数 7.6k

#include <bits/stdc++.h>
using namespace std;

typedef struct Node{
	int Element;
	struct Node* next;
}Node,*Link;

Link createnode(int n){
	Link node=(Link)malloc(sizeof(Node));
	node->next=NULL;
	node->Element=n;
	return node;
}
int main()
{
		Link head=createnode(0);
		Link p=head;
		int n;
		for(int i=0;i<5;i++){
			while(p->next!=NULL) p=p->next;
			cin>>n;
			p->next=createnode(n);
		}//创建链表
		
		Link L1=head;	//有序区
		Link L2=L1->next->next;	//无序区 
		L1->next->next=NULL;
		while(L2!=NULL){
			Link nL2=L2->next;
			while(L1->next!=NULL&&L1->next->Element<L2->Element)
				L1=L1->next;
			L2->next=L1->next;
			L1->next=L2;
			L2=nL2;
			L1=head;
		}//排序
		Link h=head;
		while(h->next!=NULL){
			cout<<h->next->Element<<' ';
			h=h->next;
		}//打印
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发