-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerCircularLinkedList.java
92 lines (88 loc) · 1.81 KB
/
PlayerCircularLinkedList.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
/*
* Written by Noah Shaw
*/
public class PlayerCircularLinkedList {
//Private class
private class ListNode
{
//Instance variables for ListNode
private Player data;
private ListNode link;
//Constructor for ListNode
public ListNode(Player aData, ListNode aLink)
{
data = aData;
link = aLink;
}
}
//Instance variables
private ListNode head;
private ListNode current;
private ListNode previous;
private ListNode tail;
//Constructor
public PlayerCircularLinkedList()
{
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 insert(Player aData) //Inserts data at the end of the list
{
ListNode newNode = new ListNode(aData, null);
if(head == null) //List is empty
{
head = newNode;
current = newNode;
tail = newNode;
return;
}
tail.link = newNode; //List is not empty
tail = newNode;
tail.link = head;
}
public Player getDataAtCurrent() //Returns the data at the current node
{
if(current != null)
{
return current.data;
}
return null;
}
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 //Current is in the middle of the list
{
previous.link = current.link;
current = current.link;
}
}
}