• 数据结构实现


    package kunpu.arithmetic.datatype;
    
    import java.util.Iterator;
    
    /**
     * @author zhen
     * @Date 2018/4/11 10:12
     */
    public class Bag<Item> implements Iterable<Item> {
        private Node first;
        private class Node{
            Item item;
            Node next;
        }
    
        public void add(Item item){
            Node oldFirst = first;
            first = new Node();
            first.item = item;
            first.next = oldFirst;
        }
        @Override
        public Iterator<Item> iterator() {
            return null;
        }
    
        private class ListIterator<Item> implements Iterator<Item>{
            private Bag.Node current = first;
    
            @Override
            public boolean hasNext() {
                return current != null;
            }
    
            @Override
            public Item next() {
                Item item = (Item)current.item;
                current = current.next;
                return item;
            }
        }
    }
    
    
    package kunpu.arithmetic.datatype;
    
    import java.util.Iterator;
    
    /**
     * 下压堆栈(链表实现)
     *
     * @author zhen
     * @Date 2018/4/11 9:51
     */
    public class Stack<Item> implements Iterable<Item> {
    
        private Node first;
        private int N;
        public int size(){
            return N;
        }
        public boolean isEmpty(){
            return first == null;
        }
        public void push(Item item){
            Node oldFirst = first;
            first.item = item;
            first.next = oldFirst;
            N++;
        }
    
        public Item pop(){
            Item item = first.item;
            first = first.next;
            N--;
            return item;
        }
    
    
        public Iterator<Item> iterator() {
            return new ReverseArrayIterator();
        }
    
        private class ReverseArrayIterator implements Iterator<Item>{
    
            public boolean hasNext(){
                return first.next != null;
            }
            public Item next(){
                return first.next.getItem();
            }
            public void remove(){
    
            }
        }
    
    
    
        private class Node{
            Item item;
            Node next;
    
            public Item getItem() {
                return item;
            }
    
            public void setItem(Item item) {
                this.item = item;
            }
    
            public Node getNext() {
                return next;
            }
    
            public void setNext(Node next) {
                this.next = next;
            }
        }
    
    }
    
    
    package kunpu.arithmetic.datatype;
    
    import java.util.Iterator;
    
    /**
     * 下压栈
     *
     * @author zhen
     * @Date 2018/4/11 9:22
     */
    public class ResizingArraysStack<Item> implements Iterable<Item> {
    
        private Item[] a = (Item[]) new Object[1];
        private int N = 0;
        public boolean isEmpty(){
            return N ==0;
        }
        public int size(){
            return N;
        }
    
        private void resize(int max){
            Item[] temp = (Item[]) new Object[max];
            for (int i = 0; i < N; i++){
                temp[i] = a[i];
            }
            a = temp;
        }
    
        public void push(Item item){
            if (N == a.length){
                resize(2*a.length);
            }
            a[N++] = item;
        }
    
        public Item pop(){
            Item item = a[--N];
            a[N] = null;
            if (N >0 && N== a.length/4){
                resize(a.length/2);
            }
            return item;
        }
        @Override
        public Iterator<Item> iterator() {
            return new ReverseArrayIterator();
        }
    
        private class ReverseArrayIterator implements Iterator<Item>{
            private int i = N;
            public boolean hasNext(){
                return i>0;
            }
            public Item next(){
                return a[--i];
            }
            public void remove(){}
        }
    }
    
    
    package kunpu.arithmetic.datatype;
    
    import java.util.Iterator;
    
    /**
     * @author zhen
     * @Date 2018/4/11 10:02
     */
    public class Queue<Item> implements Iterable<Item> {
        private Node first;
        private Node last;
        private int N;
    
        public boolean isEmpty(){
            return first == null;
        }
    
        public int size(){
            return N;
        }
    
        public void enqueue(Item item){
            Node oldLast = last;
            last = new Node();
            last.item = item;
            last.next = null;
            if (isEmpty()){
                first = last;
            }else{
                oldLast.next = last;
            }
            N++;
        }
    
        public Item dequeue(){
            Item item = first.item;
            first = first.next;
            if (isEmpty()){
                last = null;
            }
            N--;
            return item;
        }
    
        @Override
        public Iterator<Item> iterator() {
            return null;
        }
    
        private class Node{
            Item item;
            Node next;
        }
    }
    
    
    package kunpu.arithmetic.datatype;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    /**
     * 使用无序链表实现符号表
     *
     * @author zhen
     * @Date 2018/4/11 16:30
     */
    public class SequentialSearchST<Key, Value> {
        Set<Key> keySet = new HashSet<>();
        int N = 0;
        private class Node {
            Key key;
            Value value;
            Node next;
            Node(Key key, Value value, Node next){
                this.key = key;
                this.value = value;
                this.next = next;
            }
        }
    
        private Node first;
    
        public void put(Key key, Value value){
            for (Node x = first; x != null; x = x.next){
                if (key.equals(x.key)){
                    x.value = value;
                    return;
                }
            }
            first = new Node(key, value, first);
            N++;
            keySet.add(key);
        }
    
        public Value get(Key key){
            for (Node x = first; x != null; x = x.next){
                if (key.equals(x.key)){
                    return x.value;
                }
            }
            return null;
        }
    
        public void delete(Key key){
            for (Node x = first; x != null; x = x.next){
                if (x.next == null){
                    return;
                }
                if (x.next.key.equals(key)){
                    x.next = x.next.next;
                }
            }
            N--;
            keySet.remove(key);
        }
    
        public boolean contains(Key key){
            return get(key) != null;
        }
    
        public boolean isEmpty(){
            return first == null;
        }
    
        public int size(){
            return N;
        }
    
        public Iterable<Key> keys(){
           return new Iterable<Key>() {
               @Override
               public Iterator<Key> iterator() {
                   return keySet.iterator();
               }
           };
        }
    
    
    }
    
    
    
    package kunpu.arithmetic.datatype;
    
    /**
     * 二分查找 基于有序数组
     * @author zhen
     * @Date 2018/4/12 9:26
     */
    public class BinarySearchST<Key extends Comparable<Key>, Value> {
        private Key[] keys;
        private Value[] values;
        private int N;
        public BinarySearchST(int capacity){
            keys = (Key[])new Comparable[capacity];
            values = (Value[])new Object[capacity];
        }
    
        public int size(){
            return N;
        }
    
        public boolean isEmpty(){
            return N == 0;
        }
    
        public Value get(Key key){
            if (isEmpty()){
                return null;
            }
            int i = rank(key);
            if (i < N && keys[i].compareTo(key) == 0){
                return values[i];
            }else{
                return  null;
            }
        }
    
        public void put(Key key, Value value){
            int i = rank(key);
            if ((i < N && keys[i].compareTo(key) == 0)){
                values[i] = value;
                return;
            }
            for (int j = N; j >i; j--){
                keys[j] = keys[j-1];
                values[j] = values[j-1];
                keys[i] = key;
                values[i] = value;
            }
            N++;
        }
    
        public void delete(Key key){
            int i = rank(key);
            if ((i < N && keys[i].compareTo(key) == 0)){
                for (int j = i; j <N-1; j++){
                    keys[j] = keys[j+1];
                    values[j] = values[j+1];
                }
            }
        }
    
        public int rank(Key key){
            int lo = 0, hi = N-1;
            while (lo <= hi){
                int mid = lo + (hi -lo)/2;
                int cmp = key.compareTo(keys[mid]);
                if (cmp < 0){
                    hi = mid -1;
                }else if (cmp > 0){
                    lo = mid + 1;
                }else{
                    return mid;
                }
            }
            return lo;
        }
    
        public Key min(){
            return keys[0];
        }
    
        public Key max(){
            return keys[N-1];
        }
    
        public Key select(int k){
            return keys[k];
        }
    
        public Key ceiling(Key key){
            int i = rank(key);
            return keys[i];
        }
    
        public boolean contains(Key key){
            for (Key key1 : keys){
                if (key1.compareTo(key) == 0){
                    return true;
                }
            }
            return false;
        }
    
        public Iterable<Key> keys(Key lo, Key hi){
            Queue<Key> q = new Queue<Key>();
            for (int i = rank(lo); i < rank(hi); i++){
                q.enqueue(keys[i]);
            }
            if (contains(hi)){
                q.enqueue(keys[rank(hi)]);
            }
            return q;
        }
    }
    
    
    
    package kunpu.arithmetic.datatype;
    
    /**
     * 基于二叉查找树的符号表
     * @author zhen
     * @Date 2018/4/12 14:50
     */
    public class BST<Key extends Comparable<Key>, Value> {
        private Node root;
        private class Node{
            private Key key;
            private Value value;
            private Node left, right;
            private int N; //以该节点为根的子树种的节点总数
    
            public Node(Key key, Value value, int n) {
                this.key = key;
                this.value = value;
                N = n;
            }
        }
    
        public int size(){
            return size(root);
        }
    
        private int size(Node x){
            if (x == null){
                return 0;
            }else{
                return x.N;
            }
        }
    
        private Value get(Key key){
            return get(root, key) ;
        }
    
        private Value get(Node x, Key key){
            if (x == null){
                return null;
            }
            int cmp = key.compareTo(x.key);
            if (cmp < 0){
                return get(x.left, key);
            }else if (cmp > 0){
                return get(x.right, key);
            }else{
                return x.value;
            }
        }
    
        public void put(Key key, Value value){
            put(root, key, value);
        }
    
    
        private Node put(Node node, Key key, Value value){
            if (node == null){
                return new Node(key, value, 1);
            }
            int cmp = node.key.compareTo(key);
            if (cmp < 0){
                node.left = put(node.left, key, value);
            }else if (cmp > 0){
                node.right = put(node.right,  key, value);
            }else {
                node.value = value;
            }
            node.N = size(node.left) + size(node.right) + 1;
            return node;
        }
    
        public Key min(){
            return min(root).key;
        }
    
        private Node min(Node x){
            if (x.left == null) {
                return x;
            }
            return min(x.left);
        }
    
        public Key floor(Key key){
            Node x = floor(root, key);
            if (x == null) {
                return null;
            }
            return x.key;
        }
    
        private Node floor(Node x, Key key){
            if (x == null){
                return null;
            }
            int cmp = x.key.compareTo(key);
            if (cmp == 0){
                return x;
            }else if (cmp < 0){
                return floor(x.left, key);
            }else{
                Node t = floor(x.right, key);
                if (t != null){
                    return t;
                }else{
                    return x;
                }
            }
        }
    
        public Key select(int k){
            return select(root, k).key;
        }
    
        private Node select(Node x, int k){
            if (x == null) {
                return null;
            }
            int t = size(x.left);
            if (t > k){
                return select(x.left, k);
            }else if (t < k){
                return select(x.right, k);
            }else {
                return x;
            }
        }
    
        public int rank(Key key){
            return rank(root, key);
        }
    
        private int rank(Node x, Key key){
            if(x == null){
                return 0;
            }
            int cmp = key.compareTo(x.key);
            if (cmp < 0){
                return rank(x.left, key);
            }else if(cmp > 0){
                return 1 + size(x.left) + rank(x.right, key);
            }else {
                return size(x.left);
            }
        }
    
        /**
         * 删除最小值
         */
        public void deleteMin(){
            root = deleteMin(root);
        }
    
        private Node deleteMin(Node x){
            if (x.left == null){
                return x.right;
            }
            x.left = deleteMin(x.left);
            x.N = size(x.left) + size(x.right) + 1;
            return x;
        }
    
        /**
         * 删除
         * @param key
         */
        public void delete(Key key){
            root = delete(root, key);
        }
    
        private Node delete(Node x, Key key){
            if (x == null){
                return null;
            }
            int cmp = key.compareTo(x.key);
            if (cmp < 0){
                x.left = delete(x.left, key);
            }else if(cmp > 0){
                x.right = delete(x.right, key);
            }else{
                if (x.right == null){
                    return x.left;
                }
                if (x.left == null){
                    return x.right;
                }
                Node t = x;
                x = min(t.right);
                x.right = deleteMin(t.right);
                x.left = t.left;
            }
            x.N = size(x.left) + size(x.right) + 1;
            return x;
        }
        public Key max(){
            return max(root).key;
        }
    
        private Node max(Node x){
            if (x.right == null){
                return x;
            }
            return max(x.right);
        }
    
        public Iterable<Key> keys(){
            return keys(min(),max());
        }
    
        public Iterable<Key> keys(Key lo, Key hi){
            Queue<Key> queue = new Queue<Key>();
            keys(root, queue, lo, hi);
            return queue;
        }
    
        private void keys(Node x, Queue<Key> queue, Key lo, Key hi){
            if (x == null){
                return ;
            }
            int cmpLo = lo.compareTo(x.key);
            int cmpHi = hi.compareTo(x.key);
            if (cmpLo < 0){
                keys(x.left, queue, lo, hi);
            }
            if (cmpLo <= 0 && cmpHi >= 0){
                queue.enqueue(x.key);
            }
            if (cmpHi > 0){
                keys(x.right, queue, lo, hi);
            }
        }
    }
    
    
    
    package kunpu.arithmetic.datatype;
    
    /**
     * 基于堆的优先队列
     * 从大到小
     * 自上而下的完全二叉树结构,数组实现。k,k/2 = parent, 2k = left, 2k+1=right
     *
     * @author zhen
     * @Date 2018/4/10 16:30
     */
    public class MaxPQ<Key  extends Comparable<Key>>{
        private Key[] pq; //基于堆的完全二叉树
        private int N = 0; //存储于pq[1..N]中,pq[0]没有使用
    
        public MaxPQ(int maxN){
            pq = (Key[])new Comparable[maxN + 1];
        }
    
        public boolean isEmpty(){
            return N ==0;
        }
    
        public int size(){
            return N;
        }
    
        public void insert(Key v){
            pq[++N] = v;
            swim(N);
        }
    
        public Key delMax(){
            Key max = pq[1]; //从根节点得到最大元素
            exch(1, N--); //将其和最后一个节点交换
            pq[N+1] = null; //防止越界
            sink(1); //恢复堆的有序性
            return max;
        }
    
        private void swim(int k){//上浮
            while(k > 1 && less(k/2, k)){
                exch(k/2, k);
                k = k/2;
            }
        }
    
        private void sink(int k){//下沉
            while (2*k <= N){
                int j = 2*k;
                if (j < N && less(j, j+1)){
                    j++;
                }
                if (!less(k, j)){
                    break;
                }
                exch(k, j);
                k = j;
            }
        }
    
        private void exch(int a, int b){
            Key temp = pq[a];
            pq[a] = pq[b];
            pq[b] = temp;
        }
    
        private boolean less(int i, int j){
            return pq[i].compareTo(pq[j]) < 0;
        }
    
    
    
    
    }
  • 相关阅读:
    分页内存管理——虚拟地址到物理地址的转换【转】
    设备树(device tree)学习笔记【转】
    08 在设备树里描述platform_device【转】
    [dts]Device Tree机制【转】
    设备树快速入门【转】
    Linux 设备树详解【转】
    设备树处理之——device_node转换成platform_device【转】
    最新内核3.4)Linux 设备树加载I2C client adapter 的流程(内核3.4 高通)【转】
    基于tiny4412的Linux内核移植 --- aliases节点解析【转】
    Linux 文件系统IO性能优化【转】
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/8853765.html
Copyright © 2020-2023  润新知