Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testclass;
- import java.util.*;
- class LinkedListNode<E> {
- E data;
- LinkedListNode<E> next;
- }
- class LinkedList<E> {
- static <E> E deleteNode(LinkedListNode<E> node) {
- if(node == null || node.next == null) return null;
- E retval = node.data;
- LinkedListNode<E> next = node.next;
- node.data = next.data;
- node.next = next.next;
- return retval;
- }
- private LinkedListNode<E> head;
- private LinkedListNode<E> tail;
- public LinkedList() {
- this.head = new LinkedListNode<E>();
- this.tail = new LinkedListNode<E>();
- head.next = tail;
- }
- public LinkedListNode<E> getHead() {
- return head;
- }
- public LinkedListNode<E> getTail() {
- return tail;
- }
- public void addLast(E e) {
- LinkedListNode<E> node = new LinkedListNode<E>(); // e and next are null
- tail.data = e;
- tail.next = node;
- tail = node;
- }
- public void addFirst(E e) {
- LinkedListNode<E> node = new LinkedListNode<E>(); // e and next are null;
- node.next = head.next;
- node.data = e;
- head.next = node;
- }
- public E deleteFirst() {
- LinkedListNode<E> first = head.next;
- head.next = first.next;
- return first.data;
- }
- public E deleteLast() {
- // cannot do without iteration of the list! :-(
- throw new UnsupportedOperationException();
- }
- public LinkedListNode<E> findFirst(E e) {
- LinkedListNode<E> curr = head.next;
- while(curr != null) {
- if(curr.data != null && curr.data.equals(e)) return curr;
- curr = curr.next;
- }
- return null;
- }
- public void print() {
- LinkedListNode<E> curr = head.next;
- while(curr.next != null) {
- System.out.println(curr.data);
- curr = curr.next;
- }
- }
- public boolean isEmpty(){
- return this.head.next == this.tail;
- }
- }
- class HashNode<T>{
- T object;
- String key;
- HashNode(String s,T o){
- this.object = o;
- this.key = s;
- }
- }
- class HashTable{
- LinkedList<HashNode>[] Table;
- int size;
- HashTable(int initial){
- this.size = initial;
- Table = new LinkedList[initial];
- for(int i = 0; i < initial;i++ ){
- Table[i] = new LinkedList<HashNode>();
- }
- }
- HashTable(){
- this(9151);
- }
- public int hashKey(String S){
- return (((S.hashCode() & (0x7fffffff)) + 3) % this.size);
- }
- public int hashKey(int I){
- return ((I + 3) % this.size);
- }
- void add(HashNode node){
- int k = hashKey(node.key);
- LinkedListNode N = Table[k].getHead();
- int i = 0;
- if(Table[k].isEmpty()){
- Table[k].addLast(node);
- System.out.println("OK");
- }else{
- while(N.next != Table[k].getTail()){
- N=N.next;
- i++;
- }
- node.object = (String) ("" + node.key + String.valueOf(i));
- Table[k].addLast(node);
- System.out.println(node.object);
- }
- }
- /*
- void delete(int I){
- int k = hashKey(I);
- int z=Table[k].size();
- int index = 0;
- for(int i = 0; i < z; i++){
- if(Table[k].get(i).key == I){
- index = i;
- break;
- }
- }
- Table[k].remove(index);
- }
- HashNode search(int I){
- int k = hashKey(I);
- int z=Table[k].size();
- int index = 0;
- for(int i = 0; i < z; i++){
- if(Table[k].get(i).key == I){
- index = i;
- break;
- }
- }
- System.out.println(k+" "+I);
- return Table[k].get(index);
- }
- */
- /*
- void delete(String S){
- int k = hashKey(S);
- int z=Table[k].size();
- int index = 0;
- for(int i = 0; i < z; i++){
- if(Table[k].get(i).key == S){
- index = i;
- break;
- }
- }
- Table[k].remove(index);
- }
- HashNode search(String S){
- int k = hashKey(S);
- int z = Table[k].size();
- int index = -1;
- for(int i = 0; i < z; i++){
- if(Table[k].get(i).key == S){
- index = i;
- break;
- }
- }
- if(index==-1){
- HashNode N = new HashNode("00","No existe");
- return N;
- }
- //System.out.println(k+" "+S);
- return Table[k].get(index);
- }
- */
- /*
- public boolean contains(HashNode H){
- int k = hashKey(H.key);
- return Table[k].contains(H);
- }
- */
- }
- public class TestClass {
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- while(sc.hasNext()){
- int tc = sc.nextInt();
- HashTable T = new HashTable(7919);
- for(int i = 0; i < tc; i++){
- String st = sc.next();
- HashNode N = new HashNode(st,st);
- T.add(N);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement