main.cpp

来自「一个我的数据结构解题集合」· C++ 代码 · 共 59 行

CPP
59
字号
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "IoUtils.h"
#include "FreqList.h"
using namespace std;


int main() {
	int noErrors = 0;

	cout << "FreqList::search:\n"
		 << "\t为方便起见, 本程序先创建一个FreqList, 并在其中填充10个100以内的随机数\n"
		 << "\t然后打印该链表, 随后让用户输入想要查找的元素,\n"
		 << "\t每次查找后首先打印访问结果 (即是否找到),\n"
		 << "\t然后打印该链表, 以便观察其变化\n"
		 << endl;


	FreqList<int> list;

	srand((unsigned int)time(NULL));

	int i=0;
	while (i<10) {
		int val = rand()%99 + 1;
		if ( list.has(val) ) continue;
		list.insert(val);
		++i;
	}
	list.print();

	
	while (true) try {

		cout << "请输入想要查找的元素值 (0: 退出): " << flush;
		int val = getInteger();

		if (val == 0)
			return noErrors;

		cout << "\n";
		if ( list.search(val) )
			cout << "找到元素 " << val << "\n";
		else
			cout << "未找到元素 " << val << "\n";
		cout << endl;

		list.print();

	} catch (const std::exception& e) {
		cerr << "捕捉到异常!" << endl;
		cerr << e.what() << endl;
		++noErrors;
	}

	return noErrors;
} // main()

⌨️ 快捷键说明

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