ITPub博客

首页 > 应用开发 > IT综合 > C++单链表示例

C++单链表示例

原创 IT综合 作者:YongjianChang 时间:2014-04-20 02:30:52 0 删除 编辑

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. typedef struct tagNote
  5. {
  6.     int nNumber;
  7.     struct tagNote* pNext;
  8. }Note;

  9. bool CreateList(Note*&pListHead);
  10. bool DisposeList(Note*&pListHead);
  11. bool ListInsertItem(Note*&pListHead, int nValue, int nIndex = -1);
  12. bool ListItemPushBack(Note*&pListHead,int nValue);
  13. bool ListItemPushFront(Note*&pListHead,int nValue);
  14. bool ListDeleteItem(Note*&pListItem,int nIndex=-1);
  15. bool ListDeleteFirstItem(Note*&pListHead);
  16. bool ListDeleteLastItem(Note*&pListHead);
  17. bool ListGetFirstItem(Note*&pListHead,int &nValue);
  18. bool ListGetLastItem(Note*&pListHead,int &nValue);
  19. bool ListGetItemNumber(Note*&pListHead,int &nNumber);
  20. bool ListGetItem(Note*&pListHead,int&nValue,int nIndex=-1);
  21. bool ListPrint(Note*&pListHead);
  22. void ShowMenu();

  23. int main()
  24. {
  25.     Note*pListHead = NULL;
  26.     int nSelect = 0;
  27.     bool isLoop = true;
  28.     int nTmpInput1, nTmpInput2, nTmpOut;
  29.     while (isLoop)
  30.     {
  31.         ShowMenu();
  32.         cin >> nSelect;
  33.         switch (nSelect)
  34.         {
  35.         case 0:
  36.             isLoop = false;
  37.             break;
  38.         case 1:
  39.             if (!CreateList(pListHead))
  40.             {
  41.                 cout << \"创建链表失败\" << endl;
  42.             }
  43.             else
  44.             {
  45.                 cout << \"创建链表成功\" << endl;
  46.             }
  47.             break;
  48.         case 2:
  49.             cout << \"请输入插入项的值:\" << endl;
  50.             cin >> nTmpInput1;
  51.             if (!ListItemPushFront(pListHead, nTmpInput1))
  52.             {
  53.                 cout << \"输入插入值(\" << nTmpInput1 << \")失败\" << endl;
  54.             }
  55.             else
  56.             {
  57.                 cout << \"插入项成功\" << endl;
  58.             }
  59.             break;
  60.         case 3:
  61.             cout << \"请输入插入项的值:\" << endl;
  62.             cin >> nTmpInput1;
  63.             if (!ListItemPushBack(pListHead, nTmpInput1))
  64.             {
  65.                 cout << \"输入插入值(\" << nTmpInput1 << \")失败\" << endl;
  66.             }
  67.             else
  68.             {
  69.                 cout << \"插入值成功\" << endl;
  70.             }
  71.             break;
  72.         case 4:
  73.             cout << \"请输入插入项的值:\" << endl;
  74.             cin >> nTmpInput1;
  75.             cout << \"请输入插入项的位置:\" << endl;
  76.             cin >> nTmpInput2;
  77.             if (!ListInsertItem(pListHead, nTmpInput1, nTmpInput2))
  78.             {
  79.                 cout << \"输入插入值(\" << nTmpInput1 << \")在位置\" << nTmpInput2 << \")失败\" << endl;
  80.             }
  81.             else
  82.             {
  83.                 cout << \"插入项成功\" << endl;
  84.             }
  85.             break;
  86.         case 5:
  87.             if (!ListGetFirstItem(pListHead, nTmpOut))
  88.             {
  89.                 cout << \"取第一个值失败\" << endl;
  90.             }
  91.             else
  92.             {
  93.                 cout << \"取第一个值失败\" << nTmpOut << endl;
  94.             }
  95.             break;
  96.         case 6:
  97.             if (!ListGetLastItem(pListHead, nTmpOut))
  98.             {
  99.                 cout << \"取最后一个值失败\" << endl;
  100.             }
  101.             else
  102.             {
  103.                 cout << \"取最后一个值为\" << nTmpOut << endl;
  104.             }
  105.             break;
  106.         case 7:
  107.             cout << \"请输入要取值的位置:\" << endl;
  108.             cin >> nTmpInput1;
  109.             if (!ListGetItem(pListHead, nTmpOut, nTmpInput1))
  110.             {
  111.                 cout << \"取值在位置(\" << nTmpInput1 << \")失败\" << endl;
  112.             }
  113.             else
  114.             {
  115.                 cout << \"取值在位置(\" << nTmpInput1 << \")值为\" << nTmpOut << endl;
  116.             }
  117.             break;
  118.         case 8:
  119.             if (!ListDeleteFirstItem(pListHead))
  120.             {
  121.                 cout << \"删除第一个项目失败\" << endl;
  122.             }
  123.             else
  124.             {
  125.                 cout << \"删除第一个项目成功\" << endl;
  126.             }
  127.             break;
  128.         case 9:
  129.             if (!ListDeleteLastItem(pListHead))
  130.             {
  131.                 cout << \"删除最后一个项目失败\" << endl;
  132.             }
  133.             else
  134.             {
  135.                 cout << \"删除最后一个项目失败\" << endl;
  136.             }
  137.             break;
  138.         case 10:
  139.             cout << \"请输入要删除项的位置:\" << endl;
  140.             cin >> nTmpInput1;
  141.             if (!ListDeleteItem(pListHead, nTmpInput1))
  142.             {
  143.                 cout << \"删除第(\" << nTmpInput1 << \")项目失败\" << endl;
  144.             }
  145.             else
  146.             {
  147.                 cout << \"删除第(\" << nTmpInput1 << \")项目成功\" << endl;
  148.             }
  149.         case 11:
  150.             if (!ListGetItemNumber(pListHead, nTmpOut))
  151.             {
  152.                 cout << \"取项目个数失败\" << endl;
  153.             }
  154.             else
  155.             {
  156.                 cout << \"取项目个数为\" << nTmpOut << endl;
  157.             }
  158.             break;
  159.         case 12:
  160.             if (!DisposeList(pListHead))
  161.             {
  162.                 cout << \"销毁链失败\" << endl;
  163.             }
  164.             else
  165.             {
  166.                 cout << \"销毁链成功\" << endl;
  167.             }
  168.         case 13:
  169.             ListPrint(pListHead);
  170.             break;
  171.         default:
  172.             break;
  173.         }
  174.         if (isLoop)
  175.         {
  176.             system(\"pause\");
  177.             system(\"cls\");
  178.         }
  179.     }
  180.     DisposeList(pListHead);
  181.     return 0;
  182. }

  183. void ShowMenu()
  184. {
  185.     cout << \"--------------------------------\" << endl;
  186.     cout << \"1.\\t创建一个单向链表\" << endl;
  187.     cout << \"2.\\t从头部插入一个项\" << endl;
  188.     cout << \"3.\\t从尾部插入一个项\" << endl;
  189.     cout << \"4.\\t从指定位置插入一个项\" << endl;
  190.     cout << \"5.\\t取出第一个项的值\" << endl;
  191.     cout << \"6.\\t取出最后一个项的值\" << endl;
  192.     cout << \"7.\\t取出指定项的值\" << endl;
  193.     cout << \"8.\\t删除第一个项的值\" << endl;
  194.     cout << \"9.\\t删除最后一个项的值\" << endl;
  195.     cout << \"10.\\t删除指定项的值\" << endl;
  196.     cout << \"11.\\t得到表中的项目个数\" << endl;
  197.     cout << \"12.\\t销毁当前链表\" << endl;
  198.     cout << \"13.\\t打印整个链表\" << endl;
  199.     cout << \"0.\\t推出程序\" << endl;
  200.     cout << \"--------------------------------\" << endl;
  201.     cout << \"请输入你的选择:\\t\";
  202. }

  203. bool CreateList(Note*&pListHead)
  204. {
  205.     if (pListHead != NULL)
  206.     {
  207.         return false;
  208.     }
  209.     else
  210.     {
  211.         pListHead = new Note;
  212.         pListHead->nNumber = 0;
  213.         pListHead->pNext = NULL;
  214.         return true;
  215.     }
  216. }

  217. bool DisposeList(Note*&pListHead)
  218. {
  219.     if (pListHead == NULL)
  220.     {
  221.         return false;
  222.     }
  223.     else
  224.     {
  225.         while (ListDeleteLastItem(pListHead))
  226.         {
  227.             delete pListHead;
  228.             pListHead = NULL;
  229.         }
  230.         return true;
  231.     }
  232. }

  233. bool ListInsertItem(Note*&pListHead, int nValue, int nIndex)
  234. {
  235.     if (pListHead == NULL)
  236.     {
  237.         return false;
  238.     }
  239.     int nNum = 0;
  240.     ListGetItemNumber(pListHead, nNum);
  241.     if (nIndex > nNum)
  242.     {
  243.         return false;
  244.     }
  245.     else if (nIndex == -1)
  246.     {
  247.         nIndex = nNum;
  248.     }
  249.     Note*pTmp = pListHead;
  250.     for (int i = 0; i < nIndex; i++)
  251.     {
  252.         pTmp = pTmp->pNext;
  253.     }
  254.     Note*pNext = pTmp->pNext;
  255.     pTmp->pNext = new Note;
  256.     pTmp->pNext->nNumber = nValue;
  257.     pTmp->pNext->pNext = pNext;
  258.     return true;
  259. }

  260. bool ListItemPushFront(Note*&pListHead, int nValue)
  261. {
  262.     return ListInsertItem(pListHead, nValue, 0);
  263. }

  264. bool ListItemPushBack(Note*&pListHead, int nValue)
  265. {
  266.     return ListInsertItem(pListHead, nValue, -1);
  267. }

  268. bool ListDeleteItem(Note*&pListHead, int nIndex)
  269. {
  270.     if (pListHead == NULL)
  271.     {
  272.         return false;
  273.     }
  274.     int nNum = 0;
  275.     ListGetItemNumber(pListHead, nNum);
  276.     if (nNum == 0)
  277.     {
  278.         return false;
  279.     }
  280.     if (nIndex > nNum)
  281.     {
  282.         return false;
  283.     }
  284.     else if (nIndex == -1)
  285.     {
  286.         nIndex = nNum - 1;
  287.     }
  288.     Note*pTmp = pListHead;
  289.     for (int i = 0; i < nIndex; i++ )
  290.     {
  291.         pTmp = pTmp->pNext;
  292.     }
  293.     Note*pNext = pTmp->pNext->pNext;
  294.     delete pTmp->pNext;
  295.     return true;
  296. }

  297. bool ListDeleteFirstItem(Note*&pListHead)
  298. {
  299.     return ListDeleteItem(pListHead, 0);
  300. }

  301. bool ListDeleteLastItem(Note*&pListHead)
  302. {
  303.     return ListDeleteItem(pListHead, -1);
  304. }

  305. bool ListGetItem(Note*&pListHead, int&nValue, int nIndex)
  306. {
  307.     if (pListHead == NULL)
  308.     {
  309.         return NULL;
  310.     }
  311.     int nNum = 0;
  312.     ListGetItemNumber(pListHead, nNum);
  313.     if (nIndex >= nNum)
  314.     {
  315.         return false;
  316.     }
  317.     else if (nIndex == -1)
  318.     {
  319.         nIndex = nNum;
  320.     }
  321.     Note*pTmp = pListHead;
  322.     for (int i = 0; i <= nIndex; i++)
  323.     {
  324.         pTmp = pTmp->pNext;
  325.     }
  326.     nValue = pTmp->nNumber;
  327.     return true;
  328. }

  329. bool ListGetFirstItem(Note*&pListHead, int&nValue)
  330. {
  331.     return ListGetItem(pListHead, nValue, 0);
  332. }

  333. bool ListGetLastItem(Note*&pListHead, int&nValue)
  334. {
  335.     return ListGetItem(pListHead, nValue, -1);
  336. }

  337. bool ListGetItemNumber(Note*&pListHead, int&nNumber)
  338. {
  339.     if (pListHead == NULL)
  340.     {
  341.         return false;
  342.     }
  343.     Note*pTmp = pListHead->pNext;
  344.     nNumber = 0;
  345.     while (pTmp != NULL)
  346.     {
  347.         nNumber++;
  348.         pTmp = pTmp->pNext;
  349.     }
  350.     return true;
  351. }

  352. bool ListPrint(Note*&pListHead)
  353. {
  354.     if (pListHead == NULL)
  355.     {
  356.         return false;
  357.     }
  358.     Note*pTmp = pListHead->pNext;
  359.     int nIndex = 0;
  360.     while (pTmp != NULL)
  361.     {
  362.         cout << nIndex++ << \":\\t\" << pTmp->nNumber << endl;
  363.         pTmp = pTmp->pNext;
  364.     }
  365.     return true;
  366. }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29617676/viewspace-1145457/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-04-20

  • 博文量
    1
  • 访问量
    2328