当前位置:   article > 正文

链表实现输入两个整数数组,A B,将B与A合并,并且排序_输入两个整数数组,每个数组有5个整数,将二者进行合并,然后按照数值从小到大排序输

输入两个整数数组,每个数组有5个整数,将二者进行合并,然后按照数值从小到大排序输

实现两个整数数组进行合并,并且对合并的数组进行排序(通过链表实现);

代码如下:

/*****************************************************************
实现一个函数,功能为输入两个整数数组,A,B 实现A=A+B,也就是将A与B进行合并
并且对数组A里面的数字大小进行排序。
******************************************************************/
#include<stdio.h>
#include<stdlib.h>
//创建结构体
struct ptr
{
	int data;
	struct ptr* next;
};
//创建链表头
struct ptr* creat_headlist()
{
	struct ptr*Head_list=(struct ptr*)malloc(sizeof(struct ptr));
	Head_list->next=NULL;
	return Head_list;
}
//创建结点
struct ptr* creat_conection_polt(int data)
{
	struct ptr*c_list=(struct ptr*)malloc(sizeof(struct ptr));
	c_list->data=data;
	c_list->next=NULL;
	return c_list;
}
//插入结点
void in_set(struct ptr * Head_list,int data)
{
	struct ptr*set_list=creat_conection_polt(data);
	set_list->next=Head_list->next;
	Head_list->next=set_list;
}
//找到此链表的最后一个结点
struct ptr* last_list(struct ptr*Head_list)
{
	struct ptr*next_list_last=(struct ptr*)malloc(sizeof(struct ptr));
	while(1)
	{
		if(Head_list->next==NULL)
		{
			return Head_list;
			break;
		}
		else
		{
			next_list_last=Head_list->next;
			Head_list=next_list_last;
		}
	}
}
//对此链表的数据进行从小到大的排序利用冒泡排序法进行操作
void pai_xu(struct ptr* Head_list)
{
	struct ptr*next_ptri=(struct ptr*)malloc(sizeof(struct ptr));
	struct ptr*next_ptrj=(struct ptr*)malloc(sizeof(struct ptr));
	for(next_ptri=Head_list->next;next_ptri!=NULL;next_ptri=next_ptri->next)
	{
		for(next_ptrj=next_ptri->next;next_ptrj!=NULL;next_ptrj=next_ptrj->next)
		{
			if(next_ptri->data>next_ptrj->data)
			{
				int ptri=next_ptri->data;next_ptri->data=next_ptrj->data;next_ptrj->data=ptri;
			}
		}
	}
	free(next_ptri);
	free(next_ptrj);
}
//打印链表
void print_list(struct ptr* Head_list)
{
	struct ptr* next_list=(struct ptr*)malloc(sizeof(struct ptr));
	while(1)
	{
		if(Head_list->next==NULL)
		{
			return;
		}
		else
		{
			next_list=Head_list->next;
			Head_list=next_list;
			printf("%d",Head_list->data);
			printf("\n");
		}
	}
}
//主函数
main()
{
	struct ptr* h_list=creat_headlist();
	struct ptr* H_list=creat_headlist();
	int shuzi,shuzi_again;

	while(1)
	{
		printf("请输入整数:\n");
		scanf("%d",&shuzi);
		in_set(h_list,shuzi);
		printf("是否还需要输入?Y/N\n");
		setbuf(stdin,NULL);
		char panduan;
		scanf("%c",&panduan);
		if(panduan=='N'||panduan=='n')
		{
			break;
		}
	}
	while(1)
	{
		printf("请输入整数:\n");
		scanf("%d",&shuzi_again);
		in_set(H_list,shuzi_again);
		printf("是否还需要输入?Y/N\n");
		setbuf(stdin,NULL);
		char panduan_again;
		scanf("%c",&panduan_again);
		if(panduan_again=='N'||panduan_again=='n')
		{
			break;
		}
	}
	struct ptr* l_list=last_list(h_list);
	l_list->next=H_list->next;
	pai_xu(h_list);
	print_list(h_list);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129

总结下,首先是进行链表的创建,最后是找到第一个链表的最后一个结点,将其与第二个链表的第一个结点连接,组成一个新的链表。
关于排序,采用的是冒泡排序的方法,结点的值进行比较,
第一次循环将最小的放在第一个结点,第二次将第二小的放在第二个结点,以此列推。。。。。。
冒泡排序:如下

void pai_xu(struct ptr* Head_list)
{
	struct ptr*next_ptri=(struct ptr*)malloc(sizeof(struct ptr));
	struct ptr*next_ptrj=(struct ptr*)malloc(sizeof(struct ptr));
	for(next_ptri=Head_list->next;next_ptri!=NULL;next_ptri=next_ptri->next)
	{
		for(next_ptrj=next_ptri->next;next_ptrj!=NULL;next_ptrj=next_ptrj->next)
		{
			if(next_ptri->data>next_ptrj->data)
			{
				int ptri=next_ptri->data;next_ptri->data=next_ptrj->data;next_ptrj->data=ptri;
			}
		}
	}
	free(next_ptri);
	free(next_ptrj);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

最终实现这一结果,谢谢大家,如有更好的意见,欢迎评论留言。
谢谢大家!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/780552
推荐阅读
相关标签
  

闽ICP备14008679号