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

    2017-08-28 20:46:09

    writer:pprp

    双向链表比单链表每个节点还多了一个 pre 可以从当前节点找到上一个节点,操作比较方便,

    但是由于指针较多,容易乱,所以写代码的时候要画图

    代码及解释&测试如下:

    /*
    @theme: 双链表
    @writer:pprp
    @start:19:13
    @end:20:17
    @declare:有两个指针比较好操作,但是也比较乱
    @date:2017/8/28
    */
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    struct node
    {
        int data;
        node * pre;
        node * next;
    };
    
    class LinkList
    {
    private:
        node *head;
        node *tail;
        int len;
    public:
        LinkList();
        //构建节点
        node * makeNode();
        //添加节点到尾端
        bool push(int data);
        //弹出最后一个节点
        int pop();
        //通过index来查找元素
        int FindAt(int index);
        //插入元素到指定位置
        bool Insert(int index, int data);
        //遍历打印
        void display(bool tag);
    };
    
    LinkList :: LinkList()
    {
        head = makeNode();
        tail = head;
        len = 0;
    }
    
    //构建节点
    node * LinkList::makeNode()
    {
        node * newNode = new node();
        return newNode;
    }
    //添加节点到尾端
    //test:ok
    bool LinkList::push(int data)
    {
        //mine
        node * newNode = new node();
        newNode->data = data;
        newNode->pre = tail;
        tail->next = newNode;
        tail = newNode;
        len++;
        return true;
    }
    //弹出最后一个节点
    //test:ok
    int LinkList::pop()
    {
        int ans;
        node * tmp = tail;
        tail = tail->pre;
        ans = tmp->data;
        delete(tmp);
        tail->next = NULL;
        tmp = NULL;
        len--;
        return ans;
    }
    
    //通过index来查找元素
    //test:ok
    int LinkList::FindAt(int index)
    {
        if(index < 1 || index > len)
            return 0;
        int data = 0;
        node * it = head;
        for(int i = 0 ; i < index ; i++)
        {
            it = it->next;
        }
        data = it->data;
        return data;
    }
    
    //插入元素到指定位置
    //test:ok
    bool LinkList::Insert(int index, int data)
    {
        if(index < 1 || index > len)
            return false;
        node *it = head;
        for(int i = 0 ; i < index ; i++)
            it = it ->next;
        node * newNode = new node();
        newNode->data = data;
        it = it->pre;
        newNode->next = it->next;
        it->next = newNode;
        newNode->pre =  it;
        it = newNode->next;
        it->pre = newNode;
        len++;
    
        return true;
    }
    
    //遍历打印,tag = 1 正向遍历,tag = 0 方向遍历
    //test :ok
    void LinkList::display(bool tag)
    {
        if(tag == 1) //--ok
        {
            node * it = head->next;
            while(it)
            {
                cout << it->data <<" ";
                it = it->next;
            }
            cout << endl;
        }
        else  //-- ok
        {
            node * it = tail;
            while(it!=head) //条件修改为head
            {
                cout << it->data << " ";
                it = it->pre;
            }
            cout << endl;
        }
    }
    
    int main()
    {
        LinkList *p = new LinkList();
        for(int i = 0 ; i < 10 ; i++)
        {
    //        srand((int)time(NULL) + i);
    //        p->push(rand()%100);
            p->push(i+1);
        }
    
        p->display(true);
        cout << "-----" << endl;
        p->display(false);
    
        cout << p->pop() << endl;
        
        p->display(true);
        cout << "-----" << endl;
        p->display(false);
        
        cout << p->FindAt(5) << endl;
    
        p->Insert(5,817);
        
        p->display(true);
        cout << "-----" << endl;
        p->display(false);   
    
    
        return 0;
    }
  • 相关阅读:
    Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
    Spring Boot 如何给微信公众号返回消息
    Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
    Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
    Spring Boot 开发微信公众号后台
    Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
    Spring Boot2 系列教程(十六)定时任务的两种实现方式
    Spring Boot2 系列教程(十五)定义系统启动任务的两种方式
    Spring Boot2 系列教程(十四)CORS 解决跨域问题
    JavaScript二维数组
  • 原文地址:https://www.cnblogs.com/pprp/p/7445700.html
Copyright © 2011-2022 走看看