文章

13

粉丝

328

获赞

13

访问

131.1k

头像
(单链表的创建和排序)练习是学知识的,钻空子没有意义
P1015 贵州大学机试题
发布于2020年4月3日 01:45
阅读数 11.4k

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//定义单链表结点类型
struct node{  
	int data;//数据域 
	struct node *next;//指针域 
}node;//这里node不加,后面sizeof(node)会报错:node未定义;但是我看资料都没有加的,不懂他们为啥可以不加 
//创建链表 
struct node* create(int a[]){
	struct node *p,*L,*head,*pre;
	int i;
	head=(struct node *)malloc(sizeof(node));
	head->next=NULL;
	L=head;
	for(i=0;i<5;i++){
		p=(struct node *)malloc(sizeof(node));
		p->data=a[i];
		p->next=L->next;
		L->next=p;
	}
	return head;
} 
//递增排序单链表 (直插) 
void sort(struct node* L){
	struct node *pre,*p,*r;
	p=L->next;
	r=p->next;
	p->next=NULL;
	p=r;
	while(p){
		r=p->next;//保存*p的后续结点指针 
		pre=L;
		while(pre->next!=NULL&&pre->next->data<p->data)
			pre=pre->next;//在表中查找插入*p的前驱结点*pre 
		p->next=pre->next;//插入 
		pre->next=p;
		p=r;
	}	
}
int main(){
	int a[5],i;
	for(i=0;i<5;i++){
		scanf("%d",&a[i]);
	}
	struct node *L=creat...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发