算法导论10.2链表
带哨兵的双向链表,代码中我使用了nullptr,所以需要编译器升级,我的编译器是gcc/g++ 4.7.0这是可以的,编译的时候加参数—std=c++0x
节点中还可能有卫星元素
/* * 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; }