定义一个节点:

  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. C#第二节课

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  2. 关于linux suse11忘记root密码修改方法

    SUSE Linux忘记root密码   1.重新启动机器,在出现grub引导界面后,按F2,在启动linux的选项里加上init=/bin/bash,通过给内核传递init=/bin/bash参数使 ...

  3. php中的empty()方法

    empty() 判断一个变量是否为“空”,isset() 判断一个变量是否已经设置.empty还会检测变量是否为空.为零.当一个变量值为0,empty() 认为这个变量同等于空,即相当于没有设置.例如 ...

  4. (12)Spring Boot改变JDK编译版本【从零开始学Spring Boot】

    Spring Boot在编译的时候,是有默认JDK版本的,如果我们期望使用我们要的JDK版本的话,那么要怎么配置呢? 这个只需要修改pom.xml文件的<build> -- <plu ...

  5. poj 1734 floyd求最小环,可得到环上的每个点

    #include<stdio.h> #include<string.h> #define inf  100000000 #define N 110 #define min(a, ...

  6. 转载 - 算法实践——舞蹈链(Dancing Links)算法求解数独

    出处:http://www.cnblogs.com/grenet/p/3163550.html 在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dan ...

  7. DJANGO里让用户自助修改密码

    参考了网上的实现,最终实现的各代码如下: changepwd.html模板文件: {% extends "Prism/index.html" %} {% load staticfi ...

  8. Unable to read the project file &#39;client.csproj&#39;. Could not load file or assembly &#39;Microsoft.Build.En

    错误具体信息: Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft. ...

  9. cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第六步--金币&amp;岩石加入而且管理

    说明:这里是借鉴:晓风残月前辈的博客,他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...

  10. Problem G 宝石合成 (内蒙古14年省赛)

    1117: Problem G 宝石合成 时间限制: 1 Sec  内存限制: 128 MB 提交: 18  解决: 4 [提交][状态][讨论版] 题目描写叙述 故事人物:豆豆强 走上致富之路的豆豆 ...