单链表C++ 模板

#include "stdafx.h"

#include <iostream>
#include <string>
using namespace std;

/************************* 异常类 **************************/
// 异常类
class MyException{
private:
	string mesg;
public:
	MyException(const char *str) :mesg(const_cast<char*>(str)){  }
	string what(){  return mesg; }
};
/********************** 单链表类 *************************/
// 链表结点
template <class T>
struct LinkNode{
	T data;
	LinkNode<T> *next;
	LinkNode(LinkNode<T> *ptr = NULL){
		next = ptr;
	}
	LinkNode(const T& item, LinkNode<T> *ptr = NULL){
		data = item;
		next = ptr;
	}
};

// 单链表
template <typename T>
class List{
private:
	LinkNode<T>* head;
public:
	List(T data);
	List(LinkNode<T> *list);
	~List();
	void AddNodeAtTail(T item);                 // 尾插法
	void AddNodeAtHead(T item);                 // 头插法
	void DispAllNodes();                        // 显示所有链表
	int GetLength();                            // 计算链表长度
	void ClearList();                           // 清空链表
	void DeleteNode(int);                       // 删除结点
	void ModifyNode(T item, int pos);           // 修改第pos个结点
	int LocatItem(T item);                      // 找到出现item的位置
	LinkNode<T>* GetItem(int pos);              // 获取第pos个元素结点
	static T InputElem();                              // 输入结点
	static int InputPos();                             // 输入位置
};

// 析构函数
template <class T>
List<T>::~List(){
	while (head){
		LinkNode<T>* lp = head;
		head = head->next;
		delete lp;
	}
	delete head;
}
//构造函数
template <typename T>
List<T>::List(T data=0){
	head = new LinkNode<T>;
	head->next = NULL;
	head->data = data;
}
//复制构造函数
template <class T>
List<T>::List(LinkNode<T>* list){
	head = list;
}
// 头插法插入结点
template <class T>
void List<T>::AddNodeAtHead(T data){
	LinkNode<T>* newNode = new LinkNode<T>;
	newNode->data = data;
	newNode->next = head;
	head = newNode;
}
//尾插法插入结点
template<class T>
void List<T>::AddNodeAtTail(T item){
	LinkNode<T>* list = head;
	while (list->next) list = list->next;
	LinkNode<T>* newNode = new LinkNode<T>;
	newNode->data = item;
	newNode->next = NULL;
	list->next = newNode;
}
// 显示所有链表
template<class T>
void List<T>::DispAllNodes(){
	LinkNode<T>* list = head;
	while (list->next != NULL){
		list = list->next;
		cout << list->data << endl;
	}
	cout << endl;
}
// 计算链表长度
template<class T>
int List<T>::GetLength(){
	LinkNode<T>* list = head;
	int n = 0;
	while (list->next){
		list = list->next;
		n++;
	}
	return n;
}
// 清空链表
template <class T>
void List<T>::ClearList(){
	while (head->next){
		LinkNode<T>* lp = head;
		head = head->next;
		delete lp;
	}
	head->next = NULL;
}
// 删除结点
template<typename T>
void List<T>::DeleteNode(int pos){
	if (pos <= 0 || pos > GetLength())
		throw MyException("DeleteNode, pos out of range.");
	int n = 0;
	LinkNode<T>* list = head;
	while (list->next && n < pos-1){
		list = list->next;
		n++;
	}
	LinkNode<T>* lp = list->next;
	list->next = lp->next;
	delete lp;
}
// 修改结点
template<typename T>
void List<T>::ModifyNode(T item, int pos){
	if (pos <= 0 || pos > GetLength())
		throw MyException("ModifyNode, pos out of range");
	int n = 0;
	LinkNode<T>* list = head;
	while (list->next && n < pos){
		list = list->next;
		n++;
	}
	list->data = item;
}
// 找到出现item的位置
template <class T>
int List<T>::LocatItem(T item){
	LinkNode<T>* list = head;
	int n = 0;
	while (list){
		
		if (item == list->data)
			return n;
		n++;
		list = list->next;
	}
	return -1;
}
// 获取第pos个元素结点
template <class T>
LinkNode<T>* List<T>::GetItem(int pos){
	if (pos <= 0 || pos > GetLength())
		throw MyException("GetItem,pos out of range.");
	LinkNode<T>* list = head;
	int n = 0;
	while (list && n < pos){
		n++;
		list = list->next;
	}
	return list;
}
// 输入结点
template <class T>
T List<T>::InputElem(){
	T data;
	cout << "Input element:" << endl;
	cin >> data;
	return data;
}
// 输入位置
template <class T>
int List<T>::InputPos(){
	int n;
	cout << "Input pos:";
	cin >> n;
	return n;
}
/*********************** 学生类 ************************/
// 学生信息类
class Student{
private:
	int age;
	char *name;
	double score;
public:
	Student(int age = 0, char *name = "", double score = 0.0) :age(age), name(name), score(score)
	{  }
	int getAge(){ return age; }
	char* getName(){ return name; }
	double getScore(){ return score; }
	void setAge(int age){ this->age = age; }
	void setName(char *name){ this->name = name; }
	void setScore(double score){ this->score = score; }
	friend ostream& operator<<(ostream &out,  Student &s);
	friend istream& operator>>(istream &in,  Student &s);
	void operator=(const Student &s);
	bool operator==(const Student &s);
	bool operator>(const Student &s);
	bool operator<(const Student &s);
};

