Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. package testclass;
  2.  
  3. import java.util.*;
  4.  
  5. class LinkedListNode<E> {
  6. E data;
  7. LinkedListNode<E> next;
  8. }
  9.  
  10. class LinkedList<E> {
  11.  
  12. static <E> E deleteNode(LinkedListNode<E> node) {
  13. if(node == null || node.next == null) return null;
  14.  
  15. E retval = node.data;
  16. LinkedListNode<E> next = node.next;
  17. node.data = next.data;
  18. node.next = next.next;
  19. return retval;
  20. }
  21.  
  22. private LinkedListNode<E> head;
  23. private LinkedListNode<E> tail;
  24.  
  25. public LinkedList() {
  26. this.head = new LinkedListNode<E>();
  27. this.tail = new LinkedListNode<E>();
  28. head.next = tail;
  29. }
  30.  
  31. public LinkedListNode<E> getHead() {
  32. return head;
  33. }
  34.  
  35. public LinkedListNode<E> getTail() {
  36. return tail;
  37. }
  38.  
  39.  
  40.  
  41. public void addLast(E e) {
  42. LinkedListNode<E> node = new LinkedListNode<E>(); // e and next are null
  43. tail.data = e;
  44. tail.next = node;
  45. tail = node;
  46. }
  47.  
  48. public void addFirst(E e) {
  49. LinkedListNode<E> node = new LinkedListNode<E>(); // e and next are null;
  50. node.next = head.next;
  51. node.data = e;
  52. head.next = node;
  53. }
  54.  
  55. public E deleteFirst() {
  56. LinkedListNode<E> first = head.next;
  57. head.next = first.next;
  58. return first.data;
  59. }
  60.  
  61. public E deleteLast() {
  62. // cannot do without iteration of the list! :-(
  63. throw new UnsupportedOperationException();
  64. }
  65.  
  66. public LinkedListNode<E> findFirst(E e) {
  67. LinkedListNode<E> curr = head.next;
  68. while(curr != null) {
  69. if(curr.data != null && curr.data.equals(e)) return curr;
  70. curr = curr.next;
  71. }
  72. return null;
  73. }
  74.  
  75. public void print() {
  76. LinkedListNode<E> curr = head.next;
  77. while(curr.next != null) {
  78. System.out.println(curr.data);
  79. curr = curr.next;
  80. }
  81. }
  82.  
  83. public boolean isEmpty(){
  84. return this.head.next == this.tail;
  85. }
  86.  
  87.  
  88. }
  89.  
  90. class HashNode<T>{
  91. T object;
  92. String key;
  93.  
  94. HashNode(String s,T o){
  95. this.object = o;
  96. this.key = s;
  97. }
  98. }
  99.  
  100. class HashTable{
  101. LinkedList<HashNode>[] Table;
  102. int size;
  103.  
  104. HashTable(int initial){
  105. this.size = initial;
  106. Table = new LinkedList[initial];
  107. for(int i = 0; i < initial;i++ ){
  108. Table[i] = new LinkedList<HashNode>();
  109. }
  110. }
  111.  
  112. HashTable(){
  113. this(9151);
  114. }
  115.  
  116. public int hashKey(String S){
  117. return (((S.hashCode() & (0x7fffffff)) + 3) % this.size);
  118. }
  119.  
  120. public int hashKey(int I){
  121. return ((I + 3) % this.size);
  122. }
  123.  
  124. void add(HashNode node){
  125. int k = hashKey(node.key);
  126. LinkedListNode N = Table[k].getHead();
  127. int i = 0;
  128. if(Table[k].isEmpty()){
  129. Table[k].addLast(node);
  130. System.out.println("OK");
  131. }else{
  132. while(N.next != Table[k].getTail()){
  133. N=N.next;
  134. i++;
  135. }
  136. node.object = (String) ("" + node.key + String.valueOf(i));
  137. Table[k].addLast(node);
  138. System.out.println(node.object);
  139. }
  140. }
  141. /*
  142. void delete(int I){
  143. int k = hashKey(I);
  144. int z=Table[k].size();
  145. int index = 0;
  146. for(int i = 0; i < z; i++){
  147. if(Table[k].get(i).key == I){
  148. index = i;
  149. break;
  150. }
  151. }
  152. Table[k].remove(index);
  153. }
  154.  
  155. HashNode search(int I){
  156. int k = hashKey(I);
  157. int z=Table[k].size();
  158. int index = 0;
  159. for(int i = 0; i < z; i++){
  160. if(Table[k].get(i).key == I){
  161. index = i;
  162. break;
  163. }
  164. }
  165. System.out.println(k+" "+I);
  166. return Table[k].get(index);
  167. }
  168. */
  169. /*
  170. void delete(String S){
  171. int k = hashKey(S);
  172. int z=Table[k].size();
  173. int index = 0;
  174. for(int i = 0; i < z; i++){
  175. if(Table[k].get(i).key == S){
  176. index = i;
  177. break;
  178. }
  179. }
  180. Table[k].remove(index);
  181. }
  182.  
  183. HashNode search(String S){
  184. int k = hashKey(S);
  185. int z = Table[k].size();
  186. int index = -1;
  187. for(int i = 0; i < z; i++){
  188. if(Table[k].get(i).key == S){
  189. index = i;
  190. break;
  191. }
  192. }
  193. if(index==-1){
  194. HashNode N = new HashNode("00","No existe");
  195. return N;
  196. }
  197. //System.out.println(k+" "+S);
  198. return Table[k].get(index);
  199. }
  200. */
  201. /*
  202. public boolean contains(HashNode H){
  203. int k = hashKey(H.key);
  204. return Table[k].contains(H);
  205. }
  206. */
  207. }
  208.  
  209. public class TestClass {
  210.  
  211. public static void main(String[] args){
  212. Scanner sc = new Scanner(System.in);
  213. while(sc.hasNext()){
  214. int tc = sc.nextInt();
  215. HashTable T = new HashTable(7919);
  216. for(int i = 0; i < tc; i++){
  217. String st = sc.next();
  218. HashNode N = new HashNode(st,st);
  219. T.add(N);
  220. }
  221. }
  222. }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement