-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularLinkedList
119 lines (100 loc) · 2.13 KB
/
CircularLinkedList
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
package com.cs2114.dungeonescape;
/**
* Circular linked list
*
* @author Joe Bruzek, Yash Pant, Marcus Stewart
* @version Dec 5, 2013
* @param <T>
* the type of data stored in the list
*/
public class CircularLinkedList<T>
{
private Node<T> current;
private int size;
/**
* Create a new CircularLinkedList object.
*/
public CircularLinkedList()
{
current = null;
size = 0;
}
/**
* add an element behind the current node
*
* @param data
* the data of the new node
*/
public void add(T data)
{
if (size == 0)
{
current = new Node<T>(data);
current.join(current);
size++;
}
else
{
addBehind(current, data);
}
}
/**
* add an element behind a specific node makes the new node the current node
*
* @param node
* the node to be added behind
* @param data
* the data of the new node
*/
public void addBehind(Node<T> node, T data)
{
Node<T> newNode = new Node<T>(data);
newNode.join(node.split());
node.join(newNode);
current = newNode;
size++;
}
/**
* removes the current node moves the current node to the previous node
*/
public void remove()
{
Node<T> previous = current.previous();
previous.split();
previous.join(current.split());
current = previous;
size--;
}
/**
* returns the data of the current node
*
* @return the data of the current node
*/
public T getCurrent()
{
return current.data();
}
/**
* move the current node to the next node
*/
public void next()
{
current = current.next();
}
/**
* move the current node to the previous node
*/
public void previous()
{
current = current.previous();
}
/**
* return the size of the list
*
* @return the size of the list
*/
public int size()
{
return size;
}
}