你的功能實在是太複雜了。功能越複雜,它包含的錯誤就越多,讀起來就越困難。:)
函數可以寫成下面的方式。我假設已經定義了以下功能
getNext, setNext, getPrev, setPrev
^^^^
給你。
bool StudentLinkedList::insertStudentAfter(StudentNode *student,
const std::string &name)
{
StudentNode *current = headP;
while (current != nullptr && current->getName() != name)
{
current = current->getNext();
}
bool success = current != nullptr;
if (success)
{
student->setPrev(current);
student->setNext(current->getNext());
if (current->getNext() != nullptr)
{
current->getNext()->setPrev(student);
}
current->setNext(student);
}
return success;
}
考慮到,如果該列表還具有稱爲像尾的數據成員,則函數還具有改變尾變量如果current->getNext
等於nullptr
。
例如
if (current->getNext() != nullptr)
{
current->getNext()->setPrev(student);
}
else
{
tailP = student;
//^^^^
}
這裏是一個示範項目,顯示瞭如何在類可以用你的方法來定義
#include <iostream>
#include <string>
class StudentLinkedList
{
private:
class StudentNode
{
private:
StudentNode *next;
StudentNode *prev;
std::string name;
public:
StudentNode(const std::string &name)
: next(nullptr), prev(nullptr), name(name) {}
StudentNode * getNext() const { return next; }
StudentNode * getPrev() const { return prev; }
const std::string & getName() const { return name; }
void setNext(StudentNode *student) { next = student; }
void setPrev(StudentNode *student) { prev = student; }
} *head = nullptr, *tail = nullptr;
public:
StudentLinkedList() = default;
StudentLinkedList(const StudentLinkedList &) = delete;
StudentLinkedList & operator =(const StudentLinkedList &) = delete;
~StudentLinkedList()
{
while (head != nullptr)
{
StudentNode *tmp = head;
head = head->getNext();
delete tmp;
}
tail = head;
}
void appendStudent(const std::string &name)
{
StudentNode *student = new StudentNode(name);
appendStudent(student);
}
void insertStudentAfter(const std::string ¤t_name,
const std::string &new_name)
{
StudentNode *student = new StudentNode(new_name);
insertStudentAfter(student, current_name);
}
friend std::ostream & operator <<(std::ostream &os, const StudentLinkedList &lsdt);
private:
void appendStudent(StudentNode *student)
{
if (tail == nullptr)
{
head = tail = student;
}
else
{
tail->setNext(student);
tail = tail->getNext();
}
}
bool insertStudentAfter(StudentNode *student, const std::string &name)
{
StudentNode *current = head;
while (current != nullptr && current->getName() != name)
{
current = current->getNext();
}
bool success = current != nullptr;
if (success)
{
student->setPrev(current);
student->setNext(current->getNext());
if (current->getNext() != nullptr)
{
current->getNext()->setPrev(student);
}
else
{
tail = student;
}
current->setNext(student);
}
return success;
}
};
std::ostream & operator <<(std::ostream &os, const StudentLinkedList &lst)
{
for (StudentLinkedList::StudentNode *current = lst.head;
current != nullptr;
current = current->getNext())
{
os << current->getName() << ' ';
}
return os;
}
int main()
{
const size_t N = ('Z' - 'A' + 1)/2;
StudentLinkedList students;
for (size_t i = 0; i < N; i++)
{
char name[2] = { char('A' + 2 * i) };
students.appendStudent(name);
}
std::cout << students << std::endl;
for (size_t i = 0; i < N; i++)
{
char new_name[2] = { char('A' + 2 * i + 1) };
char current_name[2] = { char('A' + 2 * i) };
students.insertStudentAfter(current_name, new_name);
}
std::cout << students << std::endl;
return 0;
}
程序輸出是
A C E G I K M O Q S U W Y
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
使用正確的工具來解決這些問題是你的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –
@Bloggs爲什麼一個函數使用大寫字母命名,如setNext,另一個命名使用小寫字母作爲getnext? –
'while(curr!= NULL)'將會非常非常困難,而不會在插入的某個地方將其設置爲NULL。 – user4581301