一个关于c++双向链表的问题
#include <iostream>
#include <string>
using namespace std;
struct Station
{
string stationName;
int minutes;
Station *prev;
Station *next;
};
Station *head = nullptr;
Station *generate_metro_line(int line_number, int station_number)
{
Station *newstation = new Station;
Station *head = newstation;
head->stationName = "line" + to_string(line_number) + " station " + to_string(1);
head->minutes = rand() % 5 + 1;
Station *prev_station = head;
int i = 1;
while (i < station_number)
{
Station *current_station = new Station;
current_station->stationName = "line" + to_string(line_number) + " station " + to_string(i + 1);
current_station->minutes = rand() % 5 + 1;
prev_station->next = current_station;
current_station->prev = prev_station;
tail = current_station;
prev_station = current_station;
i++;
}
return prev_station;
}
void output(Station *station)
{
if (station == nullptr)
{
cout << station->stationName << ": end station" << endl;
}
else //错误出现的位置
{
cout << station->stationName << ": " << station->minutes << " minute(s) to next" << endl;
}
}
void output_line(Station *firststation)
{
while (firststation != nullptr)
{
output(firststation);
firststation = firststation->next;
}
}
struct Train
{
string trainName;
int leftTime;
bool direction;
Station *nextStation;
};
void init(Train &train, string trainName, Station *stopstation)
{
train.trainName = trainName;
train.leftTime = 0;
train.direction = true;
train.nextStation = stopstation;
}
void output(Train &train)
{
if (train.direction)
{
cout << train.trainName << ": stop at " << train.nextStation->stationName << " for " << train.leftTime << " minutes" << endl;
}
else
{
cout << train.trainName << " is going to " << train.nextStation->stationName << " in " << train.leftTime << " minutes" << endl;
}
}
void output_all_trains(int simulation_time, Train t[], int train_number)
{
cout << "simulation time: " << simulation_time << " min" << endl;
for (int i = 0; i < train_number; i++)
{
if (t[i].leftTime <= simulation_time)
{
output(t[i]);
}
}
}
int main()
{
Train t[3];
Station *station[2];
int simulation_time = 0;
int n1, n2;
cout << "input number of stations of line 1:";
cin >> n1;
station[1] = generate_metro_line(1, n1);
cout << "input number of stations of line 2:";
cin >> n2;
station[2] = generate_metro_line(2, n2);
init(t[1], "train1", station[1]);
init(t[2], "train2", station[2]);
char choice;
do
{
cout << "e end" << endl
<< "l line output" << endl
<< "t train output" << endl
<< "1 simulate 1 minute" << endl;
cin >> choice;
switch (choice)
{
case 'e':
break;
case 'l':
int line_number;
cout << "which line?" << endl;
cin >> line_number;
output_line(station[line_number]);
break;
case 't':
output_all_trains(simulation_time, t, 3);
break;
case '1':
simulation_time++;
break;
default:
cout << "wrong input" << endl;
break;
}
} while (choice != 'e');
return 0;
}
请教各位朋友,这段代码中总是出现下图的错误,自己没有搞明白是为什么会出现这样的错误(个人感觉是没正确设置链表的头节点),并且能否讲述下解决的思路!(main函数中请主要查看case'l' ,自动忽略别的case错误,谢谢!)
Station *head = nullptr;
Station *generate_metro_line(int line_number, int station_number)
{
Station *newstation = new Station;
Station *head = newstation;
head->stationName = "line" + to_string(line_number) + " station " + to_string(1);
head->minutes = rand() % 5 + 1;
Station *prev_station = head;
int i = 1;
while (i < station_number)
{
Station *current_station = new Station;
current_station->stationName = "line" + to_string(line_number) + " station " + to_string(i + 1);
current_station->minutes = rand() % 5 + 1;
// 这里应该加一句
current_station->next = prev_station->next; // 不然没有连起来
prev_station->next = current_station;
current_station->prev = prev_station;
tail = current_station;
prev_station = current_station;
i++;
}
return prev_station;
}
另外 ouput 里 == nullptr 的分支应该返回 不应该访问空指针了