二重連結リストであるはずのクラスのプログラムを書いています。コンパイルはできますが、コマンド 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;
}