c++模板类(链表),实现正向反向找到链表中最大值,并比较时间差异

本文介绍了一个链表容器模板类的设计与实现,通过具体案例——寻找公司中最高薪员工,展示了正向与反向查找的方法及时间差异。文章深入解析了模板类的定义、迭代器的使用技巧,以及如何在链表中高效地进行数据查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

实现链表容器模板类,利用模板实现找到公司中工资最高的员工的工资,实现正向反向查找,并且比较两种方法的时间差异。

代码实现

公司类Company.h

#ifndef COMPCONT_H
#define COMPCONT_H

#define NUM_EMPLOYEE 10000
#define MAX_SALARY 10000
#include"listTemp.h"
template<class T>
class ListTemp;
class Company
{
private:
	int bestPaid;
	ListTemp<int> container;

public:
	void inputSalary(); //when input employee from keyboard, store the input data in the container for later use;
	
	//please implement the following two methods
	void findBestPaid(); //go through the container to find the best paid employee
	void findBestPaidReverse(); //go through the container in the reverse order to find the best paid employee

	void printBestPaid() const;
}; 

#endif

公司类Company.cpp

#include "company.h"
#include <iostream>
#include <cstdlib> // Header file needed to use srand and rand
#include <ctime> // Header file needed to use time
using namespace std;

void Company::inputSalary()
{
	//set the seed for random number generation	
	srand((unsigned int)time(0));

	for(int i=0; i < NUM_EMPLOYEE; i++)
	{
		int rand_number = rand() % MAX_SALARY;
		container.AddHead(rand_number); // add to the container
	}
}

void Company::findBestPaid()
{
	bestPaid = 0;
	ListTemp<int>::Iterator itr = container.Begin();
	while (!(itr == container.End()))
	{
		if (*itr >= bestPaid)
			bestPaid = *itr;
		itr++;
	}
}

//please implement this
void Company::findBestPaidReverse()
{
	bestPaid = 0;
	ListTemp<int>::Iterator itr = container.End();
	while (!(itr == container.Begin()))
	{
		itr--;
		if (*itr >= bestPaid)
			bestPaid = *itr;
		//itr--;
	}

}

void Company::printBestPaid() const
{
	cout << "The salary of the best-paid employee is: " << bestPaid << endl;
}

// printBestPaid

容器模板类

#ifndef LISTTEMP_H
#define LESTTEMP_H

#define NULL 0
template<class T>
class ListTemp
{
private:
	struct Node
	{
		T data;
		Node *next;
	};

	Node *head;
	int size;

public:

	ListTemp();

	~ListTemp();

	int getLength() const;

	bool isEmpty() const;

	void AddHead(const T newData);

	//************************declaration of the inner iterator class****************************
	class Iterator
	{
		friend class ListTemp<T>;
	private:
		Node *head;
		Node *curr;
		Iterator(Node *hear_ptr, Node *curr_ptr); //constructor with Node parameter, defined as private

	public:

		Iterator(); //default constructor

		Iterator operator++(int); //post-increment of ++

		//please implement this
		Iterator operator--(int); //post-decrement of --

		T& operator*() const;

		bool operator==(const Iterator other) const;

	};//class Iterator

	//please implement this
	Iterator Begin() const;

	//please implement this
	Iterator End() const;

};


//************************implementation of the iterator inner class**************************
template<class T>
ListTemp<T>::Iterator::Iterator()
{
	head = NULL;
	curr = NULL;
}

template<class T>
ListTemp<T>::Iterator::Iterator(Node *head_ptr, Node *curr_ptr)
{
	head = head_ptr;
	curr = curr_ptr;
}

template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::Iterator::operator++(int)
{
	Iterator temp = *this;
	this->curr = curr->next;
	return temp;
}

template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::Iterator::operator--(int)
{
	Iterator temp = *this;
	Node *temp_ptr = this->head;
	while (temp_ptr != NULL && temp_ptr->next != this->curr)
		temp_ptr = temp_ptr->next;
	this->curr = temp_ptr;

	return temp;
}

template<class T>
T& ListTemp<T>::Iterator::operator*() const
{
	return curr->data;
}

template<class T>
bool ListTemp<T>::Iterator::operator==(const Iterator other) const
{
	return curr == other.curr;
}


//************************implementation of the Begin and End position**************************
template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::Begin() const
{
	return Iterator(head, head);
}

template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::End() const
{
	return Iterator(head, NULL);
}


//************************implementation of the linked list class template**************************
template<class T>
ListTemp<T>::ListTemp()
{
	head = NULL;
	size = 0;
}


template<class T>
ListTemp<T>::~ListTemp()
{
	Node *current = head;
	Node *temp = NULL;
	while (current != NULL)
	{
		temp = current;
		current = current->next;
		delete temp;
	}
}


template<class T>
int ListTemp<T>::getLength() const
{
	return size;
}


template<class T>
bool ListTemp<T>::isEmpty() const
{
	return size == 0;
}


template<class T>
void ListTemp<T>::AddHead(const T newData)
{
	Node *temp = new Node;
	temp->next = head;
	temp->data = newData;
	head = temp;

	size++;
}

#endif

测试main函数

//#include "listTemp.h"   
//!!!!千万注意在cpp文件中不能引入模板类.h文件
#include "company.h"
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
	Company cmp;
	cmp.inputSalary();

	long start_t, end_t;
	long start_t2, end_t2;

	start_t = clock();
	cmp.findBestPaid();
	end_t = clock();
	cmp.printBestPaid();
	cout << "time cost: " << ((double)(end_t - start_t)) / CLOCKS_PER_SEC << " seconds" << endl;


	//please measure the time cost
	start_t2 = clock();
	cmp.findBestPaidReverse();
	end_t2 = clock();
	cmp.printBestPaid();
	cout << "time cost of reverse: " << ((double)(end_t2 - start_t2)) / CLOCKS_PER_SEC << " seconds" << endl;


	return 0;
}

实验结果

在这里插入图片描述

经验总结

这是一个应用链表容器,实现迭代器细节,利用容器解决问题的很好的例子。初写模板类可能会遇到一些问题,在此做一些经验总结:

1.模板类的定义与实现要在同一个.h文件中完成,不要把定义和实现分开。
2.定义好的模板类需要引入才能够使用。需要特别注意,引入模板类的时候只能在头文件中引入,此处为在Company.h中引入,切忌不能在任何cpp文件中再加入listTemp.h文件,这样编译器会不明确,会出现
在这里插入图片描述
类似这样的错误。

3.关于模板类的
在这里插入图片描述
在这里插入图片描述
问题,在模板类在声明这些是为了防止命名冲突,需要注意前后的匹配#ifndef,#define与#endif需要同时出现。

3.在实现迭代器的++,–操作时,需要明确–为前置还是后置,准确区分前置与后置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值