#include <iostream> using namespace std; template<class T> class List { public: List() :pFirst(nullptr) {} //构造函数 void Add(T& val) { Node* pNode = new Node; pNode->pT = &val; pNode->pNext = pFirst; pFirst = pNode; } //在Link表头添加新结点 void Remove(T& val) { Node* pNode = pFirst; Node* pPrev = nullptr; while (pNode) { if ((pNode->pT) == val) { if (pPrev) { pPrev->pNext = pNode->pNext; } else { pFirst = pNode->pNext; } delete pNode; return; } pPrev = pNode; pNode = pNode->pNext; } } //在Link中删除含有特定值的元素 T Find(T& val) { Node* pNode = pFirst; while (pNode) { if ((pNode->pT) == val) { return pNode->pT; } pNode = pNode->pNext; } return nullptr; } //查找含有特定值的结点 void PrintList() { Node pNode = pFirst; while (pNode) { std::cout << (pNode->pT) << std::endl; pNode = pNode->pNext; } } //打印输出整个链表 ~List() { Node pNode = pFirst; while (pNode) { Node* pNext = pNode->pNext; delete pNode; pNode = pNext; } } protected: struct Node { Node* pNext; T* pT; }; Node* pFirst; //链首结点指针 }; class Student { private: std::string name_; int id_; public: Student(const std::string& name, int id) :name_(name), id_(id) {} bool operator==(const Student& other) const { return id_ == other.id_; } friend std::ostream& operator<<(std::ostream& os, const Student& student); }; std::ostream& operator<<(std::ostream& os, const Student& student) { os << "Name: " << student.name_ << ", ID: " << student.id_; return os; } int main() { List<Student> classList; Student s1("张三", 1001); Student s2("李四", 1002); Student s3("王五", 1003); //添加学生 classList.Add(s1); classList.Add(s2); classList.Add(s3); //打印学生 classList.PrintList(); std::cout << std::endl; //查找学生 Student s4("李四", 1002); Student* pStudent = classList.Find(s4); if (pStudent) { std::cout << "Found student: " << *pStudent << std::endl; } else { std::cout << "Student not found." << std::endl; } std::cout << std::endl; //删除学生 classList.Remove(s2); classList.PrintList(); return 0; }请见查找学生进行完善
时间: 2024-01-15 18:04:05 浏览: 76
在List类中的Find函数中,如果没有找到指定的元素,应该返回一个T类型的默认值,而不是nullptr。因为T类型可以是任何类型,不一定是指针类型。可以在List类模板定义中加入一个默认的模板参数,指定T类型的默认值,例如:
```
template<class T, T defaultValue = T{}> class List {
//...
T Find(T& val) {
Node* pNode = pFirst;
while (pNode) {
if (*(pNode->pT) == val) {
return *(pNode->pT);
}
pNode = pNode->pNext;
}
return defaultValue;
}
//...
};
```
这样,在默认情况下,T类型的默认值为0,如果T是一个指针类型,则T类型的默认值为nullptr。如果需要使用其他类型的默认值,可以通过指定模板参数来实现。
相关问题
改进以下代码#include<iostream> #include<string.h> #include<stdio.h> using namespace std; //链表类的前向声明 template<class T> class list; template<class T> //声明模板 class node { //定义结构模板0-[-[ T val; //val取任意类型,即模板参数类型 node<T>* next; //此处node为结构模板 public: node(){ next = NULL; } node(T a) { val = a; next = NULL; } friend class list<T>; }; //请完成链表类的设计 template<class T> class list { public: list(){} void insert(T t) { if(pFirst==NULL) { pFirst=new node(t); pTail=pFirst; } else { node<T> *p=new node(t); pTail->next=p; pTail=p; } } void print() { for(node<T> *p=pFirst;p;p=p->next) cout<<p->val; } private: static node<T> pFirst; static node<T> pTail; }; template <class T> node<T> list<T>::pFirst=NULL; template <class T> node<T> list<T>::pTail=NULL; int main() { int kind = 0; // 0:表示整型,1:单精度浮点数, 2:字符串 int cnt = 0; cin >> kind >> cnt; //整数链表 if (kind == 0) { list<int> intlist; int nTemp = 0; for (int i = 0; i < cnt; i++) { cin >> nTemp; intlist.insert(nTemp); } intlist.print(); } //浮点数链表 else if (kind == 1){ list<float> floatlist; float fTemp = 0; cout.setf(ios::fixed); cout.precision(1); for (int i = 0; i < cnt; i++) { cin >> fTemp; floatlist.insert(fTemp); } floatlist.print(); } //字符串链表 else if (kind == 2){ list<string> charlist; char temp[100] ; for (int i = 0; i < cnt; i++){ cin >> temp; charlist.insert(temp); } charlist.print(); } else cout << "error"; return 0; }
以下是改进后的代码:
```
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
template<class T>
class Node {
public:
T val;
Node<T>* next;
Node() {
next = NULL;
}
Node(T a) {
val = a;
next = NULL;
}
friend class List<T>;
};
template<class T>
class List {
private:
Node<T>* pFirst;
Node<T>* pTail;
public:
List() {
pFirst = NULL;
pTail = NULL;
}
void insert(T t) {
Node<T>* p = new Node<T>(t);
if (pFirst == NULL) {
pFirst = p;
pTail = pFirst;
}
else {
pTail->next = p;
pTail = p;
}
}
void print() {
for (Node<T>* p = pFirst; p != NULL; p = p->next) {
cout << p->val << " ";
}
}
};
int main() {
int kind = 0; // 0:表示整型,1:单精度浮点数, 2:字符串
int cnt = 0;
cin >> kind >> cnt;
//整数链表
if (kind == 0) {
List<int> intList;
int nTemp = 0;
for (int i = 0; i < cnt; i++) {
cin >> nTemp;
intList.insert(nTemp);
}
intList.print();
}
//浮点数链表
else if (kind == 1) {
List<float> floatList;
float fTemp = 0;
cout.setf(ios::fixed);
cout.precision(1);
for (int i = 0; i < cnt; i++) {
cin >> fTemp;
floatList.insert(fTemp);
}
floatList.print();
}
//字符串链表
else if (kind == 2) {
List<string> stringList;
string temp;
for (int i = 0; i < cnt; i++) {
cin >> temp;
stringList.insert(temp);
}
stringList.print();
}
else
cout << "error";
return 0;
}
```
改进说明:
1. 将链表节点的定义和链表的定义分开,使代码更加清晰易懂;
2. 在链表类中增加私有成员变量pFirst和pTail,用于指向链表的头和尾;
3. 在链表类的构造函数中初始化pFirst和pTail为NULL;
4. 修改了链表的insert方法,使其可以正确处理插入第一个节点的情况;
5. 修改了链表的print方法,使其可以正确处理链表为空的情况;
6. 修改了字符串链表的输入方式,使用string类型代替char数组类型,更加方便易用。
阅读全文