-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenLinkedList.java
More file actions
137 lines (122 loc) · 2.92 KB
/
GenLinkedList.java
File metadata and controls
137 lines (122 loc) · 2.92 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
134
135
136
137
public class GenLinkedList <T> { //MAKES IT A GENERIC TYPE
//Internal class of the node
private class ListNode
{
private T data; //T CAN BE ANYTHING
private ListNode link;
public ListNode()
{
data = null;
link = null;
}//ListNode
public ListNode(T aData, ListNode aLink)
{
data = aData;
link = aLink;
}//ListNode
}//ListNode
//POINTER NODES
private ListNode head;
private ListNode current;
private ListNode previous;
public GenLinkedList()
{
head = current = previous = null;
}//GenLinkedList
public T getCurrent()
{
if(current != null)//If current is not end of list
{
return current.data;
}//if
else
{
return null;
}//else
}//getCurrent
public void setCurrent(T aData)
{
if(current!=null)
{
this.current.data = aData;
}//if
}//setCurrent
//INSERT A NEW ELEMENT TO THE END OF THE LIST
public void insert(T newData)
{
//Create a new node
ListNode newNode = new ListNode(newData,null);
if(head == null)//THIS MEANS THE LIST IS EMPTY
{
head = newNode; //POINTS HEAD TO THE NEW NODE
current = head; //SET THE CURRENT TO THE HEAD
return;
}//if
ListNode tmp = head; //TMP NEEDS TO POINT TO THE HEAD
while(tmp.link != null) //AS LONG AS THE NODE IS POINTING TO SOMETHING
{
//MOVE THE TMP MODE FORWARD
tmp = tmp.link;
}//while
//NOW WE'RE AT THE END OF THE LIST
tmp.link = newNode;
}//insert
public void insertAfterCurrent(T newData)
{
//ANY INSERT STARTS OUT BY CREATING A NEW LODE
ListNode newNode = new ListNode(newData, null);
if(current!=null)
{
newNode.link = current.link; //BECAUSE THE NEW NODE SHOULD BE POINTING AT WHERE THE CURRENT IS POINTING
current.link = newNode; //NOW CURRENT HAS TO POINT AT THE NEW NODE
}//if
else if(head!=null)
{
System.out.println("Current is outside of the linked list");
}//else fi
else
{
System.out.println("List is empty");
}//else
// IF HEAD IS NULL THAT MEANS YOU HAVE AN EMPTY LIST, BUT IF CURRENT IS NULL THERE COULD BE A VARIETY OF REASONS WHY s
}//insertAfterCurrent
//MOVE CURRENT FORWARD
public void goToNext()
{
if(current != null)
{
previous = current;
current = current.link;
}//if
}//goToNext
public void resetCurrent()
{
current = head;
previous = null;
}//resetCurrent
public boolean hasMore() {
return current != null;
}//hasMore
public void print()
{
ListNode tmp = head;
while(tmp!=null)
{
System.out.println(tmp.data);
tmp = tmp.link;
}//while
}//print
public void deleteCurrent()
{
if(current!=null&&previous!=null)//INDICATES THAT CURRENT IS NOT EQUAL TO THE HEAD
{
previous.link = current.link;//EFFECTIVELY POINTS OVER TOP OF IT
current = current.link;
}//if
else if(current != null && previous == null)//YOU'RE AT THE HEAD
{
head = head.link; //or you can do head = current.link because tehy are pointing to the same thing
current = head;
}//else if
}//deleteCurrent
}//GenLinkedList