zoukankan      html  css  js  c++  java
  • HashMap

    Node

    package DataStructures.HashMap.Hashing;
    
    class Node {
        int data;
        Node next;
    
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    LinkedList

    package DataStructures.HashMap.Hashing;
    
    class LinkedList {
    
        private Node Head;
        private int size;
    
        public LinkedList() {
            Head = null;
            size = 0;
        }
    
        public void insert(int data) {
    
            Node temp = Head;
            Node newnode = new Node(data);
        
            size++;
    
            if(Head == null) {
                Head = newnode;
            }
            else {
                newnode.next = Head;
                Head = newnode;
            }
        }
    
        public void delete(int data) {
            if(size == 0) {
                System.out.println("UnderFlow!");
                return;
            }
    
            else {
                Node curr = Head;
                if (curr.data == data) {
                    Head = curr.next;
                    size--;
                    return;
                }
                else {
                    
                    while(curr.next.next != null) {
                        if(curr.next.data == data){
                                curr.next = curr.next.next;
                                return;
                            }
                    }
    
                    System.out.println("Key not Found");
                }
            }
        }
    
        public void display() {
            Node temp = Head;
            while(temp != null) {
                System.out.printf("%d ",temp.data);
                temp = temp.next;
            }
            System.out.println();
        }
    }

    HashMap

    package DataStructures.HashMap.Hashing;
    
    
    class HashMap {
        private int hsize;
        private LinkedList[] buckets;
    
        public HashMap(int hsize) {
            buckets = new LinkedList[hsize];
            for (int i = 0; i < hsize; i++) {
                buckets[i] = new LinkedList();
                // Java requires explicit initialisaton of each object
            }
            this.hsize = hsize;
        }
    
        public int hashing(int key) {
            int hash = key % hsize;
            if (hash < 0)
                hash += hsize;
            return hash;
        }
    
        public void insertHash(int key) {
            int hash = hashing(key);
            buckets[hash].insert(key);
        }
    
    
        public void deleteHash(int key) {
            int hash = hashing(key);
    
            buckets[hash].delete(key);
        }
    
        public void displayHashtable() {
            for (int i = 0; i < hsize; i++) {
                System.out.printf("Bucket %d :", i);
                buckets[i].display();
            }
        }
    
    }
    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    软件开发面试
    jQuery插件
    基于消息的软件架构
    线程池的原理及实现(转)
    java实现生产者消费者问题(转)
    并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法(转)
    JAVA CAS原理深度分析(转)
    菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)
    Android中利用Handler实现消息的分发机制(三)
    char* 和char[]的差别
  • 原文地址:https://www.cnblogs.com/CherryTab/p/11997740.html
Copyright © 2011-2022 走看看