#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;
}
单链表C++ 模板
最新推荐文章于 2019-03-06 18:24:03 发布