// BASIC LINKED LIST (with node class having a constructor) // Insertions done at the start of the list. // Deletions done at the start of the list. #include "stdstuff.h" class LNode { public: int data; LNode *next; LNode () { data = 0; next = NULL; } LNode (int data, LNode *next) { this -> data = data; this -> next = next; } }; int main () { LNode *head = NULL, *c /*current */; int option, value; for (;;) { cout << "\nEnter option\n" << " 1 = insert\n" << " 2 = delete\n" << " 3 = list contents\n" << " 4 = quit\n" << "Option: "; cin >> option; switch (option) { case 1: cout << "\nEnter a value: "; cin >> value; head = new LNode (value, head); break; case 2: if (head == NULL) { cout << "\nNothing to delete - the list is empty.\n"; } else { c = head; head = head -> next; cout << "\nValue " << c -> data << " deleted from the list.\n"; delete c; } break; case 3: if (head == NULL) { cout << "\nThe list is empty.\n"; } else { cout << "\nThe list contains:"; for (c = head; c != NULL; c = c -> next) { cout << " " << c -> data; } cout << endl; } break; case 4: // no need to clean up the list as the programming is about to terminate pause(); return 0; } } }