分段故障双链表C ++

时间:2017-03-22 01:07:53

标签: c++ segmentation-fault nodes doubly-linked-list

这是我试图实现的类文件,函数非常自我解释。我无法处理此分段错误,而是使用*nodeArray = new Node[size];

现在已经看了一段时间,似乎无法找到问题。

class zDepthList{
    private:
        Node ** nodeArray;
        Node * head;
        Node * tail;
        int max;

    public:
        zDepthList(int[], int);
        void out(const char);
        void move(int, int, char);
        void movefront(int);
        void moveback(int);
        ~zDepthList();
};


zDepthList::zDepthList(int arr[], int size){
    *nodeArray = new Node[size];
    head = NULL;
    tail = NULL;
    struct Node * temp;
    max = size;

    int x = 0;
    while(x < max){
        temp = new Node;
        temp->data = arr[x];
        nodeArray[x] = temp;

        if(head == NULL){
            head = temp;
            tail = temp;
            temp->prev = NULL;
            temp->next = NULL;
        }
        else{
            tail = temp;
            temp->next = NULL;
            temp->prev = nodeArray[x-1];
            nodeArray[x-1]->next = temp;
        }
        x++;
    }
}//constructor ends

void zDepthList::out(const char c = 'f'){
    Node * curr = new Node;
    if(c == 'f'){
        curr = head;
        while(curr != NULL){
            cout << curr->data << " ";
            curr = curr->next;
        }
        cout << endl;
    }
    else if(c == 'r'){
            curr = tail;
            while(curr != NULL){
                cout << curr->data << " ";
                curr =  curr->prev;
            }
            cout << endl;
         }
         else{
                cout << "ERROR INVALID DIRECTION" << endl;
                return;
             }
}

void zDepthList::move(int index, int steps, char direc = 'f'){
    Node * temp = new Node;
    Node * before = new Node;
    Node * current = new Node;

    if(index > max-1){
        cout << "ERROR INDEX OUT OF BOUNDS" << endl;
        return;
    }

    if(direc == 'f'){
        temp = nodeArray[index];
        before = nodeArray[index];
        current = nodeArray[index]->next;

        int x = 0, y = index;
        while(x < steps && y < max){
            x++;
            y++;
            before = current;
            current = current->next;
        }

        //If the node is the first in the list
        if(temp->prev == NULL){
            temp->next->prev = NULL;
            head = temp->next;
            temp->prev = before;
            temp->next = before->next;
            before->next = temp;

            if(current == NULL){ 
                tail = temp; 
            }            
            else{ 
                    current->prev = temp; 
                }
        }

        else{
            temp->prev->next = temp->next;
            temp->next->prev = temp->prev;
            temp->prev = before;
            temp->next = before->next;
            before->next = temp;

            if(current == NULL) { 
                  tail = temp; 
            }
            else{ 
                    current->prev = temp; 
                }
        }
    }

    else if(direc == 'r'){
             temp = nodeArray[index];
             before = nodeArray[index]->prev;
             current = nodeArray[index];
             int x = index, y = 0;

        //Can this be handled without the while loop????
       while(x >= 0 && y < steps){
           y++;
           x--;
           current = before;
           before = before->prev;
       }

       //this is if the node is at the end of the list
       if(temp->next == NULL){
           temp->prev->next = temp->next;
           temp->next = current;
           tail = temp->prev;
           temp->prev = before;
           current->prev = before;

           if(before == NULL) { 
               head = temp; 
           }       
           else{ 
                   before->next = temp; 
               }
        }
        else{
            temp->next->prev = temp->prev;
            temp->prev->next = temp->next;
            temp->prev = current->prev;
            current->prev = temp;
            temp->next = current;
            if(before == NULL){ 
                head = temp; 
            }
            else{
                    before->next = temp;
                }
        }
    }

}

void zDepthList::movefront(int index){
    Node * temp = new Node;
    Node * current = new Node;

    temp = nodeArray[index];
    current = nodeArray[index];

    if(current->next == NULL) {
        return;
    }

    current->prev->next = current->next;
    current->next->prev = current->prev;

    temp->prev = head->prev;
    temp->next = head;
    head = temp;

    nodeArray[index] = temp;
}

void zDepthList::moveback(int index){
    Node * temp = new Node;
    Node * current = new Node;

    temp = nodeArray[index];
    current = nodeArray[index];

    if(current->next == NULL) {
        return;
    }

    current->prev->next = current->next;
    current->next->prev = current->prev;

    temp->prev = tail->prev;
    temp->next = NULL;
    tail->next = temp;
    tail = temp;

    nodeArray[index] = temp;
}


zDepthList::~zDepthList(){
    delete head;
    delete tail;
    if(nodeArray != NULL) 
        delete nodeArray;
}

0 个答案:

没有答案