参考: 日経ソフトウエア 2019年 9 月号, pp.77-83
何に使うのかわからない。
#include <iostream> #include <string> using namespace std; // 構造体Nodeを定義する。 struct Node { string stationName; // 現在位置 Node* next; // 次の位置(自己参照する) Node* prev; // 前の位置(自己参照する) }; int main() { // 構造体Node型の変数をいくつか宣言する。 Node tokyo; Node shinagawa; Node shinYokohama; Node nagoya; Node* current; // 現在位置 // 各ノードのメンバーを設定する。 tokyo.stationName = "TOKYO"; tokyo.next = &shinagawa; tokyo.prev = NULL; shinagawa.stationName = "SHINAGAWA"; shinagawa.next = &shinYokohama; shinagawa.prev = &tokyo; shinYokohama.stationName = "SHINYOKOHAMA"; shinYokohama.next = &nagoya; shinYokohama.prev = &shinagawa; nagoya.stationName = "NAGOYA"; nagoya.next = NULL; nagoya.prev = &shinYokohama; // 順番にたどる。 current = &tokyo; while (current != NULL) { cout << current->stationName << "\r\n"; current = current->next; } // 逆順にたどる。 current = &nagoya; while (current != NULL) { cout << current->stationName << "\r\n"; current = current->prev; } return 0; }