Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. class HashSetImpl {
  5. public static void main(String[] args) {
  6.  
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. int tamanho = Integer.parseInt(scan.nextLine());
  10. HashSetInteger conjunto = new HashSetInteger(tamanho);
  11.  
  12. Integer value;
  13. String entrada[];
  14. String operacao = "";
  15.  
  16. while (!operacao.equals("end")) {
  17. entrada = scan.nextLine().split(" ");
  18. operacao = entrada[0];
  19.  
  20. switch (operacao) {
  21. case "put":
  22. value = Integer.parseInt(entrada[1]);
  23. conjunto.put(value);
  24. break;
  25.  
  26. case "remove":
  27. value = Integer.parseInt(entrada[1]);
  28. conjunto.remove(value);
  29. break;
  30.  
  31. case "contains":
  32. value = Integer.parseInt(entrada[1]);
  33. System.out.println(conjunto.contains(value));
  34. break;
  35. }
  36.  
  37. }
  38. scan.close();
  39. }
  40.  
  41. }
  42.  
  43. class HashSetInteger {
  44.  
  45. private Celula[] tabela;
  46. private int elementos;
  47.  
  48. public HashSetInteger(int tamanho) {
  49. this.tabela = new Celula[tamanho];
  50. this.elementos = 0;
  51. }
  52.  
  53. public void put(int value) {
  54. if (!isFull() && !this.contains(value)) {
  55. int i = 0;
  56. int j;
  57.  
  58. boolean inseriu = false;
  59. while (i < this.tabela.length && !inseriu) {
  60. j = this.probFunction(value, i);
  61. if (this.tabela[j] == null || this.tabela[j].isDeleted()) {
  62. this.tabela[j] = new Celula(value);
  63. this.elementos++;
  64. inseriu = true;
  65. }
  66. i++;
  67. }
  68. }
  69. System.out.println(this.toString());
  70. }
  71.  
  72. public void remove(int value) {
  73. if (this.contains(value)) {
  74. int i = 0;
  75. int j = this.probFunction(value, i);
  76.  
  77. boolean removeu = false;
  78.  
  79. while (i < this.tabela.length && this.tabela[j] != null && !removeu) {
  80. j = this.probFunction(value, i);
  81. if (this.tabela[j].getValue() == value) {
  82. this.tabela[j].delete();
  83. this.elementos--;
  84. removeu = true;
  85. }
  86. i++;
  87. }
  88. }
  89. System.out.println(this.toString());
  90. }
  91.  
  92. public boolean contains(int value) {
  93. boolean contains = false;
  94. if (!isEmpty()) {
  95. int i = 0;
  96. while (i < this.tabela.length - 1 && !contains) {
  97. int j = this.probFunction(value, i);
  98. if (this.tabela[j] != null && !this.tabela[j].isDeleted()) {
  99. if (this.tabela[j].getValue() == value) {
  100. contains = true;
  101. }
  102. }
  103. i++;
  104. }
  105. }
  106. return contains;
  107. }
  108.  
  109. public boolean isEmpty() {
  110. return (this.elementos == 0);
  111. }
  112.  
  113. public boolean isFull() {
  114. return (this.elementos == this.tabela.length);
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return Arrays.toString(this.tabela);
  120. }
  121.  
  122. private int probFunction(int k, int i) {
  123. return (k + i) % this.tabela.length;
  124. }
  125.  
  126. }
  127.  
  128. class Celula {
  129.  
  130. private Integer value;
  131. private boolean deleted;
  132.  
  133. public Celula(Integer valor) {
  134. this.value = valor;
  135. this.deleted = false;
  136. }
  137.  
  138. public void delete() {
  139. this.deleted = true;
  140. }
  141.  
  142. public boolean isDeleted() {
  143. return this.deleted;
  144. }
  145.  
  146. public Integer getValue() {
  147. return value;
  148. }
  149.  
  150. @Override
  151. public String toString() {
  152. String result = null;
  153. if (!this.deleted) {
  154. result = this.value + "";
  155. }
  156. return result;
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement