-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLLSimpleList.java
136 lines (108 loc) · 2.17 KB
/
SLLSimpleList.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
public class SLLSimpleList implements SimpleList<SLLNode> {
private SLLNode head;
private SLLNode tail;
int counter;
public SLLSimpleList() {
head = null;
tail = null;
}
public void insertFirst(int value)
{
if(isEmpty()){
SLLNode node = new SLLNode(value);
head = node;
tail = node;
}
else{
SLLNode node=new SLLNode(value);
node.setNext(head);
head=node;
}
counter++;
}
public void insertLast(int value)
{
SLLNode node=new SLLNode(value);
node.setNext(null);
if(tail==null)
{
head=node;
tail=node;
}
else
{
tail.setNext(node);
tail=node;
}
counter++;
}
public SLLNode first()
{
if(isEmpty()){
return null;
}
else{
return head;
}
}
public SLLNode last()
{
if(isEmpty()){
return null;
}
else{
return tail;
}
}
public boolean isFirst(SLLNode p)
{
if(head == p)
{
return true;
}
else
{
return false;
}
}
public boolean isLast(SLLNode p)
{
if(p==tail){
return true;
}
else{
return false;
}
}
public SLLNode before(SLLNode p) {
SLLNode current_node = head;
while(!current_node.equals(tail)){
if(current_node.getNext().equals(p))
return current_node;
else
current_node = current_node.getNext();
}
return null;
}
public SLLNode after(SLLNode p) {
return p.getNext();
}
public boolean isEmpty()
{
return head==null;
}
public int size()
{
//return counter;
int size = 0;
for(SLLNode n = head; n != null; n = n.getNext())
size++;
return size;
}
public void setHead(int head){
this.head = new SLLNode(head);
}
public void setTail(int tail){
this.tail = new SLLNode(tail);
}
}