鱼C论坛

 找回密码
 立即注册
查看: 860|回复: 3

[已解决]求助 链表

[复制链接]
发表于 2019-12-5 01:14:12 | 显示全部楼层 |阅读模式

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

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

x
求助链表 救命啊
最佳答案
2019-12-5 23:26:19
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. struct node
  6. {
  7.     char ch;
  8.     node *next;
  9. };
  10. node *creatlist() //创建链表
  11. {
  12.     node *beg = new node;
  13.     node *head = beg;    //头结点
  14.     beg->ch = cin.get(); //读取数据
  15.     while (beg->ch != '\n')
  16.     {
  17.         beg->next = new node; //申请新节点
  18.         beg = beg->next;      //指针指向新节点
  19.         beg->ch = cin.get();  //读取下一个数据
  20.     }
  21.     beg->next = nullptr; //尾结点指向空节点
  22.     return head;         //返回头结点
  23. }
  24. void printNode(node *list) //输出节点数据
  25. {
  26.     while (list->next != nullptr) //遍历节点
  27.     {
  28.         cout << list->ch;  //输出数据
  29.         list = list->next; //移动节点指针
  30.     }
  31. }
  32. int main(int argc, char const *argv[])
  33. {
  34.     node *list = creatlist();
  35.     printNode(list);
  36.     return 0;
  37. }
复制代码

============================================================
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-mghsc1tp.ozo --stdout=Microsoft-MIEngine-Out-sdslqt13.uig --stderr=Microsoft-MIEngine-Error-pw2tuepu.z3z --pid=Microsoft-MIEngine-Pid-3p4twawm.qjl --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
this is a nodelist!
this is a nodelist!

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

使用道具 举报

发表于 2019-12-5 13:06:10 | 显示全部楼层
???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-5 13:07:25 | 显示全部楼层
1.png

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2019-12-5 23:26:19 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. struct node
  6. {
  7.     char ch;
  8.     node *next;
  9. };
  10. node *creatlist() //创建链表
  11. {
  12.     node *beg = new node;
  13.     node *head = beg;    //头结点
  14.     beg->ch = cin.get(); //读取数据
  15.     while (beg->ch != '\n')
  16.     {
  17.         beg->next = new node; //申请新节点
  18.         beg = beg->next;      //指针指向新节点
  19.         beg->ch = cin.get();  //读取下一个数据
  20.     }
  21.     beg->next = nullptr; //尾结点指向空节点
  22.     return head;         //返回头结点
  23. }
  24. void printNode(node *list) //输出节点数据
  25. {
  26.     while (list->next != nullptr) //遍历节点
  27.     {
  28.         cout << list->ch;  //输出数据
  29.         list = list->next; //移动节点指针
  30.     }
  31. }
  32. int main(int argc, char const *argv[])
  33. {
  34.     node *list = creatlist();
  35.     printNode(list);
  36.     return 0;
  37. }
复制代码

============================================================
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-mghsc1tp.ozo --stdout=Microsoft-MIEngine-Out-sdslqt13.uig --stderr=Microsoft-MIEngine-Error-pw2tuepu.z3z --pid=Microsoft-MIEngine-Pid-3p4twawm.qjl --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
this is a nodelist!
this is a nodelist!

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 18:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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