Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 865

D


5: // this line added to handle tail
6: if ( head == 0 ) tail = pt;
7:
8: head = pt;
9: theCount++;
10: }
11:
12: void List::append( int value )
13: {
14: ListCell *pt = new ListCell( value );
15: if ( head == 0 )
16: head = pt;
17: else
18: tail->next = pt;
19:
20: tail = pt;
21: theCount++;
22: }
23:
24: int List::is_present( int value ) const
25: {
26: if ( head == 0 ) return 0;
27: if ( head->val == value || tail->val == value )
28: return 1;
29:
30: ListCell *pt = head->next;
31: for (; pt != tail; pt = pt->next)
32: if ( pt->val == value )
33: return 1;
34:
35: return 0;
36: }


  1. The following is one possible answer:
    0: // Exercise 19.3
    1: template
    2: List::~List()
    3: {
    4: ListCell pt = head;
    5:
    6: while ( pt )
    7: {
    8: ListCell
    tmp = pt;
    9: pt = pt->next;
    10: delete tmp;
    11: }
    12: head = tail = 0;
    13: }
    14:
    15: template


32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 865

Free download pdf