鱼C论坛

 找回密码
 立即注册
查看: 1520|回复: 0

[技术交流] 单链表C++

[复制链接]
发表于 2019-10-2 00:28:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. #include <string>
  3. template <typename tp>
  4. class linklist
  5. {
  6. private:
  7.     typedef struct _linklist
  8.     {
  9.         tp date;                //数据
  10.         struct _linklist *next; //新节点
  11.     } node;
  12.     tp _eof;     //结束符
  13.     node *h, *e; //头指针h,尾指针e

  14. public:
  15.     linklist(tp str = NULL) : _eof(str), h(nullptr), e(nullptr) {}
  16.     void CreatLinkList(tp var = NULL) //创建链表
  17.     {
  18.         if (var != NULL)
  19.         {
  20.             _eof = var;
  21.         }
  22.         tp arg = NULL;
  23.         e = new node;
  24.         h = e;
  25.         while (true)
  26.         {
  27.             std::cin >> arg;
  28.             if (arg == _eof)
  29.             {
  30.                 e->date = NULL;
  31.                 e->next = nullptr;
  32.                 break;
  33.             }

  34.             e->date = arg;
  35.             e->next = new node;
  36.             e = e->next;
  37.         }
  38.         e->next = nullptr;
  39.     }
  40.     void showlist() //打印链表
  41.     {
  42.         while (h != nullptr)
  43.         {
  44.             std::cout << h->date << " ";
  45.             h = h->next;
  46.         }
  47.     }
  48. };
  49. int main(int argc, char const *argv[])
  50. {
  51.     linklist<char> l;
  52.     std::cout << "---请输入链表数据---" << std::endl;
  53.     l.CreatLinkList('a');
  54.     std::cout << "---打印链表数据---" << std::endl;
  55.     l.showlist();
  56.     return 0;
  57. }
复制代码

-------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。

E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ika0cgzr.mip --stdout=Microsoft-MIEngine-Out-agek2mvp.zox --stderr=Microsoft-MIEngine-Error-zofl5042.s5a --pid=Microsoft-MIEngine-Pid-rvgue0tj.yaq "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
---请输入链表数据---
zxcvbnm
123456789
!@#$%^&
a
---打印链表数据---
z x c v b n m 1 2 3 4 5 6 7 8 9 ! @ # $ % ^ &

E:\Users\86184\Documents\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 17:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表