1. 程式人生 > 其它 >js模組化 ES6 引入第三方模組 模組化到底做了什麼

js模組化 ES6 引入第三方模組 模組化到底做了什麼

雜湊表是一種資料結構,不是演算法

一個需求:新員工報道,將該員工的資訊加入(id,性別,年齡,。。。)輸入該員工的id時,要求找到該員工的所有資訊

要求:不使用資料庫,儘量節省記憶體,速度越快越好 ————雜湊表(雜湊)

 

 一級快取,或者還有有二級快取

  1 import java.util.Hashtable;
  2 import java.util.Scanner;
  3 
  4 public class HashTableDemo {
  5     public static void main(String[] args) {
  6         //建立雜湊表
  7
HashTab hashTab = new HashTab(7); 8 //寫一個簡單選單 9 String key = ""; 10 Scanner scanner = new Scanner(System.in); 11 while (true){ 12 System.out.println("add:新增僱員"); 13 System.out.println("list:顯示僱員"); 14 System.out.println("find:查詢僱員");
15 System.out.println("exit:退出系統"); 16 key = scanner.next(); 17 switch (key){ 18 case "add": 19 System.out.println("輸入id"); 20 int id = scanner.nextInt(); 21 System.out.println("輸入名字");
22 String name = scanner.next(); 23 //建立僱員 24 Emp emp = new Emp(id, name); 25 hashTab.add(emp); 26 break; 27 case "list": 28 hashTab.list(); 29 break; 30 case "find": 31 System.out.println("請輸入要查詢的id"); 32 id = scanner.nextInt(); 33 hashTab.findEmpById(id); 34 break; 35 case "exit": 36 scanner.close(); 37 System.exit(0); 38 default: 39 break; 40 } 41 } 42 43 } 44 45 } 46 47 48 //雜湊表HashTable 管理多條連結串列 49 class HashTab{ 50 private EmpLinkedList[] empLinkedListArray; 51 private int size;//表示有多少條連結串列 52 //構造器 53 public HashTab(int size){ 54 this.size = size; 55 //初始化empLinkedListArray 56 empLinkedListArray = new EmpLinkedList[size]; 57 //這時不要忘記分別初始化每個連結串列 58 // 59 for (int i = 0; i < size; i++) { 60 empLinkedListArray[i] = new EmpLinkedList(); 61 } 62 63 } 64 65 //新增僱員 66 public void add(Emp emp){ 67 //根據員工的id,得到該員工應該加入到哪條連結串列 68 int empLinkedListNO = hashFun(emp.id); 69 //將emp 新增到對應的連結串列中 70 empLinkedListArray[empLinkedListNO].add(emp); 71 } 72 //遍歷所有連結串列 73 public void list(){ 74 for (int i = 0; i < size; i++) { 75 empLinkedListArray[i].list(i); 76 } 77 } 78 //編寫雜湊函式,使用一個簡單取模法 79 public int hashFun(int id){ 80 return id % size; 81 } 82 83 //根據輸入id查詢僱員 84 public void findEmpById(int id){ 85 //使用雜湊函式到哪條連結串列查詢 86 int empLinkedLisNO = hashFun(id); 87 Emp emp = empLinkedListArray[empLinkedLisNO].findEmpById(id); 88 if (emp!= null){ 89 System.out.printf("在第%d條連結串列中找到僱員id=%d\n",(empLinkedLisNO+1),id); 90 }else { 91 System.out.println("在雜湊表中,沒有找到該僱員"); 92 } 93 } 94 95 } 96 97 98 //建立EmpLinkedList,表示連結串列 99 class EmpLinkedList{ 100 //頭指標,執行第一個Emp,因此我們這個連結串列的head是直接指向第一個Emp 101 private Emp head;//預設null 102 //新增僱員到連結串列 103 //說明 104 //!.假定當新增僱員時,id是自增,即id的分配總是從小到大 105 //因此將該僱員直接加入到本連結串列最後位置 106 public void add(Emp emp){ 107 //如果是新增第一個僱員 108 if (head == null){ 109 head = emp; 110 return; 111 } 112 //如果不是第一個僱員,則使用一個輔助指標幫助定位到最後 113 Emp curEmp = head; 114 while (true){ 115 if (curEmp.next == null){ 116 break; 117 } 118 curEmp = curEmp.next; 119 } 120 //退出時直接將emp加到最後 121 curEmp.next = emp; 122 123 } 124 //遍歷連結串列的僱員資訊 125 public void list(int no){ 126 if (head == null){//連結串列為空 127 System.out.println("第"+(no+1)+"連結串列為空"); 128 return; 129 } 130 System.out.print("第"+(no+1) 131 +"連結串列的資訊為"); 132 Emp curEmp = head; 133 while (true){ 134 System.out.printf("id = %d name = %s \t",curEmp.id,curEmp.name); 135 if (curEmp.next == null){ 136 break; 137 } 138 curEmp = curEmp.next; 139 } 140 System.out.println(); 141 } 142 //根據id查詢僱員 143 public Emp findEmpById(int id){ 144 //判斷連結串列是否為空 145 if (head == null){ 146 System.out.println("連結串列為空"); 147 return null; 148 } 149 //輔助指標 150 Emp curEmp = head; 151 while (true){ 152 if (curEmp.id == id){//找到 153 break;//這時curEmp指向要查詢的僱員 154 } 155 //退出 156 if (curEmp.next == null){ 157 curEmp = null; 158 break; 159 } 160 curEmp = curEmp.next; //移後 161 } 162 return curEmp; 163 164 } 165 } 166 // 167 168 169 //表示一個僱員 170 class Emp{ 171 public int id; 172 public String name; 173 public Emp next;//預設為null 174 175 public Emp(int id, String name) { 176 this.id = id; 177 this.name = name; 178 179 } 180 }