-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenLL.java
More file actions
133 lines (114 loc) · 2.77 KB
/
GenLL.java
File metadata and controls
133 lines (114 loc) · 2.77 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
public class GenLL <T>{ //generic of type t
private class ListNode{ //internal class
private T data;
private ListNode link;
public ListNode(T aData, ListNode aLink) {
data = aData;
link = aLink;
}
}
private ListNode head; //first item in list
private ListNode current;
private ListNode previous;
public GenLL() {
head = current = previous = null;
}
public void insert(T aData) {
ListNode newNode = new ListNode(aData, null);//null since inserted at end of list
if(head == null) {
head = newNode; //head and newNode hold addresses to same data in heap memory
current = head; //since head is first element, its also the current
return;
}
ListNode temp = head;
while(temp.link!=null) {
temp = temp.link;
}
temp.link = newNode;
}
public void print() {
for(ListNode temp = head; temp!=null; temp = temp.link)
System.out.println(temp.data);
System.out.println();
}
public T getCurrent() {
if(current != null)
return current.data;
return null;
}
public void setCurrent(T aData) {
if(current != null)
current.data = aData;
}
public void goToNext() {
if(current == null)
return;
previous = current;
current = current.link;
}
public void reset() {
previous = null;
current = head;
}
public void insertAfterCurrent(T aData) {
if(current == null)
return;
ListNode newNode = new ListNode(aData, current.link);
current.link = newNode;
}
public void deleteCurrent() {
if(current!=null && previous!=null) {
previous.link = current.link; //memory address of previous.link is current so just skip over current
current = current.link;
}
else if(current!=null && previous == null) { //current at head
head = head.link; //current = current.link
}
}
private ListNode search(T aData) {
ListNode temp = head;
while(temp!= null) {
if (temp.data.equals(aData))
return temp;
temp = temp.link;
}
return null;
}
public boolean isContained(T aData) {
return this.findNodeWith(aData) != null;
}
private ListNode findNodeWith(T aData) {
ListNode temp = head;
while(temp!=null) {
if(temp.data.equals(aData)) {
return temp;
}
}
return null;
}
public void goToItem(T aData) {
ListNode temp = this.findNodeWith(aData);
if(temp == null)
return;
this.resetCurrent();
while(this.hasMore() && current!= temp) {
this.goToNext();
}
}
public void resetCurrent() {
current = head;
}
public boolean hasMore() {
return current!=null;
}
public void delteCurrent() {
if(current!=null && previous!=null) { //current is at anything but the head
previous.link = current.link; //previous.link is the same thing as current
current = current.link;
}
else if(current!=null) {
head = head.link;
}
}
//goToPrevious; start at head and while
}