/
StringCircularLinkedList.java
165 lines (159 loc) · 3.22 KB
/
StringCircularLinkedList.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Written by Noah Shaw
*/
public class StringCircularLinkedList {
private class ListNode
{
//Instance variables for ListNode
private String data;
private ListNode link;
//Constructor for ListNode
public ListNode(String aData, ListNode aLink)
{
data = aData;
link = aLink;
}
}
//Instance variables
private ListNode head;
private ListNode current;
private ListNode previous;
private ListNode tail;
//Constructor
public StringCircularLinkedList()
{
head = current = previous = tail = null;
}
//Methods
public void goToNext() //Goes to next node
{
if(current == null)
{
return;
}
if(current == tail)
{
previous = current;
current = head;
return;
}
previous = current;
current = current.link;
}
public void goToPrev() //Goes to previous node
{
current = previous;
ListNode orgCurr = new ListNode(previous.data, head.link);
while(orgCurr != previous)
{
orgCurr = orgCurr.link;
}
previous = orgCurr;
}
public String getDataAtCurrent() //Returns the data at the current node
{
if(current != null)
{
return current.data;
}
return null;
}
public void setDataAtCurrent(String aData) //Sets the data at the current node
{
if(current != null)
{
current.data = aData;
}
}
public void insert(String aData) //Inserts data at the end of the list
{
ListNode newNode = new ListNode(aData, null);
//List is empty
if(head == null)
{
head = newNode;
current = newNode;
tail = newNode;
return;
}
//List is not empty
tail.link = newNode;
tail = newNode;
tail.link = head;
}
public void insertAfterCurrent(String aData) //Inserts data after the current node
{
if(current == null)
{
return;
}
ListNode newNode = new ListNode(aData, current.link);
current.link = newNode;
if(newNode == tail)
{
newNode.link = head;
}
}
public void deleteCurrent() //Deletes the current node
{
if(current == tail) //Current is at the tail
{
previous.link = current.link;
current = current.link;
tail = previous;
tail.link = head;
}
else if(current == head) //Current is at the head
{
previous.link = current.link;
current = current.link;
head = current;
head.link = current.link;
}
else
{
previous.link = current.link;
current = current.link;
}
}
public void print() //Prints all of the data in the node
{
ListNode temp = head;
System.out.println(temp.data);
for(temp = head.link; temp != head; temp = temp.link)
{
System.out.println(temp.data);
}
}
public boolean isInList(String aData) //Calls the search method
{
return search(aData) != null;
}
private ListNode search(String aData) //Searches for the data in the list
{
ListNode temp = head;
if(temp.data.equalsIgnoreCase(aData))
{
return temp;
}
else
{
for(temp = head.link; temp != head; temp = temp.link)
{
if(temp.data.equalsIgnoreCase(aData))
{
return temp;
}
}
}
return null;
}
public void printprev()
{
System.out.println(previous.data);
}
public void printcurr()
{
System.out.println(current.data);
}
}