Doubly linked list to achieve the symbol table

Doubly linked list to achieve the symbol table

2019-06-27  16:44:51

import java.util.Iterator;

/**
 * @ClassName LinkedST
 * @Author wangyudi
 * @Date 2019/6/27 16:13
 * @Version 1.0
 * @Description unordered doubly linked list to achieve the symbol table
 * Objectlist
 * Member variables: the head pointer first, the size of the chain count, the node Node anonymous inner classes
 * Member method: Gets the value get (), insert / update value put (), size size (), delete delete ()
 *
 * Anonymous inner class Node
 * Member variable: key key, the value of value, before node prev, next node after
 */
public class LinkedST<Key extends Comparable<Key>, Value> implements Iterable<Value>{
    private Node first;
    private int count;
    private class Node{
        private Key key;
        private Value value;
        private Node next;
        private Node prev;//为了实现delete()而使用双向链表

        public Node(Key key, Value value, Node next, Node prev) {
            this.key = key;
            this.value = value;
            this.next = next;
            this.prev = prev;
        }
    }
    public int size(){
        return count;
    }

    public Value get(Key key){
        if(count == 0) return null;
        for(Node i = first;i!=null;i=i.next){
            if(i.key==key) return i.value;
        }
        return null;
    }

    public void put(Key key,Value value){
        if(count==0){
            first=new Node(key,value,null,null);
            count++;
            return;
        }

        for(Node i=first;i!=null;i=i.next){
            if(i.key == key) {
                i.value=value;
                return;
            }
        }
        Node temp = first;
        first = new Node(key,value,first,null);
        temp.prev = first;
        count++;
    }

    public void delete(Key key){
        for(Node i=first;i!=null;i=i.next){
            if(i.key == key) {
                i.prev.next = i.next;
                i.next.prev = i.prev;
                count--;
            }
        }
    }

    @Override
    public Iterator<Value> iterator() {
        return new Iterator<Value>() {
            Node i = first;
            @Override
            public boolean hasNext() {
                if(i!=null) return true;
                return false;
            }

            @Override
            public Value next() {
                Value temp = i.value;
                i = i.next;
                return temp;
            }
        };
    }
}

 

/**
 * @ClassName TestCase
 * @Author wangyudi
 * @Date 2019/6/27 16:35
 * @Version 1.0
 * @Description
 */
public class TestCase {
    public static void main(String[] args) {
        LinkedST<Integer, String> st = new LinkedST<>();
        st.put(3,"3..");
        st.put(7,"7..");
        st.put(64,"64..");
        st.put(23,"23..");
        st.put(11,"11..");
        st.delete(64);

        for(String e : st){
            System.out.println(e);
        }
        
        System.out.println("=================");
        System.out.println(st.size());

    }
}


// Results 
11 ..
 23 ..
 7 ..
 3 ..
 =================
4

 

Guess you like

Origin www.cnblogs.com/youzoulalala/p/11098026.html