链表合并-排序-logo打印参考

9 篇文章 0 订阅

功能实现:链表合并,对合并后的链表的数据进行快排。

真的吐了 一直看程序 自己写的话不是这儿错了就是那儿错了 整理一下错的地方

1.链表合并的时候需要注意把链表一 end节点的next指向链表二的头节点的next 注意不是头节点
!!!!!!!头节点是没有数据的

2.对两个链表合并以及合并后排序的函数内部 注意!!!返回链表头节点 !!!可以先用一个 指针记录下头节点地址然后返回

3.链表不是list容器 也不是数组 不要 (list+10)->date这种神奇操作来访问第十个元素
建议写一个 函数利用 for循环迭代找到所需要数据!!参考 以下 listn()函数

4.算是算法的bug 我用的是自己写的快排 我发现一旦有重复值,就会无限循环 ,所以我加了一条if限定如下
在这里插入图片描述

if ((n < 2)||((n==2)&&(listn(list,1)->data==listn(list,2)->data))) return;

最后,程序写注释是不可能的,打死也不会写的
有问题欢迎讨论,虚心接受各种指点。

#include <iostream>
#include <string>

using namespace std;


typedef struct listpoint
{
	int data;
	listpoint *next;
	listpoint *last;

}listpoint;

listpoint *creat_noraml_list(int n)
{
	 listpoint *head,*normal,*end;
	 head = (listpoint*)malloc(sizeof(listpoint));
	 end = head;
	 for(int i = 0;i<n;i++){
		 normal = (listpoint*)malloc(sizeof(listpoint));
		 cout <<"Please input the number:  ";
		 cin >> normal->data;
		 end->next = normal;
		 normal->last = end;
		 end = normal;                    //最后一个节点变成新节点
		 cout<<"检测输入数据地址:"	<< normal<<endl;
	 }
	 end->next = NULL;
	 head->last = NULL;
	 
	 return head;
}


listpoint * mergeList(listpoint*list1,listpoint*list2)
{
	cout<<"检测11---"<<list1->next;
	listpoint* list_1 = list1;
	for (int i = 1; i <= 4; i++)
	{
	list1= list1->next;
	
	}
	list1->next = list2->next;
	cout<<"检测11---"<<list_1->next;
	return list_1;

}


listpoint* listn( listpoint* l1,int n)
{
	listpoint*l2=l1;
	for (int i = 1; i <= n; i++)
	{
		if (l2->next!=NULL)
		{
			l2 = l2->next;
			
		}
	}
	return l2;

}

void quickSort(listpoint* list,int n)
{
	listpoint * list1 = list;
	int i , j;
	cout<<"开始检测1---"<<list->next<<"  ";
	int val = listn(list,(n+1)/2)->data;
	cout<<"对比数val="<<val<<endl;
	cout << val<<endl;
	if ((n < 2)||((n==2)&&(listn(list,1)->data==listn(list,2)->data))) return;
	cout << "start quicksort:";
	for ( j = 1; j <=n; j++){
		
		if (list1->next!=NULL)
		{
			list1 = list1->next;
			cout  << list1->data  <<" ";
		}	
	

	}
	list1 = list;
	cout << "    " ;
	for ( i = 1, j = n; ; i++,j--){
		cout<<"开始检测2——————"<<list->next<<"  ";
		while (listn(list,i)->data < val  && i<j) {
			
			i++  ; 
			cout<<"i加一次"<<endl;}
		cout<<"找到比val大的数"<<listn(list,i)->data<<endl;
		
		while (listn(list,j)->data > val  && i<j){
		
			j--; 
			cout<<"j减一次"<<endl;}
			cout<<"找到比val小的数"<<listn(list,j)->data<<endl;
		if (i >= j)  break;
		cout<<"任意键交换数据?"<<endl;
		int temp = listn(list,i)->data; listn(list,i)->data = listn(list,j)->data; listn(list,j)->data = temp;
		cout<<"交换完毕"<<endl;
		cout<<"左边"<<listn(list,i)->data;
		cout<<"   右边"<<listn(list,j)->data<<endl;
		
    }

 
	cout << endl ;
	quickSort (list1,i);
	quickSort (listn(list1,i),n-i);
	cout << "end quicksort:";

	for ( j = 1; j <= n; j++){
		
		if (list1->next!=NULL)
		{
			list1 = list1->next;
			cout  << list1->data  <<" ";
		}	
	
	}
}

void printInfo(listpoint* list)   
{
	cout<<"排序后链表数据为:"<<endl;
	for (int i = 1; i <= 8; i++)
	{
		if (list->next!=NULL){
			list = list->next;
		cout<<list->data<<endl;
		}
		
	}
}
	

int main()
{
	listpoint * list1;
	listpoint * list2;
	listpoint * list3;
	cout<<"请输入链表1的数据"<<endl;
	list1 = creat_noraml_list(4);
	cout<<"请输入链表2的数据"<<endl;
	
	list2 = creat_noraml_list(4);
	list3 = mergeList(list1,list2);
	quickSort(list3,8);
	cout<<"开始打印排序后的链表"<<endl;
	printInfo(list3);
	system("pause");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值