From 3ccb9745e30c09b0ce3263ebdb688a1d7542a47b Mon Sep 17 00:00:00 2001 From: QQ <2312369582@qq.com> Date: Tue, 8 Dec 2020 20:55:13 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\275\234\344\270\232/HashTable.java" | 48 +++++++++++++++++++++++ "\344\275\234\344\270\232/Main.java" | 13 ++++++ "\344\275\234\344\270\232/Stuednt.java" | 18 +++++++++ 3 files changed, 79 insertions(+) create mode 100644 "\344\275\234\344\270\232/HashTable.java" create mode 100644 "\344\275\234\344\270\232/Main.java" create mode 100644 "\344\275\234\344\270\232/Stuednt.java" diff --git "a/\344\275\234\344\270\232/HashTable.java" "b/\344\275\234\344\270\232/HashTable.java" new file mode 100644 index 0000000..c2de89e --- /dev/null +++ "b/\344\275\234\344\270\232/HashTable.java" @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Iterator; +public class HashTable { + private final int DEFAULT_TABLE_SIZE = 17; + private ArrayList> array = null; + HashTable(){ + array = new ArrayList>(DEFAULT_TABLE_SIZE); + for (int i = 0; i < DEFAULT_TABLE_SIZE; i++) { + array.add(null); + } + } + public int hash(int id){ + return id % DEFAULT_TABLE_SIZE; + } + public void put(Student stu){ + // 1. 将id hash到[0-16]之间 + int hashValue = hash(stu.id); + // 2. 将stu放入array中hashValue对应的链表 + array.get(hashValue).addFirst(stu); + } + public Student get(int id){ + int hashValue=hash(id); + LinkedList student=array.get(hashValue); + for(int i=0;istudents = array.get(hashValue); + for(int i = 0; i < students.size();i ++){ + if(students.get(i).id == id){ + students.remove(i); + break; + } + } + } +} + + + \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Main.java" "b/\344\275\234\344\270\232/Main.java" new file mode 100644 index 0000000..2bd92e7 --- /dev/null +++ "b/\344\275\234\344\270\232/Main.java" @@ -0,0 +1,13 @@ +class Main { + public static void main(String[] args){ + HashTable ht = new HashTable(); + Student st1 = new Student(60, "Albert", "1996.10.01", "Male"); + + ht.put(st1); + System.out.println(ht.get(60)); + + Student st3= new Student(20, "abc", "1990.01.01", "Male"); + ht.put(st2); + System.out.println(ht.get(20)); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Stuednt.java" "b/\344\275\234\344\270\232/Stuednt.java" new file mode 100644 index 0000000..7c61fa3 --- /dev/null +++ "b/\344\275\234\344\270\232/Stuednt.java" @@ -0,0 +1,18 @@ +public class Stuednt{ + public int id; + public String name; + public String birthday; + public String sex; + + Student(int id, String name, String birthday, String sex){ + this.id = id; + this.name = name; + this.birthday = birthday; + this.sex = sex; + } + + public String toString(){ + return Integer.toSring(id) + " " + name + " " + birthday + " " + sex; + } +} +} \ No newline at end of file -- Gitee From 74a0818c2dbe50190f5738de849de42a353e5b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E8=A1=8D=E9=91=AB?= <8237970+lin-yanxin@user.noreply.gitee.com> Date: Tue, 15 Dec 2020 19:50:22 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\275\234\344\270\232/HashTable.java" | 48 ----------------------- "\344\275\234\344\270\232/Main.java" | 13 ------ "\344\275\234\344\270\232/Stuednt.java" | 18 --------- 3 files changed, 79 deletions(-) delete mode 100644 "\344\275\234\344\270\232/HashTable.java" delete mode 100644 "\344\275\234\344\270\232/Main.java" delete mode 100644 "\344\275\234\344\270\232/Stuednt.java" diff --git "a/\344\275\234\344\270\232/HashTable.java" "b/\344\275\234\344\270\232/HashTable.java" deleted file mode 100644 index c2de89e..0000000 --- "a/\344\275\234\344\270\232/HashTable.java" +++ /dev/null @@ -1,48 +0,0 @@ -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.Iterator; -public class HashTable { - private final int DEFAULT_TABLE_SIZE = 17; - private ArrayList> array = null; - HashTable(){ - array = new ArrayList>(DEFAULT_TABLE_SIZE); - for (int i = 0; i < DEFAULT_TABLE_SIZE; i++) { - array.add(null); - } - } - public int hash(int id){ - return id % DEFAULT_TABLE_SIZE; - } - public void put(Student stu){ - // 1. 将id hash到[0-16]之间 - int hashValue = hash(stu.id); - // 2. 将stu放入array中hashValue对应的链表 - array.get(hashValue).addFirst(stu); - } - public Student get(int id){ - int hashValue=hash(id); - LinkedList student=array.get(hashValue); - for(int i=0;istudents = array.get(hashValue); - for(int i = 0; i < students.size();i ++){ - if(students.get(i).id == id){ - students.remove(i); - break; - } - } - } -} - - - \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Main.java" "b/\344\275\234\344\270\232/Main.java" deleted file mode 100644 index 2bd92e7..0000000 --- "a/\344\275\234\344\270\232/Main.java" +++ /dev/null @@ -1,13 +0,0 @@ -class Main { - public static void main(String[] args){ - HashTable ht = new HashTable(); - Student st1 = new Student(60, "Albert", "1996.10.01", "Male"); - - ht.put(st1); - System.out.println(ht.get(60)); - - Student st3= new Student(20, "abc", "1990.01.01", "Male"); - ht.put(st2); - System.out.println(ht.get(20)); - } -} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Stuednt.java" "b/\344\275\234\344\270\232/Stuednt.java" deleted file mode 100644 index 7c61fa3..0000000 --- "a/\344\275\234\344\270\232/Stuednt.java" +++ /dev/null @@ -1,18 +0,0 @@ -public class Stuednt{ - public int id; - public String name; - public String birthday; - public String sex; - - Student(int id, String name, String birthday, String sex){ - this.id = id; - this.name = name; - this.birthday = birthday; - this.sex = sex; - } - - public String toString(){ - return Integer.toSring(id) + " " + name + " " + birthday + " " + sex; - } -} -} \ No newline at end of file -- Gitee From 67a808cf468766f7cfb293f7874e7c7f5e09a549 Mon Sep 17 00:00:00 2001 From: QQ <2312369582@qq.com> Date: Tue, 15 Dec 2020 19:55:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\275\234\344\270\232/Heap.java" | 84 ++++++++++++++++++++++++++++ "\344\275\234\344\270\232/Main.java" | 18 ++++++ 2 files changed, 102 insertions(+) create mode 100644 "\344\275\234\344\270\232/Heap.java" create mode 100644 "\344\275\234\344\270\232/Main.java" diff --git "a/\344\275\234\344\270\232/Heap.java" "b/\344\275\234\344\270\232/Heap.java" new file mode 100644 index 0000000..2b1c4b1 --- /dev/null +++ "b/\344\275\234\344\270\232/Heap.java" @@ -0,0 +1,84 @@ +public class Heap>{ + public T[] heap; + public int SIZE; + public int count; + + Heap(int size){ + SIZE=size; + heap=(T[])new Comparable[SIZE]; + count=0; + } + public void add (T item){ + if(count>=SIZE){ + System.out.println("Heap Full"); + return; + } + heap[count]=item; + percloateUp(count); + count++; + } + private void percloateUp(int count){ + while(count>0){ + int j=(count-1)/2; + int arr=heap[count].compareTo(heap[j]); + if(arr<0){ + break; + } + swap(count,j); + count=j; + } + } + private void swap(int i,int j){ + T temp=heap[i]; + heap[i]=heap[j]; + heap[j]=heap[i]; + } + public void print(){ + for (T i:heap){ + System.out.print(i+"\t"); + } + } + public T delete(){ + T tempmax=heap[0]; + heap[0]=heap[--count]; + percolateDown(count,0); + heap[count]=null; + return tempmax; + } + private int percolateDown(int count,int i){ + int j; + while(i != (j = ProperParent(count, i))){ + swap(i,j); + i=j; + } + return i; + } +private int ProperParent(int count, int i) { + return RChildValid(count, i) ? Bigger(Bigger(i, i * 2 + 1), i * 2 + 2) : + LChildValid(count, i) ? Bigger(i, i * 2 + 1) : i; +} + +public boolean RChildValid(int n, int i){ + if(i * 2 + 2 < n){ + return true; + } + return false; +} + +public boolean LChildValid(int n, int i){ + if(i * 2 + 1 < n){ + return true; + } + return false; +} + +public int Bigger(int i, int j){ + + int arr=heap[i].compareTo(heap[j]); + if(j < count){ + return arr>0 ? i : j; + } + return i; + } +} + diff --git "a/\344\275\234\344\270\232/Main.java" "b/\344\275\234\344\270\232/Main.java" new file mode 100644 index 0000000..535d6ea --- /dev/null +++ "b/\344\275\234\344\270\232/Main.java" @@ -0,0 +1,18 @@ +class Main { + public static void main(String[] args){ + Heap heap = new Heap(10); + heap.add(0); + + heap.add(2); + heap.add(9); + heap.add(4); + heap.add(3); + heap.add(10); + heap.add(6); + heap.add(5); + heap.print(); + + heap.delete(); + heap.print(); + } +} \ No newline at end of file -- Gitee