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

📄 main.cpp

📁 一个我的数据结构解题集合
💻 CPP
字号:
#include <iostream>
#include "IoUtils.h"
#include "List.h"

using namespace std;

/* 遍历整个整数链表, 查找最小整数,
 * 返回其迭代器
 */
List<int>::Itor seekMinimal(List<int>& list) {

	List<int>::Itor itor = list.begin();
	List<int>::Itor minNode = itor;

	// 查找最小元素
	for ( ; itor != list.end(); itor=itor->next ) {
		if ( itor->data < minNode->data ) { 
			minNode = itor;
		}
	}

	return minNode;

} // seekMinimal(List<int>&)


/* 打印出整数列表
 */
void printIntList(List<int>& list) {

	cout << "List:\n";
	List<int>::Itor itor = list.begin();
	for ( ; itor != list.end(); itor=itor->next)
		cout << "\tData: " << itor->data << "\n";
	cout << endl;

	pause();

} // PrintIntList(List<int>&)


int main() {
	List<int> list;

	cout << "输入一个正整数, 该数字为将要输入的正整数序列元素的数目: ";
	int n = getPositiveInteger();

	// 读入正整数列并依次插入链表
	cout << "输入" << n << "个正整数的序列:" << endl;
	for (int i=0; i<n; ++i) {
		cout << "输入元素" << i << ": ";
		list.insert( list.end(), getPositiveInteger() );	// 在链表尾部插入正整数
	}
	printIntList(list);

	// 查找最小的结点, 并且删除
	while ( list.size() > 0 ) {
		List<int>::Itor minNode = seekMinimal(list);
		int min = minNode->data;

		cout << "\n最小结点数据值: " << min << endl;
		list.remove(minNode);

		cout << "删除最小结点后的链表: " << endl;
		printIntList(list);
	}

	return 0;
} // main()

⌨️ 快捷键说明

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