⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 浙江工商大学 计算机与信息工程学院实验报告
💻 CPP
字号:
#include <cstdlib>
#include <iostream>
using namespace std;

typedef struct node
{
	int data;
	struct node * next;
}Node;
typedef Node* Node_pointer;

Node_pointer list_creat(int n)
{
	Node_pointer head, tail, p;
	tail = head = new Node;

	for (int i = 0 ; i < n ; i++)
	{
		p = new Node;
		p->data = rand() % 1000;
		tail->next = p;
		tail = p;
	}
	tail->next = NULL;
	return head;
}

bool list_insert(Node_pointer head, int x, int m)
{
	Node_pointer p,q,tmp;
	for (p = head->next ; p && p->data != x ; p = p->next);
	if (!p) return false;
	tmp = p->next;
	for (int i = 0 ; i < m ; i++) {
		q = new Node;
		q->data = rand() % 1000;
		p->next = q;
		p = p->next;
	}
	p->next = tmp;
	return true;
}

int list_delete(Node_pointer head, int min, int max)
{
	int count = 0;
	Node_pointer tmp;
	for (; head->next ;)
	{
		if (head->next->data > min && head->next->data < max)
		{
			tmp = head->next;
			head->next = head->next->next;
			delete tmp;
			count++;
		}
		else
			head = head->next;
	}
	return count;
}

void display(Node_pointer head)
{
	for (head = head->next ; head ; head = head->next)
		cout << head->data << ' ';
	cout << endl;
}

int main(void)
{
	int count;
	int n, m, x;
	int min, max;
	Node_pointer head;

	cin >> n;
	head = list_creat(n);
	display(head);

	cin >> x >> m;
	if (!list_insert(head, x, m))
		cout << "节点为找到" << endl;
	display(head);

	cin >> min >> max;
	count = list_delete(head, min, max);
	cout << "total delete " << count << endl;
	display(head);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -