COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
train.cpp File Reference
#include <iostream>
#include <cstdlib>
#include <string>
Include dependency graph for train.cpp:

Go to the source code of this file.

Classes

struct  Car
 

Functions

void appendCar (Car *&head, string id)
 
int main ()
 
void printTrain (Car *&head)
 

Function Documentation

◆ appendCar()

void appendCar ( Car *& head,
string id )

Definition at line 30 of file train.cpp.

30 {
31 if (head == NULL) {
32 head = new Car;
33 head->id = id;
34 head->next = NULL;
35 } else {
36 Car *current = head;
37 while (current->next != NULL) {
38 current = current->next;
39 }
40 current->next = new Car;
41 current->next->id = id;
42 current->next->next = NULL;
43 }
44}
Definition train.cpp:7
Car * next
Definition train.cpp:9
string id
Definition train.cpp:8

References Car::id, and Car::next.

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 15 of file train.cpp.

15 {
16 Car *head = NULL;
17 int n;
18 cin >> n;
19 string id;
20 for (int i = 0; i < n; i++) {
21 cin >> id;
22 appendCar(head, id);
23 }
24 printTrain(head);
25
26 return 0;
27}
void appendCar(Car *&head, string id)
Definition train.cpp:30
void printTrain(Car *&head)
Definition train.cpp:46

References appendCar(), and printTrain().

Here is the call graph for this function:

◆ printTrain()

void printTrain ( Car *& head)

Definition at line 46 of file train.cpp.

46 {
47 Car *current = head; (*current).next
48 while (current != NULL) {
49 if (current->next != NULL) {
50 cout << current->id << ", ";
51 }
52 else {
53 cout << current->id;
54 }
55 current = current->next;
56 }
57}

References Car::id, and Car::next.

Referenced by main().

Here is the caller graph for this function: