定义一个节点:

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. struct Node{
  5. T data;
  6. Node* next;
  7. Node(const T& d):data(d), next(NULL){}
  8. operator T(){
  9. return data;
  10. }
  11. };
  12. int main(){
  13. Node a(10), b(20);
  14. cout << "a=" << a << ", b=" << b << endl;
  15. return 0;
  16. }

上面的运算符重载,先将a类型强转为T类型(也就是int),Java中的toString实际上就是类似的强转成string类型的。

输出一段简单的链表

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. struct Node{
  5. T data;
  6. Node* next;
  7. Node(const T& d):data(d), next(NULL){}
  8. operator T(){
  9. return data;
  10. }
  11. };
  12. int main(){
  13. Node a(10), b(20), c(30), d(40), e(50);
  14. a.next = &b;
  15. b.next = &c;
  16. c.next = &d;
  17. Node *p = &a;
  18. while(p != NULL){
  19. cout << *p << ' ';
  20. p = p->next;
  21. }
  22. cout << endl;
  23. return 0;
  24. }

给链表添加一个元素

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. struct Node{
  5. T data;
  6. Node* next;
  7. Node(const T& d):data(d), next(NULL){}
  8. operator T(){
  9. return data;
  10. }
  11. };
  12. //输出链表
  13. void showlist(Node* p){
  14. while(p != NULL){
  15. cout << *p << ' ';
  16. p = p->next;
  17. }
  18. cout << endl;
  19. }
  20. int main(){
  21. Node a(10), b(20), c(30), d(40), e(50);
  22. a.next = &b;
  23. b.next = &c;
  24. c.next = &d;
  25. showlist(&a);
  26. //添加一个节点
  27. Node* & p = b.next;//取b.next指针的别名
  28. e.next = p;
  29. p = &e;
  30. showlist(&a);
  31. //再添加一个节点
  32. Node* k = new Node(70);
  33. Node*& r = c.next;
  34. k->next = r;
  35. r = k;
  36. return 0;
  37. }

一个C++实现的链表如下:

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. class List{
  5. struct Node{
  6. T data;
  7. Node * next;
  8. //T()零初始化
  9. Node(const T& d=T()):data(d), next(0){}
  10. };
  11. Node * head; //头指针
  12. int len;
  13. public:
  14. List():head(NULL),len(0){ }
  15. //插入到任何位置
  16. //1、在链表里找到指向那个位置的指针pn
  17. //2、让新节点的next成员和pn指向同一个地方
  18. //3、再让pn指向新节点
  19. void insert(const T&d, int pos){
  20. Node*& pn = getptr(pos);
  21. Node* p = new Node(d);
  22. p->next = pn;
  23. pn = p;
  24. len++;
  25. }
  26. //返回链表长度
  27. int size()const{
  28. return len;
  29. }
  30. //尾插
  31. void push_back(const T& d){
  32. insert(d, size());
  33. }
  34. //找链表中指向指定位置的指针
  35. Node*& getptr(int pos){
  36. if(pos<0 || pos>size()) pos = 0;
  37. if(pos==0) return head;
  38. Node* p = head;
  39. for(int i=1; i<pos; i++)
  40. p = p->next;
  41. return p->next;
  42. }
  43. //前插
  44. void push_front(const T& d){
  45. insert(d, 0);
  46. }
  47. //遍历
  48. void travel()const{
  49. Node* p = head;
  50. while(p!=NULL){
  51. cout << p->data << ' ';
  52. p = p->next;
  53. }
  54. cout << endl;
  55. }
  56. //清空
  57. void clear(){
  58. while(head!=NULL){
  59. Node * p = head->next;
  60. delete head;
  61. head = p;
  62. }
  63. len = 0;
  64. }
  65. ~List(){
  66. clear();
  67. }
  68. //按照位置删除
  69. //1、找到链表中指向那个位置的指针
  70. //2、把那个指针另存一份
  71. //3、让那个指针指向下一个节点
  72. //4、释放那个指针的动态内存
  73. void erase(int pos){
  74. if(pos<0 || pos>=size()) return;
  75. Node *& pn = getptr(pos);
  76. Node * p = pn;
  77. pn = pn->next;
  78. delete p;
  79. --len;
  80. }
  81. //根据元素查找位置
  82. int find(const T& d)const{
  83. int pos = 0;
  84. Node* p = head;
  85. while(p){
  86. if(p->data==d) return pos;
  87. p = p->next;
  88. ++pos;
  89. }
  90. return -1;
  91. }
  92. //根据元素删除
  93. void remove(const T& d){
  94. int pos;
  95. while((pos = find(d)) != -1)
  96. erase(pos);
  97. }
  98. };
  99. int main(){
  100. List l;
  101. l.push_front(5);
  102. l.push_front(8);
  103. l.push_front(20);
  104. //在第2个位置插入9
  105. l.insert(9, 2);
  106. l.travel();
  107. return 0;
  108. }

通过上图可以看出来,如果我们要插入一个节点,就需要找到指向该位置的指针(或者前一个结点),比如上图的p->next指针就是我们需要找到的。删除一个节点也一样,需要找到指向该节点的指针。

2014-12-05 22:00 958人阅读 评论(0) 收藏 举报
 分类:
数据结构(8) 

版权声明:本文出自水寒的原创文章,未经博主允许不得转载。

定义一个节点:

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. struct Node{
  5. T data;
  6. Node* next;
  7. Node(const T& d):data(d), next(NULL){}
  8. operator T(){
  9. return data;
  10. }
  11. };
  12. int main(){
  13. Node a(10), b(20);
  14. cout << "a=" << a << ", b=" << b << endl;
  15. return 0;
  16. }

上面的运算符重载,先将a类型强转为T类型(也就是int),Java中的toString实际上就是类似的强转成string类型的。

输出一段简单的链表

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. struct Node{
  5. T data;
  6. Node* next;
  7. Node(const T& d):data(d), next(NULL){}
  8. operator T(){
  9. return data;
  10. }
  11. };
  12. int main(){
  13. Node a(10), b(20), c(30), d(40), e(50);
  14. a.next = &b;
  15. b.next = &c;
  16. c.next = &d;
  17. Node *p = &a;
  18. while(p != NULL){
  19. cout << *p << ' ';
  20. p = p->next;
  21. }
  22. cout << endl;
  23. return 0;
  24. }

给链表添加一个元素

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. struct Node{
  5. T data;
  6. Node* next;
  7. Node(const T& d):data(d), next(NULL){}
  8. operator T(){
  9. return data;
  10. }
  11. };
  12. //输出链表
  13. void showlist(Node* p){
  14. while(p != NULL){
  15. cout << *p << ' ';
  16. p = p->next;
  17. }
  18. cout << endl;
  19. }
  20. int main(){
  21. Node a(10), b(20), c(30), d(40), e(50);
  22. a.next = &b;
  23. b.next = &c;
  24. c.next = &d;
  25. showlist(&a);
  26. //添加一个节点
  27. Node* & p = b.next;//取b.next指针的别名
  28. e.next = p;
  29. p = &e;
  30. showlist(&a);
  31. //再添加一个节点
  32. Node* k = new Node(70);
  33. Node*& r = c.next;
  34. k->next = r;
  35. r = k;
  36. return 0;
  37. }

一个C++实现的链表如下:

  1. #include <iostream>
  2. using namespace std;
  3. typedef int T;
  4. class List{
  5. struct Node{
  6. T data;
  7. Node * next;
  8. //T()零初始化
  9. Node(const T& d=T()):data(d), next(0){}
  10. };
  11. Node * head; //头指针
  12. int len;
  13. public:
  14. List():head(NULL),len(0){ }
  15. //插入到任何位置
  16. //1、在链表里找到指向那个位置的指针pn
  17. //2、让新节点的next成员和pn指向同一个地方
  18. //3、再让pn指向新节点
  19. void insert(const T&d, int pos){
  20. Node*& pn = getptr(pos);
  21. Node* p = new Node(d);
  22. p->next = pn;
  23. pn = p;
  24. len++;
  25. }
  26. //返回链表长度
  27. int size()const{
  28. return len;
  29. }
  30. //尾插
  31. void push_back(const T& d){
  32. insert(d, size());
  33. }
  34. //找链表中指向指定位置的指针
  35. Node*& getptr(int pos){
  36. if(pos<0 || pos>size()) pos = 0;
  37. if(pos==0) return head;
  38. Node* p = head;
  39. for(int i=1; i<pos; i++)
  40. p = p->next;
  41. return p->next;
  42. }
  43. //前插
  44. void push_front(const T& d){
  45. insert(d, 0);
  46. }
  47. //遍历
  48. void travel()const{
  49. Node* p = head;
  50. while(p!=NULL){
  51. cout << p->data << ' ';
  52. p = p->next;
  53. }
  54. cout << endl;
  55. }
  56. //清空
  57. void clear(){
  58. while(head!=NULL){
  59. Node * p = head->next;
  60. delete head;
  61. head = p;
  62. }
  63. len = 0;
  64. }
  65. ~List(){
  66. clear();
  67. }
  68. //按照位置删除
  69. //1、找到链表中指向那个位置的指针
  70. //2、把那个指针另存一份
  71. //3、让那个指针指向下一个节点
  72. //4、释放那个指针的动态内存
  73. void erase(int pos){
  74. if(pos<0 || pos>=size()) return;
  75. Node *& pn = getptr(pos);
  76. Node * p = pn;
  77. pn = pn->next;
  78. delete p;
  79. --len;
  80. }
  81. //根据元素查找位置
  82. int find(const T& d)const{
  83. int pos = 0;
  84. Node* p = head;
  85. while(p){
  86. if(p->data==d) return pos;
  87. p = p->next;
  88. ++pos;
  89. }
  90. return -1;
  91. }
  92. //根据元素删除
  93. void remove(const T& d){
  94. int pos;
  95. while((pos = find(d)) != -1)
  96. erase(pos);
  97. }
  98. };
  99. int main(){
  100. List l;
  101. l.push_front(5);
  102. l.push_front(8);
  103. l.push_front(20);
  104. //在第2个位置插入9
  105. l.insert(9, 2);
  106. l.travel();
  107. return 0;
  108. }

