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

class Node {

public:

	int cargo;
	Node* next;

	Node () {
		cargo = 0;
		next = NULL;
	}

	Node (int Cargo, Node* Next) {
		cargo = Cargo;
		next = Next;
	}

}; // Node class
/*
void printList (Node* list) {
	Node* node = list;

	while (node != NULL) {
		cout << node->cargo;
		node = node->next;
	}
	cout << endl;
}

*/
void printList(Node* list) {
    for (Node* node = list; node != NULL; node = node->next) {
        cout << node->cargo;
    }
    cout << endl;
}


void prettyPrint (Node* list) {
    cout << '(';
    // print out elements
    Node* node = list;
    while (node != NULL) {
        cout << node->cargo;
        if (node->next != NULL) cout << ", ";
        node = node->next;
    }
    cout << ')';
}


void printBackward (Node* list) {
    if (list == NULL) return;

    Node* head = list;
    Node* tail = list->next;

    printBackward (tail);
    cout << head->cargo;
}

void printBackwardNicely (Node* list) {
    cout << '(';
    if (list != NULL) {
        printBackward (list);
    }
    cout << ')';
}


Node* removeSecond (Node* list) {
	Node* first = list;
	Node* second = list->next;

	// make the first node refer to the third
	first->next = second->next;

	// separate the second node from the rest of the list
	second->next = NULL;
	return second;
}

class LinkedList {

public:
    int length;
    Node* head;

    LinkedList () {
        length = 0;
        head = NULL;
    }

    void printBackwardNicely () {
        cout << '(';

        if (head != NULL) {
            Node* tail = head->next;
            printBackward (tail);
            cout << head->cargo;
        }
        cout << ')';
    }

	void addFirst (int i) {
		Node* node = new Node (i, head);
		head = node;
		length++;
    }

};


int main () {
    Node node = Node (1, NULL);
    cout << node.cargo << endl;

	Node* pnode = new Node (2, NULL);
    cout << (*pnode).cargo << endl;
    cout << pnode->cargo << endl;

	Node* pnode1 = new Node (1, NULL);
    Node* pnode2 = new Node (2, NULL);
    Node* pnode3 = new Node (3, NULL);

	cout << pnode1->cargo << pnode2->cargo << pnode3->cargo << endl;

    // (*pnode1).next = pnode2;
	pnode1->next = pnode2;
    pnode2->next = pnode3;
    pnode3->next = NULL;
//    pnode3->next = pnode1;

	printList (pnode1); cout << endl;

    prettyPrint(pnode1); cout << endl;

	printBackward (pnode1); cout << endl;

    printBackwardNicely (pnode1); cout << endl;

	prettyPrint (pnode1); cout << endl;
	Node* removed = removeSecond (pnode1);
	prettyPrint (removed); cout << endl;
	prettyPrint (pnode1);  cout << endl;
/*

	LinkedList L;
	L.addFirst(7);
	L.addFirst(8);
	L.addFirst(9);
	L.printBackwardNicely(); cout << endl;

    LinkedList M;
    int x;
    cin >> x;
    while (x >= 0) {
        M.addFirst(x);
        cin >> x;
    }
    M.printBackwardNicely(); cout << endl;

	cout << endl;
*/
	return 0;
}

