Освой самостоятельно С++ за 21 день.
Шрифт:
66: cout << itsEngineNumber << endl;
67: }
68: private:
69: int itsEngineNumber;
70: };
71:
72: AirPlanePart::AirPlanePart
73: (int EngineNumber, int PartNumber):
74: itsEngineNumber(EngineNumber),
75: Part(PartNumber)
76: { }
77:
78: // **************** Класс Part Node ************
79: class PartNode
80: {
81: public:
82: friend class PartsList;
83: PartNode (Part*);
84: ~PartNode;
85: void SetNext(PartNode * node)
86: { itsNext = node; }
87: PartNode * GetNext const;
88: Part * GetPart const;
89: private:
90: Part *itsPart;
91: PartNode * itsNext;
92: };
93:
94:
95: PartNode::PartNode(Part* pPart):
96: itsPart(pPart),
97: itsNext(0)
98: { }
99:
100: PartNode::~PartNode
101: {
102: delete itsPart;
103: itsPart = 0;
104: delete itsNext;
105: itsNext = 0;
106: }
107:
108: //
109: PartNode * PartNode::GetNext const
110: {
111: return itsNext;
112: }
113:
114: Part * PartNode::GetPart const
115: {
116: if (itsPart)
117: return itsPart;
118: else
119: return NULL; //ошибка
120: }
121:
122:
123: // ************** Класс Part List
124: class PartsList
125: {
126: public:
127: PartsList;
128: ~PartsList;
129: // Необходимо, чтобы конструктор-копировщик и оператор соответствовали друг другу
130: void Iterate(void (Part::*f)const) const;
131: Part* Find(int & position, int PartNumber) const;
132: Part* GetFirst const;
133: void Insert(Part *);
134: Part* operator[](int) const;
135: int GetCount const { return itsCount; }
136: static PartsList& GetGlobalPartsList
137: {
138: return GiobalPartsList;
139: }
140: private:
141: PartNode * pHead;
142: int itsCount;
143: static PartsList GiobalPartsList;
144: };
145:
146: PartsList PartsList::GlobalPartsList;
147:
148: // Implementations for Lists...
149:
150: PartsList::PartsList;
151: pHead(0),
152: itsCount(0)
153: { }
154:
155: PartsList::~PartsList
156: {
157: delete pHead;
158: }
159:
160: Part* PartsList::GetFirst const
161: {
162: if (pHead)
163: return pHead->itsPart;
164: else
165: return NULL; // ловушка ошибок
166: }
167:
168: Part * PartsList::operator[](int offSet) const
169: {
170: PartNode* pNode = pHead;
171:
172: if (!pHead)
173: return NULL; // ловушка ошибок
174:
175: if (offSet > itsCount)
176: return NULL; // ошибка
177:
178: for (int i=0;i<offSet; i++)
179: pNode = pNode->itsNext;
180:
181: return pNode->itsPart;
182: }
183:
184: Part* PartsList::Find(int & position, int PartNumber) const
185: {
186: PartNode * pNode = 0;
187: for (pNode = pHead, position = 0;
188: pNode!=NULL;
189: pNode = pNode->itsNext, position++)
190: {
191: if (pNode->itsPart->GetPartNumber == PartNumber)
192: break;
193: }
194: if (pNode == NULL)
195: return NULL;
196: else
197: return pNode->itsPart;
198: }
199:
200: void PartsList::Iterate(void (Part::*func)const) const
201: {
202: if (!pHead)
203: return;
204: PartNode* pNode = pHead;
205: do
206: (pNode->itsPart->*func);
207: while (pNode = pNode->itsNext);
208: }
209:
210: void PartsList::Insert(Part* pPart)
211: {
212: PartNode * pNode = new PartNode(pPart);
213: PartNode * pCurrent = pHead;
214: PartNode * pNext = 0;
215:
216: int New = pPart->GetPartNumber;
217: int Next = 0;
218: itsCount++;
219:
220: if (!pHead)
221: {
222: pHead = pNode;
223: return;
224: }
225:
226: // если это значение меньше головного узла,
227: // то текущий узел становится головным
228: if (pHead->itsPart->GetPartNumber > New)
229: {
230: pNode->itsNext = pHead;
231: pHead = pNode;
232: return;
233: }