单链表逆置、正向排序、打印、寻找中间元素

单链表逆置原理:

头插法:

0(初始化)、q为每一步运算的主要节点位置,初始化为第一个;

1、r=q的后续,把q的后续记录下来;

2、q-》next=p,把q的后续指向p(也就是开始),换句话说,把q放在了最开始;

3、p=q,把新的开始设置为新插入的q(下一次循环时,他就是p了);

4、q=r,把下一步要进行运算的主要节点设置为r,也就是当前运算节点的下个节点。

相当于,一队小朋友双手搭着前一个人的肩。从第二个开始,把他挪到第一个人前面去,然后对下一个人做同样操作。

  1. // 单链表.cpp : 定义控制台应用程序的入口点。
  2. //单链表
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <complex>
  6. using namespace std;
  7. typedef struct node {
  8. int data;//节点内容
  9. node *next;//下一个节点
  10. }node;
  11. //单链表的正向排序
  12. node *InsertSort(){
  13. node *head,*p,*q,*cur;
  14. int a=-1;
  15. head=new node;
  16. head->next=NULL;
  17. while (1)
  18. {
  19. cout<<"please input the data(-1,quit):";
  20. cin>>a;
  21. if (-1==a){ //输入-1结束
  22. break;
  23. }
  24. p=new node;
  25. p->data=a;
  26. p->next=NULL;
  27. q=head->next;
  28. if (q==NULL){//如果第一个节点为NULL,则对第一个节点赋值
  29. head->next=p;
  30. continue;
  31. }
  32. if (q->data>a){//如果插入值小于第一个节点,则插入到head之后
  33. p->next=head->next;
  34. head->next=p;
  35. }
  36. else{ //如果插入值大于等于第一个节点
  37. while (q->data<a){
  38. cur=q;
  39. q=q->next;
  40. if (q==NULL){ //如果到了末尾,则跳出循环
  41. break;
  42. }
  43. }
  44. if (q==NULL){ //如果到了末尾,直接插入到末节点后面
  45. cur->next=p;
  46. }
  47. p->next=q; //插入值插到q前面,cur后面
  48. cur->next=p;
  49. }
  50. }
  51. return head;
  52. }
  53. //打印单链表
  54. void print(node *head){
  55. node *p=head->next;
  56. int index=0;
  57. if (p==NULL)//链表为NULL
  58. {
  59. cout<<"Link is empty!"<<endl;
  60. getchar();
  61. return;
  62. }
  63. while (p!=NULL)//遍历链表
  64. {
  65. cout<<"The "<<++index<<"th node is :"<<p->data<<endl;//打印元素
  66. p=p->next;
  67. }
  68. }
  69. //单链表逆置
  70. node *reverse(node *head){
  71. node *p,*q,*r;
  72. if (head->next==NULL)//链表为空
  73. {
  74. return head;
  75. }
  76. p=head->next;
  77. q=p->next;//保存原第2个节点
  78. p->next=NULL;//原第1个节点为末节点
  79. while (q!=NULL)//遍历,各个节点的next指针反转
  80. {
  81. r=q->next;
  82. q->next=p;
  83. p=q;
  84. q=r;
  85. }
  86. head->next=p;//新的第一个节点为原末节点
  87. return head;
  88. }
  89. //寻找单链表的中间元素
  90. node *search_mid(node *head){
  91. node *current=NULL; //current指向当前已扫描链表的尾节点
  92. node *mid=NULL; //mid指向当前已经扫描的子链表的中间元素
  93. int i=0,j=0;
  94. current=mid=head->next;//都指向第一个节点
  95. while (current!=NULL){
  96. if (i/2>j)
  97. {
  98. j++;
  99. mid=mid->next;
  100. }
  101. i++;
  102. current=current->next;
  103. }
  104. return mid;
  105. }
  106. int _tmain(int argc, _TCHAR* argv[])
  107. {
  108. node *head=InsertSort();//创建单链表
  109. cout<<"--------after insertSort()------------"<<endl;
  110. print(head);
  111. node *preverse=reverse(head);
  112. cout<<"--------after reverse()------------"<<endl;
  113. print(preverse);
  114. node *p=search_mid(head);
  115. cout<<"--------after search_mid()------------"<<endl;
  116. cout<<"search_mid():"<<p->data<<endl;
  117. system("pause");
  118. delete [] head;
  119. return 0;
  120. }
// 单链表.cpp : 定义控制台应用程序的入口点。
//单链表
#include "stdafx.h"
#include <iostream>
#include <complex>
using namespace std;

typedef struct node {
	int data;//节点内容
	node *next;//下一个节点
}node;

//单链表的正向排序
node *InsertSort(){
	node *head,*p,*q,*cur;
	int a=-1;
	head=new node;
	head->next=NULL;
	while (1)
	{
		cout<<"please input the data(-1,quit):";
		cin>>a;
		if (-1==a){ //输入-1结束
			break;
		}
		p=new node;
		p->data=a;
		p->next=NULL;
		q=head->next;
		if (q==NULL){//如果第一个节点为NULL,则对第一个节点赋值
			head->next=p;
			continue;
		}
		if (q->data>a){//如果插入值小于第一个节点,则插入到head之后
			p->next=head->next;
			head->next=p;
		}
		else{    //如果插入值大于等于第一个节点
			while (q->data<a){
				cur=q;
				q=q->next;
				if (q==NULL){ //如果到了末尾,则跳出循环
					break;
				}
			}
			if (q==NULL){ //如果到了末尾,直接插入到末节点后面
				cur->next=p;
			}
			p->next=q; //插入值插到q前面,cur后面
			cur->next=p;
		}
	}
	return head;
}

//打印单链表
void print(node *head){
	node *p=head->next;
	int index=0;
	if (p==NULL)//链表为NULL
	{
		cout<<"Link is empty!"<<endl;
		getchar();
		return;
	}
	while (p!=NULL)//遍历链表
	{
		cout<<"The "<<++index<<"th node is :"<<p->data<<endl;//打印元素
		p=p->next;
	}
}

//单链表逆置
node *reverse(node *head){
	node *p,*q,*r;
	if (head->next==NULL)//链表为空
	{
		return head;
	}
	p=head->next;
	q=p->next;//保存原第2个节点
	p->next=NULL;//原第1个节点为末节点
	while (q!=NULL)//遍历,各个节点的next指针反转
	{
		r=q->next;
		q->next=p;
		p=q;
		q=r;
	}
	head->next=p;//新的第一个节点为原末节点
	return head;
}

//寻找单链表的中间元素
node *search_mid(node *head){
	node *current=NULL; //current指向当前已扫描链表的尾节点
	node *mid=NULL; //mid指向当前已经扫描的子链表的中间元素
	int i=0,j=0;
	current=mid=head->next;//都指向第一个节点
	while (current!=NULL){		  
		 if (i/2>j)
		 {
			 j++;
			 mid=mid->next;
		 }
            i++;
		current=current->next;
	}
	return mid;
}

int _tmain(int argc, _TCHAR* argv[])
{
	node *head=InsertSort();//创建单链表
	cout<<"--------after insertSort()------------"<<endl;
	print(head);
	node *preverse=reverse(head);
	cout<<"--------after reverse()------------"<<endl;
	print(preverse);
	node *p=search_mid(head);
	cout<<"--------after search_mid()------------"<<endl;
	cout<<"search_mid():"<<p->data<<endl;
	system("pause");
	delete [] head;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值