栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

王道数据结构p42——25

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

王道数据结构p42——25

王道数据结构p42——25 代码
#include
#include
typedef struct lnode{
	int data;
	lnode *next;
}lnode,*linklist; 

void sort(linklist &l);
void printlink(linklist &l);
void creat(linklist &l,int a[],int n);
	
int main(){
	linklist l;
	l = (lnode*)malloc(sizeof(lnode));
	l->next = NULL;

	
	//int a[10] = {1,2,3,4,5,6,7,8,9,10};
	int a[9] = {1,2,3,4,5,6,7,8,9};
	
	//creat(l,a,10);
	creat(l,a,9);
	
	sort(l);	
	printlink(l);

	return 0;
} 

void sort(linklist &l){
	lnode *fast,*low;
	fast=l;
	low=l;
	while(fast != NULL && fast->next != NULL){			//找到中间的那个 
		fast = fast->next->next;
		low = low->next;
	}
	//low就是指向中间的结点  倒置low后面的结点 
	lnode *p,*q;
	p = low->next;
	low->next = NULL;
	while(p != NULL ){
		q = p->next;
		p->next = low->next;
		low->next = p;
		p=q;
	}
	//printf("%dnn",low->data);
	//将后面的数组插入 前面的数组
	
	p = l->next;		//p是插入点 
	q = low->next;		//要插入的第一个元素 
	low->next = NULL;
	lnode *m;
	while(q!=NULL){
		m = q->next;
		q->next = p->next;
		p->next = q;
		q = m;
		
		p=p->next->next;
	}
}

void printlink(linklist &l){
	lnode *s=l->next;
	while(s!=NULL){
		printf("%dn",s->data);
		s=s->next;
	}
}
void creat(linklist &l,int a[],int n){
	lnode *s,*p;
	s=l;
	for(int i=0;i
		p = (lnode*)malloc(sizeof(lnode));
		p->data = a[i];
		s->next = p;
		s=p;
	}
	s->next = NULL;
}


注:这道题好难,写了好久,还是看答案写出来的QWQ。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号