zoukankan      html  css  js  c++  java
  • 双向链表实现符号表

    双向链表实现符号表

    2019-06-27  16:44:51

    import java.util.Iterator;
    
    /**
     * @ClassName LinkedST
     * @Author wangyudi
     * @Date 2019/6/27 16:13
     * @Version 1.0
     * @Description 无序双向链表实现的符号表
     * 链表类
     * 成员变量:头指针 first、链表大小 count 、匿名内部类结点 Node
     * 成员方法: 获取值get()、插入/更新值put()、大小size()、删除delete()
     *
     * 匿名内部类Node
     * 成员变量:键key、值value、前结点 prev、后结点 next
     */
    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());
    
        }
    }
    
    
    //结果
    11..
    23..
    7..
    3..
    =================
    4
  • 相关阅读:
    比较器
    堆排序
    快速排序
    荷兰国旗问题
    python查漏补缺 --- 基础概念及控制结构
    redis 交集、并集、差集
    spark问题
    IDEA写scala简单操作
    hive 中遇到的正则
    scala模式匹配
  • 原文地址:https://www.cnblogs.com/youzoulalala/p/11098026.html
Copyright © 2011-2022 走看看