Kendrick Wesley IP5 American Intercontinental University ITCO321

Kendrick Wesley
IP5
American Intercontinental University
ITCO321

?
Hash structure

Hash Function

?
Source code

Node class

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package haship5;
import java.util.ArrayList;

/**
*
* @author squal
*/
class Node {//make a node chian
M menuKey;
T itemTaco;
Node next;

public Node(M menuKey, T itemTaco) {
this.menuKey = menuKey;
this.itemTaco = itemTaco;
}
}

HashIP5 class

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package haship5;
import java.util.ArrayList;

/**
*
* @author squal
*/
public class HashIP5 {

static class NodeMap{
private ArrayList bucketListArray;

private int bucketNumber;

private int arrayListSize;

public NodeMap(){
bucketListArray = new ArrayList();

bucketNumber = 10;

arrayListSize = 4;

}

public int arrayListSize() {
return arrayListSize;
}

private int getBucketListIndex(M menuKey){
int hashCode = menuKey.hashCode();
int bucketListIndex = hashCode % bucketNumber;
return bucketListIndex;

}

public T get(M menuKey){//get the item from the menu
int bucketListIndex = getBucketListIndex(menuKey);
Node head = bucketListArray.get(bucketListIndex);

while (head !=null){
if (head.menuKey.equals(menuKey))
return head.itemTaco;
head = head.next;
}
return null;
}

public void add(M menuKey, T itemTaco){
int bucketListIndex = getBucketListIndex(menuKey);
Node head = bucketListArray.get(bucketListIndex);
while (head !=null){
if (head.menuKey.equals(menuKey)){
head.itemTaco = itemTaco;
return;
}
head = head.next;

}

arrayListSize ++;
head = bucketListArray.get(bucketListIndex);
Node bucketNode = new Node(menuKey, itemTaco);
bucketNode.next = head;
bucketListArray.set(bucketListIndex, bucketNode);

}

public T remove(M menuKey){
int bucketListIndex = getBucketListIndex(menuKey);

Node head = bucketListArray.get(bucketListIndex);

Node prev = null;
while (head != null){
if (head.menuKey.equals(menuKey))
break;
else
prev = head;
head = head.next;
}

if (head == null)
return null;

arrayListSize –;

if (prev !=null)
prev.next = head.next;
else
bucketListArray.set(bucketListIndex, head.next);
return head.itemTaco;
}

}

public static void main(String args) {
// TODO code application logic here
NodeMap nodeMap = new NodeMap();
nodeMap.add(“crunchy”, 1);
nodeMap.add(“Soft”, 2);
nodeMap.remove(“Crunchy”);
nodeMap.remove(“Soft”);

}

}