zoukankan      html  css  js  c++  java
  • 双向链表

    双向链表

    支持删除、插入

    支持向一个元素之后和之前插入一个元素

    支持在第i个元素之后插入一个元素

    支持删除一个元素

    支持删除第i个元素

    例题

    Introduction

    Well, this time we will be a little difficult for you to do. We are going to design a link list class in c++. Again, the knowledge of pointer in c++ in widely used this time and you have to focus when you are coding. List is not easy for you.

    Knowledge

    This time, you are going to finish a advanced list which is known as doubly linked list. Each node of the list have three members: data which is use as the real storage, next pointer which is used to linked the next node of the list, prev pointer which is use to linked the previous node.
    Requirements
    Finish the member functions below.
    Notice that for output functions, the format is as below:
    toString() [1,2,3,4,5]
    NULL<-1<->2<->3<->4<->5->NULL
    toString() []
    NULL
    toString() [1]
    NULL<-1->NULL
    No new line is needed;
    void split(int position, list* des1, list* dest2) 2 [1,2,3,4]
    [1,2] [3,4]
    list& merge(const list& src1, const list& src2) [1,2,3,4] [5,6,7,8]
    [1,2,3,4,5,6,7,8]
    list& remove_if(bool (*condition)(listPointer)); [1,2,3,4,5,6] condition = odd number
    [2,4,6]
    list& unique(void) [1,2,2,3,4,4,5,6]
    [1,2,3,4,5,6]
    list& reverse(void) [1,2,3,4,5,6]
    [6,5,4,3,2,1]

    版本1.3

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #define inf 0x7fffffff
    using namespace std;
    
    class List{
        public:
        struct Listnode{
            int num;
            Listnode* pre;
            Listnode* nxt;
        };
        int len;
        Listnode* head;
        Listnode* tail;
        Listnode* newnode(int x){
            Listnode* newNode=(Listnode*)malloc(sizeof(Listnode));
            newNode->pre=NULL;
            newNode->nxt=NULL;
            newNode->num=x?x:inf;
            return newNode;
        }
        List(){
            head=newnode(0);
            tail=newnode(0);
            head->nxt=tail;
            tail->pre=head;
        }
        bool Insert(Listnode* root,int num){
            if(root==tail)return false;
            if(root==NULL)return false;
            Listnode* in=newnode(num);
            Listnode* tmp=root->nxt;
            in->nxt=tmp;in->pre=root;
            root->nxt=in;tmp->pre=in;
            len++;
            return true;
        }
        bool Insert(int num,Listnode* root){
            if(root==head)return false;
            if(root==NULL)return false;
            Listnode* in=newnode(num);
            Listnode* tmp=root->pre;
            in->pre=tmp;in->nxt=root;
            root->pre=in;tmp->nxt=in;
        }
        bool Insert(int pos,int num){
            Listnode* now=head;
            for(int i=0;i<pos;++i){
                if(now->nxt==NULL)
                    return false;
                now=now->nxt;
            }
            return Insert(now,num);
        }
    
        void print(){
            Listnode* now=head;
            cout<<"[";
            while(now->nxt!=NULL){
                if(now->nxt==tail)break;
                if(now->nxt)
                    cout<<now->nxt->num<<"->";
                now=now->nxt;
            }
            cout<<"]";
        }
        void Delete(Listnode* elem){
            elem->pre->nxt=elem->nxt;
            elem->nxt->pre=elem->pre;
            if(elem==head)head=elem->nxt;
            if(elem==tail)tail=elem->pre;
            free(elem);
        }
        bool Delete(int pos){
            Listnode* elem=head;
            for(int i=0;i<pos;++i){
                if(elem->nxt==NULL)
                    return false;
                elem=elem->nxt;
            }
            Delete(elem);
        }
    }newList;
    
    

    版本1.2

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #define inf 0x7fffffff
    using namespace std;
    
    class List{
        private:
        struct Listnode{
            int num;
            Listnode* pre;
            Listnode* nxt;
        };
        int len;
        Listnode* head;
        Listnode* tail;
        Listnode* newnode(int x){
            Listnode* newNode=(Listnode*)malloc(sizeof(Listnode));
            newNode->pre=NULL;
            newNode->nxt=NULL;
            newNode->num=x?x:inf;
            return newNode;
        }
        public:
        List(){
            head=newnode(0);
            tail=newnode(0);
            head->nxt=tail;
            tail->pre=head;
        }
        bool Insert(int pos,int num){
            Listnode* now=head;
            for(int i=0;i<pos;++i){
                if(now->nxt==NULL)
                    return false;
                now=now->nxt;
            }
            if(now==tail)return false;
            if(now==NULL)return false;
            Listnode* in=newnode(num);
            Listnode* tmp=now->nxt;
            in->nxt=tmp;in->pre=now;
            now->nxt=in;tmp->pre=in;
            len++;
            return true;
        }
        bool Insert(Listnode* root,int num){
            if(root==tail)return false;
            if(root==NULL)return false;
            Listnode* in=newnode(num);
            Listnode* tmp=root->nxt;
            in->nxt=tmp;in->pre=root;
            root->nxt=in;tmp->pre=in;
            len++;
            return true;
        }
        bool Insert(int num,Listnode* root){
            if(root==head)return false;
            if(root==NULL)return false;
            Listnode* in=newnode(num);
            Listnode* tmp=root->pre;
            in->pre=tmp;in->nxt=root;
            root->pre=in;tmp->nxt=in;
        }
        void print(){
            Listnode* now=head;
            cout<<"[";
            while(now->nxt!=NULL){
                if(now->nxt==tail)break;
                if(now->nxt)
                    cout<<now->nxt->num<<"->";
                now=now->nxt;
            }
            cout<<"]";
        }
        void Delete(Listnode* elem){
            elem->pre->nxt=elem->nxt;
            elem->nxt->pre=elem->pre;
            if(elem==head)head=elem->nxt;
            if(elem==tail)tail=elem->pre;
            free(elem);
        }
        void Delete(int pos){
            Listnode* elem=head;
            for(int i=0;i<pos;++i){
                if(elem->nxt==NULL)
                    return false;
                elem=elem->nxt;
            }
            elem->pre->nxt=elem->nxt;
            elem->nxt->pre=elem->pre;
            if(elem==head)head=elem->nxt;
            if(elem==tail)tail=elem->pre;
            free(elem);
        }
    }newList;
    

    版本1.1

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #define inf 0x7fffffff
    using namespace std;
    
    struct Listnode{
        int num;
        Listnode* pre;
        Listnode* nxt;
    };
    
    struct List{
        Listnode* head;
        Listnode* tail;
        Listnode* newnode(int x){
            Listnode* newNode=(Listnode*)malloc(sizeof(Listnode));
            newNode->pre=NULL;
            newNode->nxt=NULL;
            newNode->num=x?x:inf;
            return newNode;
        }
        List(){
            head=newnode(0);
            tail=newnode(0);
            head->nxt=tail;
            tail->pre=head;
        }
        bool Insert(Listnode* root,int num){
            Listnode* in=newnode(num);
            Listnode* tmp=root->nxt;
            in->nxt=tmp;
            in->pre=root;
            root->nxt=in;
            tmp->pre=in;
            return true;
        }
        void print(){
            Listnode* now=head;
            cout<<"[";
            while(now->nxt!=NULL){
                if(now->nxt==tail)break;
                if(now->nxt)
                    cout<<now->nxt->num<<"->";
                now=now->nxt;
            }
            cout<<"]";
        }
        void Delete(Listnode* elem){
            elem->pre->nxt=elem->nxt;
            elem->nxt->pre=elem->pre;
            if(elem==head)head=elem->nxt;
            if(elem==tail)tail=elem->pre;
            free(elem);
        }
        
    }newList;
    
  • 相关阅读:
    2017微软骇客马拉松精彩大回Fun:不一样的Hacker,一Young的Cool
    老板这种生物:只看结果,不问过程
    小目标 | 分解任务,聪明人只设达得到的“小目标”
    本号讯 | 微软被 Forrester 评为销售服务自动化解决方案领导者
    IT圈网红,抢鲜围观
    云时代“非诚勿扰”
    安装conda后去除终端出现的(base)字样
    Ubuntu18.04 安装 Anaconda3
    高斯模糊
    准确率(Accuracy) 精确率(Precision) 与 召回率(Recall)
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/8097398.html
Copyright © 2011-2022 走看看