-1

二重連結リストであるはずのクラスのプログラムを書いています。コンパイルはできますが、コマンド addleft または addright を実行しようとすると、セグメンテーション違反エラーが発生します。私はC ++にかなり慣れていないので、どんな提案も素晴らしいでしょう。コードの関連部分を投稿しました。

リスト ヘッダー ファイル:

//list.h

#include <string>
using namespace std;

class List {
    friend class Node;
public:
    List();
    void addleft(int);
    void addright(int);
    int left();
    int right();
    void print();
    bool search(int);
    //Node *head;
    //Node *tail;
    //Node *n;
};

リストクラスファイル:

#include <iostream>
#include <string>
#include <stdio.h>
#include "List.h"
#include "Node.h"
using namespace std;
Node *current;
Node *tail;
Node *head;
Node *n;

List::List() {

}

void List::addleft(int a) {
    n = new Node;                                                       //The pointer n points to a new Node
    n->number = a;                                                      //Set the value in the node
    n->next = head;                                                     //Point the node to the old head of the linked list
    head->prev = n;                                                     //Link the old head node to the new node
    head = n;                                                           //Set the new node as the new head of the linked list
    head->prev = NULL;                                                  //Make the new node's previous pointer point to nothing (NULL)
    if(current == NULL) {                                               //If the list is empty...
            current = n;                                                //Set the new node as the current node pointer
        }
}

void List::addright(int a) {
    n = new Node;                                                       //The pointer n points to a new Node
    n->number = a;                                                      //Set the value in the node
    n->prev = tail;                                                     //Point the node to the old head of the linked list
    tail->next = n;                                                     //Link the old tail node to the new node
    tail = n;                                                           //Set the new node as the new tail of the linked list
    tail->next = NULL;                                                  //Make the new node's next pointer point to nothing (NULL)
    if(current == NULL) {                                               //If the list is empty...
        current = n;                                                    //Set the new node as the current node pointer
    }
}

int List::left() {
    current = current->prev;
    return current->number;
}

int List::right() {
    current = current->next;
    return current->number;
}

void List::print() {

}

bool List::search(int a) {
    int search;
    //while(search != tail) {

    //}
}

ノード ヘッダー ファイル:

//node.h

using namespace std;

class Node {
    friend class List;
public:
    Node();
private:
    int number;
    Node *next;
    Node *prev;
};

ノード クラス ファイル:

#include <iostream>
#include <string>
#include <stdio.h>
#include "List.h"
#include "Node.h"
using namespace std;

Node::Node() {
    next = NULL;
    prev = NULL;
}
4

1 に答える 1

2

headtailは最初は null ポインターに初期化されていると思います。これが、何かを追加しようとする最初の試みがクラッシュする理由です。その場合は、コードの特別な分岐によって最初の追加を処理する必要があります。headその特別なブランチは、との両方tailが nullの場合に合わせて特別に作成する必要があります。

編集:コードをさらに確認した後、これが実際にあなたのコードに当てはまると結論付けることができます。

ただし、クラスを作成してから andをファイルスコープ変数として (クラスのメンバーにするのではなく)List宣言しても意味がありません。最初にそれらをクラス メンバーとして宣言したようですが、後でこれらのメンバーをコメントアウトしたようです。どうして???headtailList

于 2013-11-05T01:28:04.803 に答える