あなたの機能はあまりにも複雑です。より複雑な関数になるとバグが多くなり、読みにくくなります:)
この関数は次のように記述できます。以下の関数が定義されていると仮定します。
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
を受け入れ、upvote感謝します。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –
@Bloggs 1つの関数にsetNextのような大文字を使用して名前を付ける理由と、他の関数をgetnextとして小文字を使用して名前を付けるのはなぜですか? –
'while(curr!= NULL)'は、非常に難しく、何かが挿入のどこかで 'NULL'に設定されていないでしょう。 – user4581301