Fast and slow pointer and head interpolation and tail interpolation (based on C language)

 The magical effect of fast and slow pointers

1. Find the middle value

The general idea is: first traverse the linked list once, record how many nodes there are in total, and then traverse again to find the midpoint.

Using fast and slow pointers, let's see what this problem will become. The idea is as follows: we regard a linked list as a runway, assuming that the speed of a is twice that of b, then when a has finished the course, b will run halfway to achieve the purpose of finding the intermediate node.

2. Determine the ring in the linked list

Still compare the linked list to a runway, there are loops in the linked list, then this runway is a circular runway, in a circular runway, if two people have a speed difference, then two people will meet sooner or later, as long as they meet, then there is a loop. .

3. Delete the nth node from the bottom

Deleting the nth node from the bottom is equivalent to finding the element before the element to be deleted, which is the n-1th node. Smart, you must have found it. We have turned this problem into a problem of finding a node on the linked list.

#include<string>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef struct student
{
	int num;
	struct student *pnext;
}STU;
STU* Greatlist1()
{
	STU *pstu1 = (STU*)malloc(sizeof(STU));
	pstu1->pnext = NULL;
	return pstu1;
}
STU* GreastList2()
{
	STU *pstu2 = (STU*)malloc(sizeof(STU));
	pstu2->pnext = NULL;
	return pstu2;
}
void AddNode1(STU*p)//尾插法
{
	int num_1;
	cout << "输入尾插法要添加的节点数" << endl;
	cin >> num_1;
	STU*pnew = NULL;
	for (int i = 0,j=0; i < num_1; i++,j++)
	{
		pnew = (STU*)malloc(sizeof(STU));
		pnew->num = j;
		pnew->pnext = NULL;
		p->pnext = pnew;
		p = p->pnext;
	}
}
void AddNode2(STU*p)//头插法
{
	int num1;
	cout << "输入头插法要添加的节点数" << endl;
	cin >> num1;
	STU*pnew1 = NULL;
	for (int i = 0, j = 0; i < num1; i++, j++)
	{
		pnew1 = (STU*)malloc(sizeof(STU));
		pnew1->num = j;
		pnew1->pnext = p->pnext;
		p->pnext = pnew1;
	}
}
void Print1(STU*p)
{
	while (p->pnext != NULL)
	{
		cout << p->pnext->num<<"   ";
		p = p->pnext;
	}
	cout << endl;
}
void Print2(STU*p)
{
	while (p->pnext != NULL)
	{
		cout << p->pnext->num<<"  ";
		p = p->pnext;
	}
	cout << endl;
}
//快慢指针
void SearchAddNode1(STU*p)
{
	STU*fast = p;
	STU*slow = p;
	int count = 0, ss = 0;
	while (fast )
	{
		if (fast->pnext == NULL)
		{
			ss = 1;//1为偶数
			break;
		}
		count++;
		fast = fast->pnext->pnext;
		slow = slow->pnext;
	}
	if (count==0)
		cout << "List1没有元素" << endl;
	else
	{
		if (ss == 0)
		{
			cout << "平均值在当前位数为:" << count << "   " << "List1中点元素平均值为: " << slow->num << endl;
		}
		else
		{
			cout << "平均值在当前位数为:" << count<<"和"<< count+1 <<"之间"<< "  ";
			cout << "List1中点元素平均值为: " << (float)(slow->num + slow->pnext->num) / 2 << endl;
		}
	}
}
void SearchAddNode2(STU*p)
{
	STU*fast = p;
	STU*slow = p;
	int count = 0, ss = 0;
	while (fast)
	{
		if (fast->pnext == NULL)
		{
			ss = 1;//1为偶数
			break;
		}
		count++;
		fast = fast->pnext->pnext;
		slow = slow->pnext;
	}
	if (count == 0)
		cout << "List2没有元素" << endl;
	else
	{
		if (ss == 0)
		{
			cout << "平均值在当前位数为:" << count << "   " << "List2中点元素平均值为: " << slow->num << endl;
		}
		else
		{
			cout << "平均值在当前位数为:" << count<<"和"<< count-1 <<"之间" << "  ";
			cout << "List2中点元素平均值为: " << (float)(slow->num + slow->pnext->num) / 2 << endl;
		}
	}
}
int main()
{
	STU*p1 = Greatlist1();
	AddNode1(p1);
	Print1(p1);
	SearchAddNode1(p1);
	STU*p2 = GreastList2();
	AddNode2(p2);
	Print2(p2);
	SearchAddNode2(p2);
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_49019274/article/details/115037506