zoukankan      html  css  js  c++  java
  • 算法导论10.2链表

    带哨兵的双向链表,代码中我使用了nullptr,所以需要编译器升级,我的编译器是gcc/g++ 4.7.0这是可以的,编译的时候加参数—std=c++0x

    HZO7JI9YXL{}%_%@%XPX~1C

    节点中还可能有卫星元素

    M5(T}CBVX296K4AL06Z%DOK

    _4Z~{N1`PLGR(TBC27WQ{XO

    J[]UU0XED02CVAER[32MUCG

    INCPZKW[]][XRS[{V2K$7)7

    XYS~TA{A~EDU1(M3`LB`O66

    $[IX4731Y6SFLCNMVK]4ZMC

    `6HP~9Q0NC44BF}$O2J]`S9

     
    /*
     * IA_10.2LinkedLists.h
     *
     *  Created on: Feb 13, 2015
     *      Author: sunyj
     */
    
    #ifndef IA_10_2LINKEDLISTS_H_
    #define IA_10_2LINKEDLISTS_H_
    
    #include <iostream>
    
    // T is void* is a good idea for some application
    // key is a variable of type "Type"
    // data is a variable of Type "T"
    template <class Type, class T> class Node {
    public:
    	/*friend bool operator< <T>(const Node<T>&, const Node<T>&);
    	friend bool operator== <T>(const Node<T>& lhs, const Node<T>& rhs);*/
    	Node() : key(0), prev(nullptr), next(nullptr) { }
    	Node(Type k) : key(k), prev(nullptr), next(nullptr) { }
    	Node(Type const k, T const d) : key(k), data(d), prev(nullptr), next(nullptr) { }
    
        Type key;
        T data;
        Node* prev;
        Node* next;
    };
    
    // LIST-SEARCH(L, k)
    // x = L.nil.next
    // while ( x != L.nil and x.key != k)
    //     x = x.next
    // return x
    
    // LIST-INSERT(L, x)
    // x.next = L.nil.next
    // L.nil.next.prev = x
    // L.nil.next = x
    // x.prev = L.nil
    
    // LIST-DELETE(L, x)
    // x.prev.next = x.next
    // x.next.prev = x.prev
    
    template <class Type, class T> class LinkedList {
    public:
        LinkedList() : nil(&m_nil)
        {
            nil->prev = nil;
            nil->next = nil;
        }
        Node<Type, T>* search(Type const k) // find node by key
        {
        	Node<Type, T>* x = nil->next;
            while (x != nil && k != x->key)
            {
                x = x->next;
            }
            if (nil == x)
            {
            	return nullptr;
            }
            return x;
        }
        // insert the address of the node, at the head of the list
        void insert(Node<Type, T>* x)
        {
            x->next         = nil->next;
            nil->next->prev = x;
            nil->next       = x;
            x->prev         = nil;
        }
        void del(Node<Type, T>* x)
        {
            x->prev->next = x->next;
            x->next->prev = x->prev;
        }
        Node<Type, T>* GetNil()
        {
            return nil;
        }
        void print()
        {
            Node<Type, T>* x = nil->next;
            while (nil != x)
            {
                std::cout << x->key << " ";
                x = x->next;
            }
            std::cout << std::endl;
        }
    private:
        Node<Type, T>  m_nil; // empty list has one node, pointer nil points to it.
        Node<Type, T>* nil;
    };
    
    
    #endif /* IA_10_2LINKEDLISTS_H_ */
    
    /*
     * IA_10.2LinkedLists.cpp
     *
     *  Created on: Feb 12, 2015
     *      Author: sunyj
     */
    #include "IA_10.2LinkedLists.h"
    
    int main()
    {
    	// first int64_t means the class type of key is int64_t
    	// second int64_t means the class type of data stored in node is int64_t
        LinkedList<int64_t, int64_t> list;
        Node<int64_t, int64_t> node1(1, 100);
        Node<int64_t, int64_t> node4(4, 400);
        Node<int64_t, int64_t> node16(16, 1600);
        Node<int64_t, int64_t> node9(9, 900);
        list.insert(&node1);
        list.insert(&node4);
        list.insert(&node16);
        list.insert(&node9);
        list.print();
        Node<int64_t, int64_t> node25(25, 2500);
        list.insert(&node25);
        list.print();
        list.del(&node1);
        list.print();
        Node<int64_t, int64_t>* tmp;
        tmp = list.search(9);
        list.del(tmp);
        list.print();
    
    
        return 0;
    }
    
     

    69b312cbccec57d980f3f064bfa7b70072fe9f657c78ccb4eb3dc83dd10fbbb476b660865d654fa423e48d414cfd8692

    80bbcf13-5d0f-45f3-b8d1-660f86c5a1c8_size79_w640_h64085c3b8047f0d811f37ca4e0e75e99f3b0088c809-5efa-4fc8-89db-a510382d9d08_size87_w640_h64091cab761fc926f6252f8188058e3d06891ef76c6a7efce1be75fec45ad51f3deb58f65cc96dda144ad3459826fc75f930ef431adcbef846697e615b7610e4f86f4923b697e28bec3

  • 相关阅读:
    【转】ServletContext介绍及用法
    【转】UML之类图和对象图
    【转】UML各种图总结
    解决win10下 matplotlib绘图时中文乱码问题
    修改表、字段的默认字符集
    MySQL报错Incorrect date value: '0000-00-00' for column 'hirrdate' at row 1
    用vs code将qt designer的.ui文件转换为.py文件
    MySQL多表数据查询记录
    MySQL中统计函数和分组数据查询
    lambda匿名函数
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4287478.html
Copyright © 2011-2022 走看看