-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsippets.java
More file actions
95 lines (81 loc) · 1.86 KB
/
sippets.java
File metadata and controls
95 lines (81 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import CSIS2420.List;
import CSIS2420.Node;
import CSIS2420.TreeNode;
public void inOrderTraversal (TreeNode node) {
if (node == null) {
return;
}
inOrderTraversal(node.getLeft());
//System.out.println(node.getKey()+" ");
inOrderTraversal(node.getRight());
}
public void insertAtFront (Object insertItem){
if (isEmpty()) {
firstNode = lastNode = new Node(insertItem);
}
else {
firstNode = new Node(insertItem, firstNode);
}
}
public String print () {
String result = "";
String newline = "\n";
int counter = 0;
int stop = 2;
if (isEmpty()) {
result += name + " is Empty" + newline;
return result;
}
result += name + " contains: " + newline;
Node current = firstNode;
while ((current != null)&&(counter<stop)) {
result += current.getObject() + newline;
if(current.equals(lastNode)){
counter++;
if(counter==stop){
result += "Error: Infinite Loop";
}
}
current = current.getNext();
}
return result;
}
public class myHashTable {
public static void main (String [] args){
List[] hashTable = new List[23];
int key;
int value;
//Always initialize object arrays
for(int i=0; i<hashTable.length; i++){
hashTable[i] = new List();
}
for(int i=0; i<15; i++){
value = (int)(Math.random()*1000);
key = value % 23; //Hash function
hashTable[key].insertAtFront(value);
}
for(int i=0; i<23; i++){
System.out.println("index "+i+" "+hashTable[i].print());
}
}
}
public Object removeFromBack(){
Object removeItem = null;
if (isEmpty()) {
removeItem = "This list is empty!";
return removeItem;
}
removeItem = lastNode.getObject();
if (lastNode.equals(firstNode)) {
lastNode = firstNode = null;
}
else {
Node currentNode = firstNode;
while(currentNode.getNext()!=lastNode){
currentNode = currentNode.getNext();
}
lastNode = currentNode;
lastNode.setNext(null);
}
return removeItem;
}