aizu online judgeにて双方向連結リストの他の方の回答をみて疑問があります。
#include<stdio.h> #include <iostream> #include <string.h> #include <cstdlib> using namespace std; struct Node{ int data; Node* prev; Node* next; }; class ConnectedList{ public: ConnectedList(){ motherNode = new Node(); motherNode->data = -1; motherNode->prev = motherNode; motherNode->next = motherNode; }; ~ConnectedList(){ Node* tmpNode = motherNode->next; Node* nextNode; while(tmpNode != motherNode){ nextNode = tmpNode->next; delete tmpNode; tmpNode = nextNode; } delete motherNode; }; void insertNode(int x){ Node* tmpNode = motherNode->next; motherNode->next = new Node(); motherNode->next->data = x; motherNode->next->next = tmpNode; motherNode->next->prev = motherNode; tmpNode->prev = motherNode->next; }; void deleteNode(int x){ Node* tmpNode = motherNode->next; while(tmpNode != motherNode && tmpNode->data != x){ tmpNode = tmpNode->next; } if(tmpNode != motherNode){ tmpNode->prev->next = tmpNode->next; tmpNode->next->prev = tmpNode->prev; delete tmpNode; } }; void deleteFirst(){ Node* tmpNode = motherNode->next; motherNode->next = motherNode->next->next; motherNode->next->prev = motherNode; delete tmpNode; }; void deleteLast(){ Node* tmpNode = motherNode->prev; motherNode->prev = motherNode->prev->prev; motherNode->prev->next = motherNode; delete tmpNode; }; void showList(){ Node* tmpNode = motherNode->next; printf("%d",tmpNode->data); tmpNode = tmpNode->next; while(tmpNode != motherNode){ printf(" %d",tmpNode->data); tmpNode = tmpNode->next; } printf("\n"); } private: Node* motherNode; };
deleteLastをする時、motherNode->prev = motherNode->prev->prevで削除してますが、insertNodeした時のどこの箇所でmotherNode->prevにアドレスが入ったのでしょうか?
よろしくお願い致します。
> 初回はなぜtmpNode->prevとmotherNode->prevのアドレスが同じなのでしょうか?
そういう記述はないようですが
回答2件
あなたの回答
tips
プレビュー