双向链表

Posted Grary

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双向链表相关的知识,希望对你有一定的参考价值。

双向链表

支持删除、插入

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

支持在第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;

以上是关于双向链表的主要内容,如果未能解决你的问题,请参考以下文章

带头节点的双向链表

《链表》之带头双向循环链表

7L-双线链表实现

代码模板实现双向链表的去重拼接合并排序

数据结构之带头结点的循环双向链表详细图片+文字讲解

双向链表 - 是啥导致我的代码引发编译器错误,我该如何解决?