割り当ては、さまざまなことをする整数と関数のリストを作ることです。私は1つを除いてすべてを終えた。テンプレート内でリンクリストをコピーする関数の作成に問題がある
特定の値とノードのリストを検索する関数を作成する方法を理解した後、そのリストを複製する関数を作成する必要があります。しかし、私は今、私が持っている素材でこれを行うことに深刻な問題を抱えています。
//==appendNode definition==
template<class T>
void IntegerList<T>::appendNode(T newValue) {
ListNode *newNode;
ListNode *nodePtr;
newNode = new ListNode;
newNode->value = newValue;
newNode->next = NULL;
if (!head) head = newNode;
else {
nodePtr = head;
while (nodePtr->next)nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
//==displayList Definition==
template<class T>
void IntegerList<T>::displayList() const {
ListNode *nodePtr;
nodePtr = head;
while (nodePtr) {
cout << nodePtr->value << "";
nodePtr = nodePtr->next;
}
cout << endl;
}
//==insertNode Definiton==
template<class T>
void IntegerList<T>::insertNode(T newValue) {
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode = NULL;
newNode = new ListNode;
newNode->value = newValue;
if (!head) {
head = newNode;
newNode->next = NULL;
}
else {
nodePtr = head;
previousNode = NULL;
while (nodePtr != NULL && nodePtr->value < newValue) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL) {
head = newNode;
newNode->next = nodePtr;
}
else {
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//==deleteNode Definition==
template<class T>
void IntegerList<T>::deleteNode(T searchValue) {
ListNode *nodePtr;
ListNode *previousNode = NULL;
if (!head) return;
if (head->value == searchValue) {
nodePtr = head->next;
delete head;
head = nodePtr;
}
else {
nodePtr = head;
while (nodePtr != NULL && nodePtr->value != searchValue) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr) {
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
//searchNode Definiton
template <class T>
void IntegerList<T>::searchNode(T searchValue)
{
ListNode *nodePtr=0;
nodePtr = head;
int i = searchValue;
//This variable is initiated to remember the number to search for. For use in if statement.
int j = 0;
//This variable is dedicated to the position number, starting with 0. Increments by 1 when the while loop loops.
while (nodePtr)
{
if (i == nodePtr->value) {
//This if statemtent will return a success message with the position number if the number is found.
cout << "\nThe value "<< nodePtr->value <<" was found in the list, in position " << j <<" of this list.\n";
return;
}
else
{
nodePtr = nodePtr->next;
j++;
}
}
//This message only plays when it goes through the list without finding the value.
cout << "\nThe value " << i << " was not found in this list.\n";
}
//==Duplicatenode Definition==
template<class T>
void IntegerList<T>::Duplicatenode(T)
{
if (list == NULL) return NULL;
ListNode* result = new ListNode;
result->value = list->value;
result->next = Clone(list->next);
return result;
}
//==Destructor Definition==
template<class T>
IntegerList<T>::~IntegerList() {
ListNode *nodePtr;
ListNode *nextNode;
nodePtr = head;
while (nodePtr != NULL) {
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
と試験問題が発生した主な機能:
これは、これまで私が働いているクラス(Tがテンプレートである)
template <class T>
class IntegerList{
private:
struct ListNode {
T value;
struct ListNode *next;
};
ListNode *head;
public:
//This is the constructor.
IntegerList()
{head = NULL;}
//Destructor
~IntegerList();
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void searchNode(T);
void Duplicatenode(T);
void displayList() const;
};
機能です。
int main() {
IntegerList<int> list1;
list1.appendNode(1);
list1.appendNode(2);
list1.appendNode(5);
list1.displayList();
list1.insertNode(4);
list1.displayList();
list1.deleteNode(2);
list1.displayList();
cout << "\nThis line breaks to denote searchNode function running.\n";
list1.searchNode(5);
list1.searchNode(3);
cout << "\nLine break to denote copyNode function running.\n";
IntegerList<int> list2(list1);
list2.displayList();
cin.ignore();
cin.get();
return 0;
}
検索の結果、有用で有用な回答が得られませんでした。 テンプレートを保持している間に私がそうする方法はありますか?
リストを複製するには、既存のリストのノードを反復処理し、既存の 'insertNode()'を使用して各値を新しいリストに挿入します。それでおしまい。既存の 'insertNode()'はあまり効率的ではありませんが、導入の実装としてはそうします。 –
これは比較的速いのですが、Duplicatenode関数のどのリストがどのリストになるのかを示すためにはどのようなコマンドを使用しますか?たとえば、重複したリストをlist1(元のリスト)からlist2に書き込む必要があります。プログラムはどのリンクがどのようになるかをどのように知っていますか? –
あなたがそれを行うためのコードを書くので、プログラムは知っているでしょう。 –