#include<iostream> #include<stdlib.h> using namespace std; typedef struct tagNote { int nNumber; struct tagNote* pNext; }Note; bool CreateList(Note*&pListHead); bool DisposeList(Note*&pListHead); bool ListInsertItem(Note*&pListHead, int nValue, int nIndex = -1); bool ListItemPushBack(Note*&pListHead,int nValue); bool ListItemPushFront(Note*&pListHead,int nValue); bool ListDeleteItem(Note*&pListItem,int nIndex=-1); bool ListDeleteFirstItem(Note*&pListHead); bool ListDeleteLastItem(Note*&pListHead); bool ListGetFirstItem(Note*&pListHead,int &nValue); bool ListGetLastItem(Note*&pListHead,int &nValue); bool ListGetItemNumber(Note*&pListHead,int &nNumber); bool ListGetItem(Note*&pListHead,int&nValue,int nIndex=-1); bool ListPrint(Note*&pListHead); void ShowMenu(); int main() { Note*pListHead = NULL; int nSelect = 0; bool isLoop = true; int nTmpInput1, nTmpInput2, nTmpOut; while (isLoop) { ShowMenu(); cin >> nSelect; switch (nSelect) { case 0: isLoop = false; break; case 1: if (!CreateList(pListHead)) { cout << "創建鏈表失敗" << endl; } else { cout << "創建鏈表成功" << endl; } break; case 2: cout << "請輸入插入項的值:" << endl; cin >> nTmpInput1; if (!ListItemPushFront(pListHead, nTmpInput1)) { cout << "輸入插入值(" << nTmpInput1 << ")失敗" << endl; } else { cout << "插入項成功" << endl; } break; case 3: cout << "請輸入插入項的值:" << endl; cin >> nTmpInput1; if (!ListItemPushBack(pListHead, nTmpInput1)) { cout << "輸入插入值(" << nTmpInput1 << ")失敗" << endl; } else { cout << "插入值成功" << endl; } break; case 4: cout << "請輸入插入項的值:" << endl; cin >> nTmpInput1; cout << "請輸入插入項的位置:" << endl; cin >> nTmpInput2; if (!ListInsertItem(pListHead, nTmpInput1, nTmpInput2)) { cout << "輸入插入值(" << nTmpInput1 << ")在位置" << nTmpInput2 << ")失敗" << endl; } else { cout << "插入項成功" << endl; } break; case 5: if (!ListGetFirstItem(pListHead, nTmpOut)) { cout << "取第一個值失敗" << endl; } else { cout << "取第一個值失敗" << nTmpOut << endl; } break; case 6: if (!ListGetLastItem(pListHead, nTmpOut)) { cout << "取最後一個值失敗" << endl; } else { cout << "取最後一個值爲" << nTmpOut << endl; } break; case 7: cout << "請輸入要取值的位置:" << endl; cin >> nTmpInput1; if (!ListGetItem(pListHead, nTmpOut, nTmpInput1)) { cout << "取值在位置(" << nTmpInput1 << ")失敗" << endl; } else { cout << "取值在位置(" << nTmpInput1 << ")值爲" << nTmpOut << endl; } break; case 8: if (!ListDeleteFirstItem(pListHead)) { cout << "刪除第一個項目失敗" << endl; } else { cout << "刪除第一個項目成功" << endl; } break; case 9: if (!ListDeleteLastItem(pListHead)) { cout << "刪除最後一個項目失敗" << endl; } else { cout << "刪除最後一個項目失敗" << endl; } break; case 10: cout << "請輸入要刪除項的位置:" << endl; cin >> nTmpInput1; if (!ListDeleteItem(pListHead, nTmpInput1)) { cout << "刪除第(" << nTmpInput1 << ")項目失敗" << endl; } else { cout << "刪除第(" << nTmpInput1 << ")項目成功" << endl; } case 11: if (!ListGetItemNumber(pListHead, nTmpOut)) { cout << "取項目個數失敗" << endl; } else { cout << "取項目個數爲" << nTmpOut << endl; } break; case 12: if (!DisposeList(pListHead)) { cout << "銷燬鏈失敗" << endl; } else { cout << "銷燬鏈成功" << endl; } case 13: ListPrint(pListHead); break; default: break; } if (isLoop) { system("pause"); system("cls"); } } DisposeList(pListHead); return 0; } void ShowMenu() { cout << "--------------------------------" << endl; cout << "1.\t創建一個單向鏈表" << endl; cout << "2.\t從頭部插入一個項" << endl; cout << "3.\t從尾部插入一個項" << endl; cout << "4.\t從指定位置插入一個項" << endl; cout << "5.\t取出第一個項的值" << endl; cout << "6.\t取出最後一個項的值" << endl; cout << "7.\t取出指定項的值" << endl; cout << "8.\t刪除第一個項的值" << endl; cout << "9.\t刪除最後一個項的值" << endl; cout << "10.\t刪除指定項的值" << endl; cout << "11.\t得到表中的項目個數" << endl; cout << "12.\t銷燬當前鏈表" << endl; cout << "13.\t打印整個鏈表" << endl; cout << "0.\t推出程序" << endl; cout << "--------------------------------" << endl; cout << "請輸入你的選擇:\t"; } bool CreateList(Note*&pListHead) { if (pListHead != NULL) { return false; } else { pListHead = new Note; pListHead->nNumber = 0; pListHead->pNext = NULL; return true; } } bool DisposeList(Note*&pListHead) { if (pListHead == NULL) { return false; } else { while (ListDeleteLastItem(pListHead)) { delete pListHead; pListHead = NULL; } return true; } } bool ListInsertItem(Note*&pListHead, int nValue, int nIndex) { if (pListHead == NULL) { return false; } int nNum = 0; ListGetItemNumber(pListHead, nNum); if (nIndex > nNum) { return false; } else if (nIndex == -1) { nIndex = nNum; } Note*pTmp = pListHead; for (int i = 0; i < nIndex; i++) { pTmp = pTmp->pNext; } Note*pNext = pTmp->pNext; pTmp->pNext = new Note; pTmp->pNext->nNumber = nValue; pTmp->pNext->pNext = pNext; return true; } bool ListItemPushFront(Note*&pListHead, int nValue) { return ListInsertItem(pListHead, nValue, 0); } bool ListItemPushBack(Note*&pListHead, int nValue) { return ListInsertItem(pListHead, nValue, -1); } bool ListDeleteItem(Note*&pListHead, int nIndex) { if (pListHead == NULL) { return false; } int nNum = 0; ListGetItemNumber(pListHead, nNum); if (nNum == 0) { return false; } if (nIndex > nNum) { return false; } else if (nIndex == -1) { nIndex = nNum - 1; } Note*pTmp = pListHead; for (int i = 0; i < nIndex; i++ ) { pTmp = pTmp->pNext; } Note*pNext = pTmp->pNext->pNext; delete pTmp->pNext; return true; } bool ListDeleteFirstItem(Note*&pListHead) { return ListDeleteItem(pListHead, 0); } bool ListDeleteLastItem(Note*&pListHead) { return ListDeleteItem(pListHead, -1); } bool ListGetItem(Note*&pListHead, int&nValue, int nIndex) { if (pListHead == NULL) { return NULL; } int nNum = 0; ListGetItemNumber(pListHead, nNum); if (nIndex >= nNum) { return false; } else if (nIndex == -1) { nIndex = nNum; } Note*pTmp = pListHead; for (int i = 0; i <= nIndex; i++) { pTmp = pTmp->pNext; } nValue = pTmp->nNumber; return true; } bool ListGetFirstItem(Note*&pListHead, int&nValue) { return ListGetItem(pListHead, nValue, 0); } bool ListGetLastItem(Note*&pListHead, int&nValue) { return ListGetItem(pListHead, nValue, -1); } bool ListGetItemNumber(Note*&pListHead, int&nNumber) { if (pListHead == NULL) { return false; } Note*pTmp = pListHead->pNext; nNumber = 0; while (pTmp != NULL) { nNumber++; pTmp = pTmp->pNext; } return true; } bool ListPrint(Note*&pListHead) { if (pListHead == NULL) { return false; } Note*pTmp = pListHead->pNext; int nIndex = 0; while (pTmp != NULL) { cout << nIndex++ << ":\t" << pTmp->nNumber << endl; pTmp = pTmp->pNext; } return true; }
單鏈表程序示範(C++)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.