#include using namespace std; class Node { friend ostream &operator<< (ostream&, const Node*); public: int data; Node* next; }; ostream &operator<< (ostream& output, const Node* cur) { output << " [ " << cur->data << " ] -> "; if (cur->next == NULL) output << "||"; return output; } class List { friend ostream &operator<< (ostream&, const List&); public: List(void); ~List(); void Add(int); void Delete(int); private: Node* head; void DeleteHelper(Node*&, int); }; List::List(void) { head = NULL; } List::~List() { Node* t; while (head != NULL) { t = head; head = head->next; delete t; } } void List::Add(int i) { Node* t; t = new Node; t->data = i; t->next = head; head = t; } void List::Delete(int i) { DeleteHelper(head, i); } void List::DeleteHelper(Node* &n, int i) { Node* t; if (n != NULL) { if (n->data == i) { t = n; n = n->next; delete t; } else DeleteHelper(n->next, i); } } ostream &operator<< (ostream& output, const List& c) { Node* t; t = c.head; while ( t != NULL) { output << t; t = t->next; } output << endl; return output; } void main (void) { List l; l.Add(42); cout << l; l.Add(31); cout << l; l.Add(59); cout << l; l.Delete(31); cout << l; l.Delete(60); cout << l; }