C++ template linked list
#include <iostream> using namespace std; const int LEN = 80; //maximum length of names class Employee//from w w w . ja va 2s . co m { private: char name[LEN]; unsigned long number; public: friend istream& operator >> (istream& s, Employee& e); friend ostream& operator << (ostream& s, Employee& e); }; istream& operator >> (istream& s, Employee& e) { cout << "\n Enter last name: "; cin >> e.name; cout << " Enter number: "; cin >> e.number; return s; } ostream& operator << (ostream& s, Employee& e) { cout << "\n Name: " << e.name; cout << "\n Number: " << e.number; return s; } template<class TYPE> //struct "link<TYPE>" struct link //one element of list { TYPE data; //data item link* next; //pointer to next link }; template<class TYPE> //class "linklist<TYPE>" class linklist { private: link<TYPE>* first; //pointer to first link public: linklist() //no-argument constructor { first = NULL; } //no first link void additem(TYPE d); //add data item (one link) void display(); }; template<class TYPE> void linklist<TYPE>::additem(TYPE d) //add data item { link<TYPE>* newlink = new link<TYPE>; //make a new link newlink->data = d; //give it data newlink->next = first; //it points to next link first = newlink; //now first points to this } template<class TYPE> void linklist<TYPE>::display() //display all links { link<TYPE>* current = first; //set ptr to first link while( current != NULL ) //quit on last link { cout << endl << current->data; //display data current = current->next; //move to next link } } int main() { //lemp is object of linklist<Employee> lemp; //class "linklist<Employee>" Employee emptemp; //temporary Employee storage char ans; //user's response ('y' or 'n') do { cin >> emptemp; //get Employee data from user lemp.additem(emptemp); //add it to linked list lemp cout << "\nAdd another (y/n)? "; cin >> ans; } while(ans != 'n'); //when user is done, lemp.display(); //display entire linked list cout << endl; return 0; }