| #include <Windows.h> |
| #include <iostream> |
| #include <string> |
| using namespace std; |
| class Student { |
| public: |
| string id; |
| string name; |
| int age; |
| bool operator==(const Student& s) { return id == s.id; } |
| bool operator!=(const Student& s) { return id != s.id; } |
| bool operator<(const Student& s) { return id < s.id; } |
| Student() : name("小明"), age(18) {} |
| friend istream& operator>>(istream& is, Student& s) { |
| is >> s.id >> s.name >> s.age; |
| getchar(); |
| return is; |
| } |
| friend ostream& operator<<(ostream& os, const Student& s) { |
| os << "学号:" << s.id << " 姓名:" << s.name << " 年龄:" << s.age |
| << endl; |
| return os; |
| } |
| }; |
| template <class T> class List; |
| |
| template <class T> class LNode { |
| private: |
| LNode* next; |
| T Data; |
| public: |
| friend class List<T>; |
| LNode() { next = nullptr; } |
| void showdata() { cout << Data << endl; } |
| }; |
| |
| |
| template <class T> class List { |
| private: |
| LNode<T>* head; |
| public: |
| friend class LNode<T>; |
| List(); |
| ~List(); |
| LNode<T>* FindKth(int K); |
| |
| LNode<T>* Find(T data); |
| |
| void |
| Delete(int pos = |
| 1); |
| void Insert(T data, int pos = 1); |
| void PrintList(); |
| int getLength(); |
| void ReverseList(); |
| }; |
| |
| |
| |
| template <class T> List<T>::List() { |
| head = new LNode<T>; |
| head->next = nullptr; |
| } |
| |
| template <class T> List<T>::~List() { |
| while (head->next) { |
| Delete(1); |
| } |
| delete head; |
| } |
| |
| |
| template <class T> LNode<T>* List<T>::FindKth(int K) { |
| LNode<T>* p = head->next; |
| if (!p) { |
| cout << "Element not found,This List is empty!" << endl; |
| return nullptr; |
| } |
| int pos = 1; |
| while (p->next && pos < K) { |
| p = p->next; |
| pos++; |
| } |
| if (pos == K) |
| return p; |
| else { |
| cout << "The location is illegal." << endl; |
| return nullptr; |
| } |
| } |
| |
| |
| |
| template <class T> LNode<T>* List<T>::Find(T data) { |
| LNode<T>* p = head->next; |
| if (!p) { |
| cout << "Element not found,This List is empty!" << endl; |
| return nullptr; |
| } |
| while (p->next && p->Data != data) |
| p = p->next; |
| if (p->Data != data) { |
| cout << "Element not found!" << endl; |
| return nullptr; |
| } |
| else |
| return p; |
| } |
| |
| |
| template <class T> |
| void List<T>::Delete(int pos) { |
| LNode<T>* p = head->next; |
| if (!p) { |
| cout << "Deleted failed,This List is empty!" << endl; |
| return; |
| } |
| if (pos == 1) { |
| head->next = p->next; |
| if (p) |
| delete p; |
| return; |
| } |
| LNode<T>* s = FindKth(pos - 1); |
| if (!s) { |
| cout << "The " << pos-1 << " node does not exist!" << endl; |
| return; |
| } |
| p = s->next; |
| if (!p) { |
| cout << "The " << pos << " node does not exist!" << endl; |
| return; |
| } else { |
| s->next = p->next; |
| delete p; |
| cout << "Successfully deleted!" << endl; |
| } |
| } |
| |
| |
| template <class T> void List<T>::Insert(T data, int pos) { |
| LNode<T>* p = new LNode<T>(); |
| p->Data = data; |
| if (pos == 1) { |
| p->next = head->next; |
| head->next = p; |
| cout << "Successfully Inserted!" << endl; |
| return; |
| } |
| LNode<T>* s = FindKth(pos - 1); |
| if (!s) { |
| cout << "The location is illegal,Insertion failed!" << endl; |
| return; |
| } |
| p->next = s->next; |
| s->next = p; |
| cout << "Successfully Inserted!" << endl; |
| } |
| |
| |
| template <class T> int List<T>::getLength() { |
| LNode<T>* p = head; |
| int cnt = 0; |
| while (p->next) { |
| p = p->next; |
| cnt++; |
| } |
| return cnt; |
| } |
| |
| template <class T> void List<T>::PrintList() { |
| LNode<T>* p = head; |
| if (!head->next) |
| cout << "This List is empty!" << endl; |
| int cnt = 0; |
| while (p->next) { |
| p = p->next; |
| cnt++; |
| cout << "The " << cnt << "th Data is:"; |
| p->showdata(); |
| } |
| cout << "The list has " << cnt << " elements in total." << endl; |
| } |
| |
| template <class T> void List<T>::ReverseList() { |
| LNode<T>* now = head->next; |
| if (!now) { |
| cout << "This List is empty!" << endl; |
| return; |
| } |
| LNode<T>* tmp = now->next; |
| while (tmp) { |
| now->next = tmp->next; |
| tmp->next = head->next; |
| head->next = tmp; |
| tmp = now->next; |
| } |
| } |
| |
| template <class T> void input(List<T>& Head) { |
| T data; |
| cout << "请输入链表元素(以空格间隔,回车结束):"; |
| cin >> data; |
| Head.Insert(data); |
| } |
| template <class T> void remove(List<T>& Head) { |
| int i; |
| cout << "请输入i(删除链表第i个元素):"; |
| cin >> i; |
| getchar(); |
| |
| Head.Delete(i); |
| } |
| template <class T> void findKth(List<T>& Head) { |
| int K; |
| LNode<T>* s = nullptr; |
| cout << "(按序号查找)请输入K,查找链表中第K个元素:"; |
| cin >> K; |
| getchar(); |
| s = Head.FindKth(K); |
| if (!s) |
| cout << "未找到!" << endl; |
| else { |
| cout << "查找成功!该元素为:"; |
| s->showdata(); |
| } |
| } |
| template <class T> void findData(List<T>& Head) { |
| string data; |
| cout << "请输入待查找学生的学号:"; |
| cin >> data; |
| Student t; |
| t.id = data; |
| getchar(); |
| LNode<T>* s = nullptr; |
| s = Head.Find(t); |
| if (!s) |
| cout << "未找到该元素!" << endl; |
| else { |
| cout << "查找成功!该元素在链表中。"; |
| } |
| } |
| template <class T> void insert(List<T>& Head) { |
| int i; |
| T data; |
| cout << "在链表第i个位置上插入一名学生信息,输入i:"; |
| cin >> i; |
| cout << "输入该学生信息,学号 姓名 年龄以空格间隔,回车结束:"; |
| cin >> data; |
| Head.Insert(data, i); |
| } |
| template <class T> inline void getlength(List<T>& Head) { |
| cout << "该链表长度为:" << Head.getLength() << endl; |
| } |
| template <class T> void printList(List<T>& Head) { |
| cout << "---------------------- List P --------------------" << endl; |
| Head.PrintList(); |
| } |
| template <class T> void reverseList(List<T>& Head) { |
| T data; |
| cout << "---------------------- List P --------------------" << endl; |
| Head.PrintList(); |
| cout << endl; |
| |
| Head.ReverseList(); |
| cout << "------------- After reverse, List P ---------------" << endl; |
| Head.PrintList(); |
| } |
| int main() { |
| List<Student> P; |
| int choice; |
| cout << "1 添加一名学生信息" << endl; |
| cout << "2 删除链表第i个元素" << endl; |
| cout << "3 按序号查找链表中第K个元素" << endl; |
| cout << "4 按值查找元素data" << endl; |
| cout << "5 在链表第i个位置上插入元素data" << endl; |
| cout << "6 获取该链表长度" << endl; |
| cout << "7 打印所有学生信息" << endl; |
| cout << "8 逆转该链表" << endl; |
| cout << "9 结束" << endl; |
| while (1) { |
| cout << "菜单选择(1-6):"; |
| cin >> choice; |
| getchar(); |
| switch (choice) { |
| case 1: input(P); break; |
| case 2: remove(P); break; |
| case 3: findKth(P); break; |
| case 4: findData(P); break; |
| case 5: insert(P); break; |
| case 6: getlength(P); break; |
| case 7: printList(P); break; |
| case 8: reverseList(P); break; |
| case 9: break; |
| default: cout << "输入错误,请重新输入"; |
| } |
| if (choice == 9) |
| exit(0); |
| cout << "按回车键继续…" << endl; |
| getchar(); |
| }; |
| return 0; |
| } |