这两题是单向链表,所以与之前的习题分开。
List代码实现:http://www.cnblogs.com/alan-forever/archive/2012/09/12/2682437.html
关于list的题目:http://www.cnblogs.com/alan-forever/archive/2012/09/25/2702100.html
因为这两题除了添加元素的函数不同,其他操作都是一样。
3.11 假设一个单向链表的实现有一个头结点,但是没有尾结点,并且有且只有一个指向表头结点的指针。写一个类,使之包括的方法可以:
a、返回链表的大小。
b、打印链表。
c、检测值x是否在链表中。
d、如果值x没有在链表中,则将其加入链表。
e、如果值x在链表中,则删除这个值。
3.12 重复练习3.11,保持单向链表总是处于排序状态。见addSort函数!!
1 #include<iostream> 2 using namespace std; 3 4 template <typename Object> 5 class SingleList 6 { 7 private: 8 struct Node 9 { 10 Object data; 11 Node * next; 12 13 Node( const Object & d = Object( ), Node * n = NULL ) : data( d ), next( n ) { } 14 }; 15 16 public: 17 SingleList( ) 18 { 19 init( ); 20 } 21 ~SingleList( ) 22 { 23 eraseList( ); 24 delete head; 25 } 26 27 SingleList( const SingleList & rhs ) 28 { 29 init( ) 30 *this = rhs; 31 } 32 33 int size( ) 34 { 35 return theSize; 36 } 37 38 bool empty( ) 39 { 40 return size( ) == 0; 41 } 42 43 void add( const Object & x ) 44 { 45 Node * p = new Node( x ); 46 p->next = head->next; 47 head->next = p; 48 theSize++; 49 } 50 //插入函数,插入方式头插入。 51 void addSort( const Object & x ) 52 { 53 Node * p = new Node( x ); 54 Node * h = head->next; 55 Node * pp = head; 56 while( h != NULL && h->data < x ) 57 { 58 pp = h; 59 h = h->next; 60 } 61 pp->next = p; 62 p->next = h; 63 theSize++; 64 } 65 //插入的时候排序。 66 bool contains( const Object & x ) 67 { 68 Node * h = head->next; 69 while( h != NULL ) 70 { 71 if( h->data == x ) 72 return true; 73 else 74 h = h->next; 75 } 76 return false; 77 } 78 79 void remove( const Object & x ) 80 { 81 if( !contains( x ) ) 82 { 83 cout << "元素不存在链表里!" << endl; 84 } 85 86 else 87 { 88 Node * h = head; 89 Node * p; 90 while( 1 ) 91 { 92 if( h->next->data == x ) 93 { 94 p = h->next; 95 break; 96 } 97 else 98 h = h->next; 99 } 100 h->next = p->next; 101 delete p; 102 theSize--; 103 } 104 } 105 //删除结点函数。 106 void eraseList( ) 107 { 108 Node * p; 109 while( head->next != NULL ) 110 { 111 p = head->next; 112 head->next = head->next->next; 113 delete p; 114 theSize--; 115 } 116 } 117 //将链表清空。 118 void print( ) 119 { 120 Node * h = head->next; 121 while( h != NULL ) 122 { 123 cout << h->data << " "; 124 h = h->next; 125 } 126 cout << endl; 127 } 128 //输出链表的数据。 129 private: 130 int theSize; 131 Node * head; 132 133 void init( ) 134 { 135 theSize = 0; 136 head = new Node; 137 head->next = NULL; 138 } 139 }; 140 141 int main( ) 142 { 143 SingleList<int> l; 144 int num, m, i; 145 cout << "输入元素的数目:" << endl; 146 cin >> num; 147 for( i = 0; i != num; ++i ) 148 { 149 cin >> m; 150 //l.add( m ); 151 l.addSort( m ); 152 } 153 cout << "链表的数目:" << endl; 154 cout << l.size() << endl; 155 156 cout << "将所有元素输出来!" << endl; 157 l.print( ); 158 159 cout << "输入一个数字,如果这个数存在链表里就删除它,否则就添加它到链表里!" << endl; 160 cin >> m; 161 if( l.contains( m ) ) 162 { 163 cout << "存在链表里!" << endl; 164 l.remove( m ); 165 } 166 else 167 { 168 cout << "不存在链表里!" << endl; 169 //l.add( m ); 170 l.addSort( m ); 171 } 172 173 cout << "将所有元素输出来!" << endl; 174 l.print( ); 175 176 cout << "将链表的元素全部清除!" << endl; 177 l.eraseList( ); 178 179 cout << l.size() << endl; 180 if( l.empty( ) ) 181 cout << "链表为空!" << endl; 182 else 183 cout << "链表不为空!" << endl; 184 185 return 0; 186 }