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

📄 c++.txt

📁 是一个数学的应用问题:   已知n个人(以编号1
💻 TXT
字号:
#include<iostream.h>
#include<conio.h>

struct People
{
	int pwd;
	int id;
	People *next;
};

class List
{
public:
	List(int n);
	People* Find(People *p, int m);
	int Delete(People *p);
	int n;
	People* Head() { return head; }

private:
	People *head, *tail;
};

List::List(int n)
{
	head = NULL;
	this->n = n;
	for (int i=0; i<n; i++)
	{
		People *t = new People;

		cout << "输入此人持有的密码:";
		cin >> t->pwd;
		t->id = i+1;

		if (head == NULL)
		{
			head = t;
			tail = t;
			tail->next = NULL;
		}
		else
		{
			tail->next = t;
			tail = t;
			tail->next = NULL;
		}		
	}
	tail->next = head;
}

People* List::Find(People *p, int m)     //从p开始寻找,找到第m-1个节点
{
	int j = 1;
	while (j < m-1)
	{
		p = p->next;
		j++;
	}
	return p;
}

int List::Delete(People *p)             //删除第m个节点,并返回此人密码
{
	People *t = p->next;
	p->next = t->next;
	int k = t->pwd;	
	cout << "第" << t->id << "个出列" << endl;
	delete t;
	return k;
} 
void main(void)
{
	List l(7);
	int j = 0;
	int m;
	cout << "请输入初始上限:";
	cin >> m;
	People *temp = l.Head();
	cout << "出列顺序为: " << endl;

	while (j < l.n)
	{
		People *p = l.Find(temp, m);
		m = l.Delete(p);
		temp = p->next;
		j++;
	}
	getch();
}

⌨️ 快捷键说明

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