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.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该 ...
随机推荐
- 系统和帮助-Linux基础知识
iOS镜像: 硬盘分区:留出一些空间;实在不成,可安装完成以后,新增一块虚拟硬盘; 终端:terminal 用户界面: GUI:图形界面 GNome KDE CLI: bash,zsh,sh,csh, ...
- .net 单元测试
都说测试驱动开发,但是想写好单元测试其实不容易,不是因为测试用例难以构造,而是因为很多时候方法非常复杂 其中部分测试想要完成就十分费力,其中让人崩溃的地方主要如下: 实例私有函数 实例静态私有函数 十 ...
- Spring Boot学习总结(3)——SpringBoot魅力所在
使用Java做Web应用开发已经有近20年的历史了,从最初的Servlet1.0一步步演化到现在如此多的框架,库以及整个生态系统.经过这么长时间的发展,Java作为一个成熟的语言,也演化出了非常成熟的 ...
- HRBUST 1214 方格取数
方格取数 Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged on HRBUST. Original ID: 12 ...
- Nikita and stack
Nikita and stack time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- WinForm 登录窗体 + 单实例运行
关于怎么在winform里增加登录窗体或者如何让winform程序单实例运行网上都很多例子. 然而两者结合起来呢? //Program.cs static class Program { public ...
- ZOJ - 3483 - Gaussian Prime
先上题目: Gaussian Prime Time Limit: 3 Seconds Memory Limit: 65536 KB In number theory, a Gaussian ...
- Spring Boot实例Hello World Demo
Spring Boot要求Maven的版本达到3.2或以上. 实例: POM: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...
- spring boot使用外部tomcat部署
1:pom里面的packaging修改为war(<packaging>war</packaging>) 2:在pom依赖spring-boot-starter-web排除内置的 ...
- HDU 4500
哈哈,好爽好爽,刷水题报复社会啦... 哥这次省赛一定要拿一等奖,让你小看我,让你小看我.... #include <iostream> #include <cstdio> # ...