0

初めてcurrent->nextの値をnullに設定したときに、リンクリストに挿入します。これを使用して、印刷中にリストの最後にいるかどうかを確認します。デバッガーでチェックすると、null値が表示されますが、ifステートメント[if(current-> next == NULL)]は無限の出力を妨げません。私のコードは以下の通りです。

#include "List.h"
#include <cstdlib>

List::List(){
    //precondition:none
    //postcondition:empty list;position @ 1
    head = NULL;
    current = NULL;
    previous = NULL;
position = 1;
}
List::~List(){
    //precondition:link exists
    //List is now an empty linked list
makeEmpty();
}
void List::goToNext(){
    if(!isAtEnd()){
        if(current->next == NULL){
            previous == current;
            current == NULL;
        }
        else{
            previous = current;
            current = current->next;
            position +=1;
        }
    }
}
void List::goToStart(){
    if(position = 1){
        return;
    }
    else{
        current = head;
        previous = NULL;
        position = 1;
    }
}
void List::makeEmpty(){
    if(!isEmpty()){
        this->goToStart();
        while(isAtEnd() == false)
        {
            goToNext();
            delete previous;
            previous = current;
        }
        head = NULL;
        previous = NULL;
        current = NULL;
        position = 1;
    }
}
bool List::isEmpty(){
    if(this->head == NULL && this->current == NULL && this->previous == NULL){
        return true;
    }
}
bool List::isAtEnd(){
    if(current == NULL){
        return true;
    }
    else{
        return false;
    }
}
ItemType List::CurrentItem(){
    if(isAtEnd() != true){
        return current->data;
    }
}
void List::insert(ItemType item){
    nodeType * temp = new nodeType;
    temp->data = item;

    if(head == NULL){
        temp->next = NULL;
        head = temp;
        current = head;
        current->next = NULL;
    }
    else if(position == 1){
        head = temp;
        head->next = current;
        current = head;
    }
    else if(!isAtEnd() && current->next == NULL){
        temp->next = current;
        current = temp;
        if(previous != NULL){
            previous->next = current;
        }
    }
    else{
        current = temp;
        current->next = NULL;
    }
}
void List::deleteCurrentItem(){
    previous->next = current->next;
    delete current;
    current = previous->next;
    position -= 1;
}
int List::currentPosition(){
    return position;
}

////////---print function from main---///////
void print(List & testList){
    int storedPosition = testList.currentPosition();

    testList.goToStart();
    while (!testList.isAtEnd())
    {
        cout << testList.CurrentItem() << endl;
        testList.goToNext();
    }

    testList.goToStart();
    for ( int i = 1; i < storedPosition; i++)
        testList.goToNext();        
}
4

1 に答える 1

3

では、コンパレータを使用するのではなく、List::goToNext()設定時に代入を使用する必要があります。previouscurrent

    previous = current;
    current = NULL;
于 2013-03-14T22:36:44.840 に答える