//
// Created by Fredrick Park on 5/1/17.
//
// Beacons of Gondor
// This program describes the concept of a linked list by mimicking the
// Beacons of Gondor story from J.R.R. Tolkien's Return of the King

#include <iostream>
#include <string>
using namespace std;

// Type: Tower
// This structure contains the name of the tower and the link to the next tower
struct Tower {
    string name; // the name of this tower
    Tower *link; // pointer to next tower in the chain
};

// function prototypes
Tower *createBeaconsOfGondor();
Tower *createTower(string name, Tower *link);
void signal(Tower *start);

// main program
int main() {
    Tower *list = createBeaconsOfGondor();
    signal(list);
    return 0;
}


// function: createBeaconsOfGondor
// usage: Tower *list = createBeaconsOfGondor();
// creates a linked list of the towers described by Tolkien.
// the function builds the list backwards and returns a pointer to the first tower
Tower *createBeaconsOfGondor() {
    Tower *tp = createTower("Rohan", NULL);
    tp = createTower("Halifirien", tp);
    tp = createTower("Calenhad", tp);
    tp = createTower("Min-Rimmon", tp);
    tp = createTower("SLC 416", tp);
    tp = createTower("Erelas", tp);
    tp = createTower("Nardol", tp);
    tp = createTower("Eilenach", tp);
    tp = createTower("Amon Din", tp);
    return createTower("Minas Tirith", tp);
}

// function: createTower
// Usage: Tower *chain = createTower(name, link);
// creates a new Tower structure with the specified components
Tower *createTower(string name, Tower *link) {
    Tower *tp = new Tower;
    tp->name = name; // same as (*tp).name = name;
    tp->link = link;
    return tp;
}

// function: signal
// useage: signal(start);
// generates a signal starting at the current tower and proceeding
// through the end of the chain
void signal(Tower *start) {
    for (Tower *tp = start; tp != NULL; tp = tp->link) {
        cout << "Lighting *** " << tp->name << " ***" << endl;
        //cout << "Lighting *** " << (*tp).name << " ***" << endl;

    }
}


// recursive implementation of signal function
//void signal(Tower *start) {
//    if (start  != NULL) {
//        cout << "Lighting *** " << start->name << " ***" << endl;
//        signal(start->link);
//    }
//}