通过上图可以看出来,如果我们要插入一个节点,就需要找到指向该位置的指针(或者前一个结点),比如上图的p->next指针就是我们需要找到的。删除一个节点也一样,需要找到指向该节点的指针。

01--数据结构——动态链表(C++)的更多相关文章

  1. 数据结构——动态链表(C++)

    定义一个节点: [cpp] view plain copy   print? #include <iostream> using namespace std; typedef int T; ...

  2. HDU 2095 find your present (2) 动态链表

    解题报告:输入一个n,后面紧跟着输入n个数,输入的这n个数中,除了有一个数的个数为奇数外,其它的数的个数都是偶数个,现在要你找出这个个数为奇数的这个数. 看起来好像很简单的样子,不过,这题的重点不在这 ...

  3. C++ 数据结构模板 队列 栈 动态链表 模板 Queue Stack List

    C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();...... (由于动态链表用的不多,若有错误望各位大神不吝赐教:) 队列: cl ...

  4. vc++基础班[28]---动态数组及动态链表的讲解

    C++中也有相应的动态数组.动态链表.映射表的模板类,就是STL中的:vector.list.map 他们属于C++标准中的一部分,对于程序的移植性来说也是不错的,但是在MFC编程中使用 CArray ...

  5. Linux C 数据结构 ->单向链表<-(~千金散尽还复来~)

    之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...

  6. Linux C 数据结构 ->单向链表

    之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...

  7. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  8. 使用C语言描述静态链表和动态链表

    静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...

  9. linux内核数据结构之链表

    linux内核数据结构之链表 1.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该 ...

随机推荐

  1. 系统和帮助-Linux基础知识

    iOS镜像: 硬盘分区:留出一些空间;实在不成,可安装完成以后,新增一块虚拟硬盘; 终端:terminal 用户界面: GUI:图形界面 GNome KDE CLI: bash,zsh,sh,csh, ...

  2. .net 单元测试

    都说测试驱动开发,但是想写好单元测试其实不容易,不是因为测试用例难以构造,而是因为很多时候方法非常复杂 其中部分测试想要完成就十分费力,其中让人崩溃的地方主要如下: 实例私有函数 实例静态私有函数 十 ...

  3. Spring Boot学习总结(3)——SpringBoot魅力所在

    使用Java做Web应用开发已经有近20年的历史了,从最初的Servlet1.0一步步演化到现在如此多的框架,库以及整个生态系统.经过这么长时间的发展,Java作为一个成熟的语言,也演化出了非常成熟的 ...

  4. HRBUST 1214 方格取数

    方格取数 Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged on HRBUST. Original ID: 12 ...

  5. Nikita and stack

    Nikita and stack time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. WinForm 登录窗体 + 单实例运行

    关于怎么在winform里增加登录窗体或者如何让winform程序单实例运行网上都很多例子. 然而两者结合起来呢? //Program.cs static class Program { public ...

  7. ZOJ - 3483 - Gaussian Prime

    先上题目: Gaussian Prime Time Limit: 3 Seconds      Memory Limit: 65536 KB In number theory, a Gaussian ...

  8. Spring Boot实例Hello World Demo

    Spring Boot要求Maven的版本达到3.2或以上. 实例: POM: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...

  9. spring boot使用外部tomcat部署

    1:pom里面的packaging修改为war(<packaging>war</packaging>) 2:在pom依赖spring-boot-starter-web排除内置的 ...

  10. HDU 4500

    哈哈,好爽好爽,刷水题报复社会啦... 哥这次省赛一定要拿一等奖,让你小看我,让你小看我.... #include <iostream> #include <cstdio> # ...