1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
| #include <iostream> using namespace std; #define ElemType int typedef struct Node { ElemType data; struct Node *next; }Node,*LinkList;
void InitList(LinkList &L) { L = new Node; L->next= NULL; }
void CreateList_H(LinkList& L,int n) { for(int i = 0;i < n;i++) { Node* p = new Node; cout<<"请输入第"<<i+1<<"个元素:"; cin>>p->data; p->next = L->next; L->next = p; } }
void CreateList_R(LinkList& L,int n) { Node* r = L; for(int i = 0;i < n;i++) { Node* p = new Node; cout<<"请输入第"<<i+1<<"个元素:"; cin>>p->data; p->next = NULL; r->next = p; r = p; } }
void GetElem(LinkList L,int i) { Node* p = L->next; int j = 1; while(p && j < i) { p = p->next; j++; } if(!p || j > i) cout<<"i的值不合法!"<<endl; else cout<<p->data<<endl; }
Node* LocateElem(LinkList L,ElemType e) { Node* p = L->next; while(p && p->data != e) p = p->next; return p; }
void isEmpty(LinkList L) { if(L->next = NULL) cout<<"链表为空"<<endl; else cout<<"链表不为空"<<endl; }
void LinkInsert(LinkList& L,int i,ElemType e) { Node* p = L; int j = 0; while(p && j < i-1) { p = p->next; j++; } if(!p || j > i-1) cout<<"插入位置不合法!"<<endl; else{ Node* s = new Node; s->data = e; s->next = p->next; p->next = s; cout<<"数据插入成功!"<<endl; } }
void ListDelete(LinkList& L,int i) { Node* p = L; int j = 0; while((p->next) && j < i-1) { p = p->next; j++; } if(!(p->next)||j > j-1) cout<<"删除位置不合理!"<<endl; else { Node* q = p->next; p->next = q->next; delete q; cout<<"删除成功!"<<endl; } }
void PrintList(LinkList L) { Node* p; if(L->next == NULL) cout<<"该单链表为空!"<<endl; else { p = L->next; while(p) { cout<<p->data<<' '; p = p->next; } cout<<endl; } }
void ClearList(LinkList L) { LinkList p,q; p = L->next; while(p != NULL) { q = p->next; delete p; p = q; } L->next = NULL; cout<<"链表已清空!"<<endl; }
void DestoryList(LinkList L) { LinkList p; while(L) { p = L; L = L->next; delete p; } cout<<"链表已删除"<<endl; }
int GetLen(LinkList L) { int len = 0; Node* p = L; if(L == NULL) return -1; while(p->next != NULL) { p = p->next; len++; } return len; }
void Merge(LinkList A,LinkList B,LinkList &C) { Node* p = A->next; Node* q = B->next; C = A; C->next = NULL; Node* r = C; while(p != NULL && q != NULL) { if(p->data <= q->data) { r->next = p; p = p->next; r = r->next; }else{ r->next = q; q = q->next; r = r->next; } } if(p != NULL) r->next = p; if(q != NULL) r->next = q; } void menu() { cout << "---------------------------" << endl; cout << "0---创建表" << endl; cout << "1---清空单链表" << endl; cout << "2---判断单链表是否为空" << endl; cout << "3---求单链表长度" << endl; cout << "4---获取单链表指定位置元素" << endl; cout << "5---在单链表指定位置插入元素" << endl; cout << "6---删除单链表指定位置元素" << endl; cout << "7---显示单链表" << endl; cout << "8---两个有序链表归并" << endl; cout << " 退出,输入一个负数!" << endl; cout << "请输入操作代码:"; } int main() { LinkList L; InitList(L); int num; menu(); cin>>num; int index; int n; while(num >= 0) { if (num == 8) { Node* A; InitList(A); cout << "注意两个链表元素大小从小到大排序" << endl; cout << "创建第一个链表" << endl; cout << "请输入元素个数:"; cin >> n; CreateList_R(A, n); Node* B; InitList(B); cout << "创建第二个链表" << endl; cout << "请输入元素个数:"; cin >> n; CreateList_R(B, n); Node* C; InitList(C); Merge(A, B, C); cout << "合并后链表元素为"; PrintList(C); menu(); cin >> num; continue; } if (GetLen(L) == 0 && num != 0) { cout << "链表长度为0,请先为链表添加数据。" << endl; cout << "请输入表的长度:"; cin >> n; CreateList_R(L, n); menu(); cin >> num; continue; } switch (num) { case 0: int n; cout << "请输入表的长度"; cin >> n; CreateList_R(L, n); break; case 1: ClearList(L); break; case 2: isEmpty(L); break; case 3: cout << "链表长度为:"<<GetLen(L) << endl; break; case 4: int index; cout << "请输入位置:"; cin >> index; cout << "该位置元素为:"; GetElem(L, index); break; case 5: ElemType e; cout << "请输入位置和元素:"; cin >> index >> e; LinkInsert(L, index, e); break; case 6: cout << "请输入位置:"; cin >> index; ListDelete(L, index); break; case 7: PrintList(L); break; default: cout << "输入数字有误,请重新输入。" << endl; break; } menu(); cin >> num; } return 0; }
|