using namespace std; #include #include "tut.h" cell::cell(int h, cell* t) // constructor { head = h; tail = t; } cell::~cell() { } void list::insert(int x) // inserts new cell to front of list { front = new cell(x,front); /* equivalently: cell* newcell; newcell = new cell(x,0); // tail intially set to null newcell->tail = front; front = newcell; */ } // end insert void list::print() // prints data in list { cell* current; current = front; while (current != 0) // note "0" instead of "null" { cout << current->head << "\n"; current = current->tail; } } list::list() { front = 0; } // constructor list::~list() // destructor : must erase all cells too! { cell* temp; cell* current; current = front; cout << "Garbage collecting the list cells...\n"; while (current != 0) { temp = current; current = current->tail; delete(temp); // memory deallocation } } int main() { int A[5]; // array of 5 integers list* L; // a list L = new list(); int i; // loop counter for (i=0;i<5;i++) { cout <<"Enter number: "; cin >> A[i]; // read input into A[i]; } cout << "\nInserting numbers into a list."; for (i=0;i<5;i++) L->insert(A[i]); cout << "\nAnd these are the contents of the list:\n"; L->print(); delete(L); // memory deallocation return 0; } // end of main