他のポインタが残っている間に、リスト(HeadFirstCl、HeadNoSm、HeadSm)を接続して大きなリスト(HeadByPlace)を作成するリストがいくつかあります。私の質問は、ConnectListsの機能が機能しないのはなぜですか?3つのリストを1つのビッグリストにマージする
#include <iostream>
#include <string>
using namespace std;
struct Item {
string naprav;
string chasizl;
string chaskac;
int termizl;
int termkac;
char fime[5];
int mqsto;
Item *NextByPlace;
};
typedef Item *Point;
Point HeadByPlace, HeadFirstCl, HeadNoSm, HeadSm;
void ConnectLists(Point &P, Point A) {
while (A) {
if (P->NextByPlace == NULL)
P->NextByPlace = A;
P = P->NextByPlace;
}
}
void PrintOut(Point P) {
while (P) {
cout << P->fime<<endl;
cout << P->chasizl << endl;
cout << P->chaskac << endl;
cout << P->mqsto << endl;
cout << P->naprav << endl;
cout << P->termizl << endl;
cout << P->termkac << endl;
P = P->NextByPlace;
}
}
void Create(Point &Head, int i) {
Point Last, P;
Last = NULL;
P = new Item;
P->mqsto = i;
cout << "Enter destination" << endl;
cin >> P->naprav;
cout << "Enter departure HOUR" << endl;
cin >> P->chasizl;
cout << "Enter arrival HOUR" << endl;
cin >> P->chaskac;
cout << "Enter # of leaving terminal" << endl;
cin >> P->termizl;
cout << "Enter # of entering terminal" << endl;
cin >> P->termkac;
cout << "Last name of traveler" << endl;
cin >> P->fime;
P->NextByPlace = NULL;
if (Head == NULL) {
Head = P;
} else {
Last->NextByPlace = P;
}
Last = P;
}
void Delete(char name[], Point &Head) {
Point Pprev, P;
P = new Item;
Pprev = new Item;
cin >> name;
while (Head) {
if (strcmp(Head->fime, name) == 1) {
Pprev = P->NextByPlace;
*P = *Pprev;
delete Pprev;
}
}
}
void main() {
char ch;
HeadByPlace = NULL;
HeadFirstCl = NULL;
HeadNoSm = NULL;
HeadSm = NULL;
int i;
cout << "New element? (Y/N)? : ";
cin >> ch;
while (ch == 'Y' || ch == 'y') {
cout << "Enter seat #: ";
cin >> i;
if (i < 7) Create(HeadFirstCl,i);
else if (i > 7 && i < 25) Create(HeadNoSm,i);
else if (i > 25) Create(HeadSm,i);
cout << " New element? (Y/N) ?: ";
cin >> ch;
}
ConnectLists(HeadByPlace, HeadFirstCl);
ConnectLists(HeadByPlace, HeadNoSm);
ConnectLists(HeadByPlace, HeadSm);
PrintOut(HeadByPlace);
system("pause");
}
Connectリストを呼び出す前後にPの値を調べます。私の推測は、それは同じになるだろうということです。 cでは、ポインターを関数に渡すことでポインタを変更することができますが、ポインター自体の変更は関数の外では持続しません(P:P = P-> NextByPlaceへの変更は失われます)。私はあなたがそれが欲しいと思うようにこれが動作するためには、あなたはポインタポインタを使用する必要があります。 (すなわち、Point ** P)、それをあなたの関数で逆参照してください。 –