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;
    
  • 相关阅读:
    react方法传参的两种方式
    react引入本地图片和远程图片
    用yarn代替npm
    react生命周期
    react子传父
    react删除元素
    react遍历数组
    react监听input框里的值
    react创建组件的两种方式
    vue打包更改dist文件夹名称
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/8097398.html
Copyright © 2011-2022 走看看