|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <iostream>
- #include <string>
- template <typename tp>
- class linklist
- {
- private:
- typedef struct _linklist
- {
- tp date; //数据
- struct _linklist *next; //新节点
- } node;
- tp _eof; //结束符
- node *h, *e; //头指针h,尾指针e
- public:
- linklist(tp str = NULL) : _eof(str), h(nullptr), e(nullptr) {}
- void CreatLinkList(tp var = NULL) //创建链表
- {
- if (var != NULL)
- {
- _eof = var;
- }
- tp arg = NULL;
- e = new node;
- h = e;
- while (true)
- {
- std::cin >> arg;
- if (arg == _eof)
- {
- e->date = NULL;
- e->next = nullptr;
- break;
- }
- e->date = arg;
- e->next = new node;
- e = e->next;
- }
- e->next = nullptr;
- }
- void showlist() //打印链表
- {
- while (h != nullptr)
- {
- std::cout << h->date << " ";
- h = h->next;
- }
- }
- };
- int main(int argc, char const *argv[])
- {
- linklist<char> l;
- std::cout << "---请输入链表数据---" << std::endl;
- l.CreatLinkList('a');
- std::cout << "---打印链表数据---" << std::endl;
- l.showlist();
- return 0;
- }
复制代码
-------------------------------------------------------------------------------
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> |
|