01--数据结构——动态链表(C++)
定义一个节点:
- #include <iostream>
- using namespace std;
- typedef int T;
- struct Node{
- T data;
- Node* next;
- Node(const T& d):data(d), next(NULL){}
- operator T(){
- return data;
- }
- };
- int main(){
- Node a(10), b(20);
- cout << "a=" << a << ", b=" << b << endl;
- return 0;
- }
上面的运算符重载,先将a类型强转为T类型(也就是int),Java中的toString实际上就是类似的强转成string类型的。
输出一段简单的链表
- #include <iostream>
- using namespace std;
- typedef int T;
- struct Node{
- T data;
- Node* next;
- Node(const T& d):data(d), next(NULL){}
- operator T(){
- return data;
- }
- };
- int main(){
- Node a(10), b(20), c(30), d(40), e(50);
- a.next = &b;
- b.next = &c;
- c.next = &d;
- Node *p = &a;
- while(p != NULL){
- cout << *p << ' ';
- p = p->next;
- }
- cout << endl;
- return 0;
- }
给链表添加一个元素
- #include <iostream>
- using namespace std;
- typedef int T;
- struct Node{
- T data;
- Node* next;
- Node(const T& d):data(d), next(NULL){}
- operator T(){
- return data;
- }
- };
- //输出链表
- void showlist(Node* p){
- while(p != NULL){
- cout << *p << ' ';
- p = p->next;
- }
- cout << endl;
- }
- int main(){
- Node a(10), b(20), c(30), d(40), e(50);
- a.next = &b;
- b.next = &c;
- c.next = &d;
- showlist(&a);
- //添加一个节点
- Node* & p = b.next;//取b.next指针的别名
- e.next = p;
- p = &e;
- showlist(&a);
- //再添加一个节点
- Node* k = new Node(70);
- Node*& r = c.next;
- k->next = r;
- r = k;
- return 0;
- }
一个C++实现的链表如下:
- #include <iostream>
- using namespace std;
- typedef int T;
- class List{
- struct Node{
- T data;
- Node * next;
- //T()零初始化
- Node(const T& d=T()):data(d), next(0){}
- };
- Node * head; //头指针
- int len;
- public:
- List():head(NULL),len(0){ }
- //插入到任何位置
- //1、在链表里找到指向那个位置的指针pn
- //2、让新节点的next成员和pn指向同一个地方
- //3、再让pn指向新节点
- void insert(const T&d, int pos){
- Node*& pn = getptr(pos);
- Node* p = new Node(d);
- p->next = pn;
- pn = p;
- len++;
- }
- //返回链表长度
- int size()const{
- return len;
- }
- //尾插
- void push_back(const T& d){
- insert(d, size());
- }
- //找链表中指向指定位置的指针
- Node*& getptr(int pos){
- if(pos<0 || pos>size()) pos = 0;
- if(pos==0) return head;
- Node* p = head;
- for(int i=1; i<pos; i++)
- p = p->next;
- return p->next;
- }
- //前插
- void push_front(const T& d){
- insert(d, 0);
- }
- //遍历
- void travel()const{
- Node* p = head;
- while(p!=NULL){
- cout << p->data << ' ';
- p = p->next;
- }
- cout << endl;
- }
- //清空
- void clear(){
- while(head!=NULL){
- Node * p = head->next;
- delete head;
- head = p;
- }
- len = 0;
- }
- ~List(){
- clear();
- }
- //按照位置删除
- //1、找到链表中指向那个位置的指针
- //2、把那个指针另存一份
- //3、让那个指针指向下一个节点
- //4、释放那个指针的动态内存
- void erase(int pos){
- if(pos<0 || pos>=size()) return;
- Node *& pn = getptr(pos);
- Node * p = pn;
- pn = pn->next;
- delete p;
- --len;
- }
- //根据元素查找位置
- int find(const T& d)const{
- int pos = 0;
- Node* p = head;
- while(p){
- if(p->data==d) return pos;
- p = p->next;
- ++pos;
- }
- return -1;
- }
- //根据元素删除
- void remove(const T& d){
- int pos;
- while((pos = find(d)) != -1)
- erase(pos);
- }
- };
- int main(){
- List l;
- l.push_front(5);
- l.push_front(8);
- l.push_front(20);
- //在第2个位置插入9
- l.insert(9, 2);
- l.travel();
- return 0;
- }
通过上图可以看出来,如果我们要插入一个节点,就需要找到指向该位置的指针(或者前一个结点),比如上图的p->next指针就是我们需要找到的。删除一个节点也一样,需要找到指向该节点的指针。

