Visual studio2022在运行以下程序时会在MakeEmpty函数处引发异常,请帮我修改#include<iostream> #include<fstream>…
- User
Visual studio2022在运行以下程序时会在MakeEmpty函数处引发异常,请帮我修改#include #include #include ; using namespace std; class student { int id; string name; int score; public: student(int i = 0, string n = "\0", int s = 0); void show(); bool operator<(student stu); bool operator!=(student stu); void input(int i = 0, string n = "\0", int s = 0); friend ostream& operator<<(ostream& out, student stu); void operator=(const student stu); void putkey(int i) { id = i; }; }; void student:: operator=(const student stu) { id = stu.id; name = stu.name; score = stu.score; } ostream& operator<<(ostream& out, student stu) { out << stu.id << '\t' << stu.name << '\t' << stu.score << endl; return out; } void student::input(int i, string n, int s) { id = i; name = n; score = s; } student::student(int i, string n, int s) { id = i; name = n; score = s; } void student::show() { cout << "id= " << id << " name= " << name << " score= " << score << endl; } bool student:: operator<(student stu) { return(score < stu.score); } bool student:: operator!=(student stu) { return (id != stu.id); } template class List; template class Node { T info; Node* link; public: Node(); Node(const T& i); friend class List; }; template Node::Node() { link = NULL; } templateNode::Node(const T& i) { info = i; link = NULL; } templateclass List { Node* head, * tail; public: List(); List(List& lis); ~List(); void MakeEmpty(); Node* CreatNode(T data); void InsertFront(Node* p); void PrintList(); void InsertRear(Node* p); Node* Find(T inf); Node* DeleteNode(Node* p); int Length(); void InsertOrder(Node* p); void Reverse(); }; templatevoid List::Reverse() { Node* temp , * p = head, q=head->link; head->link = tail; while (tail != q) { temp = q; while (temp != tail) { p = temp; temp = temp->link; } tail->link = p; tail = p; tail->link = NULL; } } templatevoid List::InsertOrder(Node p) { Node* temp = head->link, * p2 = head; while (temp != NULL&& temp->info < p->info) { p2 = temp->link; temp = temp->link; } if (temp!=NULL&&p->info < temp->info) { p2->link = p; p->link = temp; } else { tail->link = p; tail = p; tail->link = NULL; } } template int List::Length() { int i = 0; Node* temp = head->link; while (temp != 0) { i++; temp=temp->link; } return i; } template Node* List::DeleteNode(Node* p) { Node* temp = head, t=head; while (temp != NULL && temp->link != p) { t = temp->link; temp = temp->link; } if (temp->link == p) { t->link = p->link; delete p; } return head; } template Node List::Find(T inf) { Node* temp = head->link; while (temp != NULL&&temp->info != inf)temp=temp->link; return temp; } template void List::PrintList() { Node* temp = head->link; while (temp!= 0) { cout << temp->info; temp = temp->link; } cout << endl; } templatevoid List:: InsertRear(Node* p){ tail->link = p; tail = p; p->link = NULL; } template void List::InsertFront(Node* p) { p->link = head->link; head->link = p; if (head == tail) tail = p; } template Node* List::CreatNode(T data) { Node* temp = new Node(data); return temp; } /template List::List(List& lis) { head = tail = new Node(); Node iter = lis.head->link; // 原链表中的第一个结点 while (iter != NULL) { Node* newNode = new Node(iter->info); // 复制原结点数据 InsertRear(newNode); // 插入到新链表尾部 iter = iter->link; // 遍历下一个结点 } } temp = lis.head->link; while (temp != NULL) { Node* pnew = CreatNode(temp->info); InsertFront(pnew); temp=temp->link; } } template List::List() { head = tail = new Node(); } templatevoid List::MakeEmpty() { Node* temp; while (head->link != NULL) { temp = head->link; head->link = temp->link; delete temp; } tail = head; } templateList::~List() { if(head->link)MakeEmpty(); delete head; }
- Assistant
在修改之前,需要先了解代码中的错误。在MakeEmpty函数中,程序试图删除链表中所有的节点,但是在执行完第一次删除后,head节点是一直存在的,并没有被释放,因此在第二次删除时,会引发异常。因此,需要在删除完所有节点后,再释放head节点。修改后的代码如下:
templatevoid List::MakeEmpty() { Node* temp; while (head->link != NULL) { temp = head->link; head->link = temp->link; delete temp; } delete head; head = tail = nullptr; // 将head和tail指针设置为nullptr }
除此之外,建议将头文件的顺序调整为:先包含和,然后再包含。另外,尽量不要在头文件中使用using namespace std,以避免可能的命名冲突问题。完整代码见下: