前提・実現したいこと
c++で双方向リストを作成したいです
発生している問題・エラーメッセージ
末尾にうまく値を挿入できない
該当のソースコード
C++
1#include <cstdlib> 2 class BidirectionalCell{ 3 public: 4 int value; 5 6BidirectionalCell* forward; 7 BidirectionalCell* backward; 8 BidirectionalCell(int val= 0): value(val), 9 forward(NULL), backward(NULL) {} 10 }; 11 12 13 #include "BidirectionalCell.h" 14 #include <cstdlib> 15 #include <string> 16 class BidirectionalList{ 17 private: 18 BidirectionalCell* top; 19 BidirectionalCell* tail; 20 public: 21 BidirectionalList(): 22 top(NULL), tail(NULL) {} 23 void pushTop(int value); 24 void pushTail(int value); 25 std::string toString() const; 26 }; 27 28 #include "BidirectionalList.h" 29 #include <iostream> 30 using namespace std; 31 int main() { 32 BidirectionalList* list = new BidirectionalList(); 33 list->pushTop(1); 34 list->pushTail(2); 35 list->pushTop(3); 36 list->pushTail(9); 37 list->pushTail(10); 38 cout<< "list = " << list->toString() << endl; 39 } 40
試したこと
pushtopというメンバ関数内に
BidirectionalCell* tmp = new BidirectionalCell(value);
tmp->forward = top;
top = tmp;
というコードを書いたら先頭に値が入るようになりました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/22 05:43