版权声明:本文出自水寒的原创文章,未经博主允许不得转载。
定义一个节点:
- #include <iostream>
- using namespace std;
- typedef int T;
- struct Node{
- T data;
- Node* next;
- Node(const T& d):data(d), next(NULL){}
- operator T(){
- return data;
- }
- };
- int main(){
- Node a(10), b(20);
- cout << "a=" << a << ", b=" << b << endl;
- return 0;
- }
上面的运算符重载,先将a类型强转为T类型(也就是int),Java中的toString实际上就是类似的强转成string类型的。
输出一段简单的链表
- #include <iostream>
- using namespace std;
- typedef int T;
- struct Node{
- T data;
- Node* next;
- Node(const T& d):data(d), next(NULL){}
- operator T(){
- return data;
- }
- };
- int main(){
- Node a(10), b(20), c(30), d(40), e(50);
- a.next = &b;
- b.next = &c;
- c.next = &d;
- Node *p = &a;
- while(p != NULL){
- cout << *p << ' ';
- p = p->next;
- }
- cout << endl;
- return 0;
- }
给链表添加一个元素
- #include <iostream>
- using namespace std;
- typedef int T;
- struct Node{
- T data;
- Node* next;
- Node(const T& d):data(d), next(NULL){}
- operator T(){
- return data;
- }
- };
- //输出链表
- void showlist(Node* p){
- while(p != NULL){
- cout << *p << ' ';
- p = p->next;
- }
- cout << endl;
- }
- int main(){
- Node a(10), b(20), c(30), d(40), e(50);
- a.next = &b;
- b.next = &c;
- c.next = &d;
- showlist(&a);
- //添加一个节点
- Node* & p = b.next;//取b.next指针的别名
- e.next = p;
- p = &e;
- showlist(&a);
- //再添加一个节点
- Node* k = new Node(70);
- Node*& r = c.next;
- k->next = r;
- r = k;
- return 0;
- }
一个C++实现的链表如下:
- #include <iostream>
- using namespace std;
- typedef int T;
- class List{
- struct Node{
- T data;
- Node * next;
- //T()零初始化
- Node(const T& d=T()):data(d), next(0){}
- };
- Node * head; //头指针
- int len;
- public:
- List():head(NULL),len(0){ }
- //插入到任何位置
- //1、在链表里找到指向那个位置的指针pn
- //2、让新节点的next成员和pn指向同一个地方
- //3、再让pn指向新节点
- void insert(const T&d, int pos){
- Node*& pn = getptr(pos);
- Node* p = new Node(d);
- p->next = pn;
- pn = p;
- len++;
- }
- //返回链表长度
- int size()const{
- return len;
- }
- //尾插
- void push_back(const T& d){
- insert(d, size());
- }
- //找链表中指向指定位置的指针
- Node*& getptr(int pos){
- if(pos<0 || pos>size()) pos = 0;
- if(pos==0) return head;
- Node* p = head;
- for(int i=1; i<pos; i++)
- p = p->next;
- return p->next;
- }
- //前插
- void push_front(const T& d){
- insert(d, 0);
- }
- //遍历
- void travel()const{
- Node* p = head;
- while(p!=NULL){
- cout << p->data << ' ';
- p = p->next;
- }
- cout << endl;
- }
- //清空
- void clear(){
- while(head!=NULL){
- Node * p = head->next;
- delete head;
- head = p;
- }
- len = 0;
- }
- ~List(){
- clear();
- }
- //按照位置删除
- //1、找到链表中指向那个位置的指针
- //2、把那个指针另存一份
- //3、让那个指针指向下一个节点
- //4、释放那个指针的动态内存
- void erase(int pos){
- if(pos<0 || pos>=size()) return;
- Node *& pn = getptr(pos);
- Node * p = pn;
- pn = pn->next;
- delete p;
- --len;
- }
- //根据元素查找位置
- int find(const T& d)const{
- int pos = 0;
- Node* p = head;
- while(p){
- if(p->data==d) return pos;
- p = p->next;
- ++pos;
- }
- return -1;
- }
- //根据元素删除
- void remove(const T& d){
- int pos;
- while((pos = find(d)) != -1)
- erase(pos);
- }
- };
- int main(){
- List l;
- l.push_front(5);
- l.push_front(8);
- l.push_front(20);
- //在第2个位置插入9
- l.insert(9, 2);
- l.travel();
- return 0;
- }
通过上图可以看出来,如果我们要插入一个节点,就需要找到指向该位置的指针(或者前一个结点),比如上图的p->next指针就是我们需要找到的。删除一个节点也一样,需要找到指向该节点的指针。
01--数据结构——动态链表(C++)的更多相关文章
- 数据结构——动态链表(C++)
定义一个节点: [cpp] view plain copy print? #include <iostream> using namespace std; typedef int T; ...
- HDU 2095 find your present (2) 动态链表
解题报告:输入一个n,后面紧跟着输入n个数,输入的这n个数中,除了有一个数的个数为奇数外,其它的数的个数都是偶数个,现在要你找出这个个数为奇数的这个数. 看起来好像很简单的样子,不过,这题的重点不在这 ...
- C++ 数据结构模板 队列 栈 动态链表 模板 Queue Stack List
C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();...... (由于动态链表用的不多,若有错误望各位大神不吝赐教:) 队列: cl ...
- vc++基础班[28]---动态数组及动态链表的讲解
C++中也有相应的动态数组.动态链表.映射表的模板类,就是STL中的:vector.list.map 他们属于C++标准中的一部分,对于程序的移植性来说也是不错的,但是在MFC编程中使用 CArray ...
- Linux C 数据结构 ->单向链表<-(~千金散尽还复来~)
之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...
- Linux C 数据结构 ->单向链表
之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...
- 学习javascript数据结构(二)——链表
前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...
- 使用C语言描述静态链表和动态链表
静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...
- linux内核数据结构之链表
linux内核数据结构之链表 1.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该 ...
随机推荐
- Ajax传递的参数如何在浏览器中查看
如图当需要在浏览器中知道Ajax传递的参数可以,点击浏览器的右键检查,点击XHR,此时要记得提交带有参数的Ajax页面, 这样才可以显示出来传递的参数
- [Ynoi2016]镜中的昆虫
题目大意: 给定一个序列,有2个操作: 1. 区间覆盖.2. 区间数颜色. 解题思路: 珂朵莉树+树套树. 看到区间覆盖当然想到珂朵莉树然而这是Ynoi 所以我们得优化掉珂朵莉树那个暴力过程. 考虑对 ...
- mysql 7 种 join
一. select * from A inner join B on A.key = B.key 二. select * from A left join B on A.key = B.key 三. ...
- UVALive Archive - 6196 - Party Games
题目: You've been invited to a party. The host wants to divide the guests into 2 teams for party games ...
- 如何实现网卡bond
https://jingyan.baidu.com/article/375c8e19da666325f2a229f7.html
- 【ACM】hdu_1096_A+BVIII_201307261748
A+B for Input-Output Practice (VIII)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- Bi-shoe and Phi-shoe 欧拉函数 素数
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...
- Maven使用package打包Spring Boot时出现:Unable to find a single main class from the following candidates的问题解决
问题如下: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE ...
- 【待解决】maven创建web报Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins
Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:maven-war-plugin:maven- ...
- Setting up a EDK II build environment on Windows and Linux:搭建Windows和Linux开发环境[2.2]
Setting up a EDK II build environment on Windows and Linux:搭建Windows和Linux开发环境[2.2] 2015-07 北京海淀区 ...
