简单 双向链表得增删改查 c++描述

硅谷探秘者 2160 0 0

简单 双向链表得增删改查 c++描述

class node{
public :
    int data;
    node * next;
    node * prev;
};
#include"node.h"
class relink{
private :
    node * head;//首节点
    node * tail;//尾节点

public:
    relink(){
        head=new node();//初始化,不做数据得处理
        tail=new node();
        head->next=tail;
        head->prev=NULL;
        tail->next=NULL;
        tail->prev=head;
    }
    void add(int data);//增
    void del(int data);//删
    void edit(int olddatda,int newdata);//;改
    node * getnode(int data);//查
    void print();//循序打印
};
#include<iostream>
#include "relink.h"
using namespace std;
void relink::add(int data){
    node * end=tail;
    node * p=tail->prev;
    node * n =new node();
    n->data=data;
    n->prev=p;
    n->next=end;
    p->next=n;
    end->prev=n;
}
void relink::print(){
    node * n=head;
    while(n->next->next!=NULL){
        cout<<n->next->data<<endl;
        n=n->next;
    }
}
void relink::edit(int olddata,int newdata){
    node * n=head;
    while(n->next->next!=NULL){
        if(n->next->data==olddata){
            n->next->data=newdata;
            return;
        }
        n=n->next;
    }
}
void relink::del(int data){
    node * n=head->next;
    while(n->next!=NULL){
        if(n->data==data){
            node * pr  = n->prev;
            node * old = n->next;
            pr->next=old;
            old->prev=pr;
            return;
        }
        n=n->next;
    }
}
node * relink::getnode(int data){
    node * n=head;
    while(n->next->next!=NULL){
        if(n->next->data==data){
            return n->next;
        }
        n=n->next;
    }
    return NULL;
}
int main(){
    relink * rl=new relink();
    rl->add(0);
    rl->add(1);
    rl->add(2);
    rl->edit(2,23);
    rl->del(23);
    rl->print();
    node * n=rl->getnode(1);
    if(n!=NULL){
        cout<<"get:"<<n->data<<endl;
    }
    return 1;
}

QQ截图20181129192230.png



评论区
请写下您的评论...
暂无评论...
猜你喜欢
数据结构与算法 2707 c++classnode{public:intdata;node*next;};#include"node.h"usingnamespacestd;classstack
数据结构与算法 2971 式栈的出栈入栈操作c++基于//节点classnode{public:intdata;node*next;node*prev;};//#include"node.h
框架 2211 创建一个Maven项目,2.修jdk版本(因为这里使用的springboot是1.5,在2.0一下springboot推荐使用1.7)!--修jdk版本springboot2.0之前推荐使用
weblog 2128 因为需求需要,所以直接写一个数据结构 直接上代码: usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceConsoleApplication2 { classProgram { staticvoidMai
official 831 leetcode第237题()原接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/submissions/问题
数据结构与算法 4351 递归实现合并两递-合并后保持递序列java数据结构:算法:递归节点packageclub.test;/****节点*@authorjiajia
java虚拟机(jvm) 5681 。一个类的内部名就是这个类的完全限定名,其中的点号用斜线代替。例如,String的内部名为java/lang/String。2.类型符内部名只能用于类或接口类型。所有其他Java类型,比如字段类型,
official 853 ,等待客户端发送消息。先看一个bio最的例子:publicstaticvoidmain1(String[]args)throwsIOException{ //绑定端口 ServerSocketserv
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。