// 输出流重载
ostream& operator<<(ostream &out,Student &s){
	out << "name:" << s.getName() << "\tage:" << s.getAge() << "\tscore:" << s.getScore();
	return out;
}
// 输入流重载
istream& operator>>(istream &in, Student &s){
	char *name = new char[32]{0};
	int age;
	double score;
	cout << "name:"; 
	in >> name;
	cout << "age:"; 
	in >> age;
	cout << "score:"; 
	in >> score;
	s.setAge(age);
	s.setName(name);
	s.setScore(score);
	return in;
}
// 小于符号重载
bool Student::operator<(const Student &s){
	return this->score < s.score;
}
// 大于符号重载
bool Student::operator>(const Student &s){
	return this->score > s.score;
}
// 等于符号重载
bool Student::operator==(const Student &s){
	if (strcmp(this->name,name) == 0 && this->age == s.age && this->score == s.score)
		return 1;
	return 0;
}
// 赋值符号重载
void Student::operator=(const Student &s){
	this->age = s.age;
	this->score = s.score;
	this->name = s.name;
}
//

// 菜单
int menu(){
	cout << "1.头插法插入             2.尾插法插入" << endl;
	cout << "3.显示所有结点           4.获取长度" << endl;
	cout << "5.清空链表               6.删除结点" << endl;
	cout << "7.修改结点               8.找到结点位置" << endl;
	cout << "9.获取指定位置结点       0.退出" << endl;
	cout << "SELECT:";
	char str[32] = { 0 };
	cin >> str;
	fflush(stdin);
	return atoi(str);
}

int main(int argc, char *argv[]){
	List<Student> list(Student(21, "fulianzhou", 90));
	
	while (true){
		try{
			switch (menu()){
			case 0:
				cout << "Bey Bey!" << endl;
				return 0;
			case 1:list.AddNodeAtHead(List<Student>::InputElem()); break;
			case 2:list.AddNodeAtTail(List<Student>::InputElem()); break;
			case 3:list.DispAllNodes(); break;
			case 4:cout << "length = " << list.GetLength() << endl; break;
			case 5:list.ClearList(); break;
			case 6:list.DeleteNode(List<Student>::InputPos()); break;
			case 7:list.ModifyNode(List<Student>::InputElem(), List<Student>::InputPos()); break;
			case 8:cout << "pos = " << list.LocatItem(List<Student>::InputElem()) << endl; break;
			case 9:cout << list.GetItem(List<Student>::InputPos())->data << endl; break;
			}
		}
		catch (MyException ex){
			cout << ex.what() << endl;
		}
		system("pause");
		system("cls");
	}

	return 0;
}


面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值