链表的操作复习

链表的操作复习

1.链表的建立,查找与删除

#include<Windows.h>
#include<cstdio>
using namespace std;
struct Node{
	int data;
	Node*next;
};
//建立一个长度为n的非递减变量
Node* Build(Node* &head,Node* &tail,int n) {
	int*a = new int[n] {0, 2, 2, 3, 4, 5, 7, 7, 9,13};
	for (int i = 0; i < n; ++i) {
		Node*p = new Node;
		p->data = a[i],p->next = NULL;
		if (head == NULL)
			head = tail = p;
		else 
			tail->next = p,tail = p;
	}
	return head;
}

//输出链表
int Output(Node*head) {
	if (head == NULL)
		return -1;
	Node*temp = head;
	while (temp) {
		printf("%d ", temp->data);
		temp = temp->next;
	}
	printf("\n");
	return 0;
}

//删除
void Del(Node*head,Node*tail) {
	while (head) {
		Node*temp = head;
		head = head->next;
		delete temp;
		temp = NULL;
	}
}

Node* Insert(Node* &head,Node* &tail,int X) {
	Node*p = new Node;
	p->data = X,p->next = NULL;
	//空表
	if (head == NULL)
		return head;
	else {
		//只需要头遍历即可,只要从左向右
		//第一个必X小的即可
		if (X <= head->data) 
			p->next = head,head = p;
		else {
			Node*pre = head,*post = head->next;
			bool flag = true;
			while (post) {
				if (X <= post->data) {
					pre->next = p,p->next = post;
					flag = false;
					break;
				}
				pre = pre->next,post = post->next;
			}
			if (flag) {
				tail->next = p;
				tail = p;
			}
		}
		return head;
	}
}

void test(Node*head,Node*tail) {
	Insert(head, tail, 11);
	Output(head);
	Insert(head, tail, 14);
	Output(head);
	Insert(head, tail, -1);
	Output(head);
	Insert(head, tail, 1);
	Output(head);
	Insert(head, tail, 2);
	Output(head);
}
int main() {
	Node*head, *tail;
	head = tail = NULL;
	head = Build(head, tail, 10);
	printf("The original list: ");
	Output(head);
	test(head,tail);
	Del(head, tail);
	//一定要归NULL
	head = tail = NULL;
	printf("%d\n", Output(head));
	system("pause");
	return 0;
}

2.约瑟夫环

2.1.题目

​ Josephu 问题 : 有 N(N >= 1) 个人围成一圈 , 从第 i (i<=N) 个人开始从 1 报数 , 数到 m(m >= 1) 时 ,此人出列 ; 下一人重新从 1 开始报数 , 再数到 m 时 , 又一人出列 , 直至所有人全部出列 .要求写一个算法实现按出列的先后次序得到一个新的序列 , 如 N=5, i =1, m=3 则新序列是 3 、 1 、 5 、 2 、 4.

2.2.code

​ 没什么好说的,模拟整个过程就好,唯一需要注意的是我们在出队的时候,当只有一个人的时候,删除需要单独拿出来考虑。

#include<iostream>
using namespace std;
struct  Node{
	int num;
	Node*next;
};

void Build(Node* &head, Node* &tail,int n) {
	for (int i = 1; i <= n; ++i) {
		Node*p = new Node;
		p->num = i;
		p->next = NULL;
		if (head == NULL)
			head = tail = p;
		else {
			tail->next = p;
			tail = p;
		}
	}
}

void Josephu(int N,int index,int m) {
	Node *head, *tail;
	head = tail = NULL;
	Build(head, tail, N);
	//模拟整个过程,开始输出
	tail->next = head;
	Node* temp = head;
	//temp指向index
	for (int i = 1; i < index; ++i)
		temp = temp->next;
	while (temp) {
		for (int i = 1; i < m-1; ++i)
			temp = temp->next;
		if (temp) {
			//删除temp->next
			cout << temp->next->num << " ";
			if (temp->next == temp) {
				delete temp;
				temp = NULL;
			}
			else {
				Node*p = temp->next;
				temp->next = p->next;
				delete p;
				p = NULL;
			}
		}
	}
	cout << endl;
}
int main() {
	int N, index, m;
	cin >> N >> index >> m;
	Josephu(N, index, m);
	system("pause");
	return 0;
}
posted @ 2021-03-11 14:51  关河梦断  阅读(68)  评论(0)    收藏  举报