-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
176 lines (156 loc) · 2.73 KB
/
LinkedList.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
166
167
168
169
170
171
172
173
174
175
176
public class LinkedList<Type>
{
public Node<Type> first;
private int size;
public LinkedList()
{
size = 0;
first = null;
}
public LinkedList(Type f)
{
Node<Type> temp = new Node<Type>(f);
first = temp;
size = 1;
}
//------------------------------
//Adds element to front of list
//------------------------------
public void addFirst(Type f)
{
Node<Type> temp = new Node<Type>(f);
if(size == 0)
first = temp;
else
{
temp.setNext(first);
first = temp;
}
size++;
}
//------------------------------------
//removes the last element of the list
//------------------------------------
public void addLast(Type last)
{
Node<Type> temp = new Node<Type>(last);
if(size == 0)
first = temp;
else
{
temp.setNext(null);
Node<Type> curr = first;
while(curr.getNext() != null)
curr = curr.getNext();
curr.setNext(temp);
}
size++;
}
public Node<Type> getFirstNode()
{
return first;
}
public void reverseList()
{
Node<Type> previousNode = null;
Node<Type> currNode = first;
while(currNode != null)
{
Node<Type> nextNode = currNode.getNext();
currNode.setNext(previousNode);
previousNode = currNode;
currNode = nextNode;
}
first = previousNode;
}
public Node<Type> newReverse(Node<Type> curr)
{
if(curr == null || curr.getNext() == null)
return curr;
else
{
Node<Type> reversed = newReverse(curr.getNext());
curr.getNext().setNext(curr);
curr.setNext(null);
first = reversed;
return reversed;
}
}
//---------------------
//removes first element
//---------------------
public Type remFirst()
{
if(size == 0)
return null;
else if(size == 1)
{
Type temp = first.getValue();
first = null;
size--;
return temp;
}
else
{
Type temp = first.getValue();
first = first.getNext();
size--;
return temp;
}
}
//--------------------
//removes last element
//--------------------
public Type remLast()
{
if(size == 0)
return null;
else if(size == 1)
{
Type temp = first.getValue();
first = null;
size--;
return temp;
}
else
{
Type temp;
Node<Type> curr = first;
while(curr.getNext().getNext() != null)
curr = curr.getNext();
temp = curr.getNext().getValue();
curr.setNext(null);
size--;
return temp;
}
}
public int getSize()
{
return size;
}
public Type getFirst()
{
if(first != null)
return first.getValue();
return null;
}
public String toString()
{
String str = "";
if(size == 0)
{
str = "The list is empty";
}
else
{
Node<Type> tem = first;
str = tem.toString() + " ";
while(tem.getNext() != null)
{
str += tem.getNext().toString() + " ";
tem = tem.getNext();
}
}
return str;
}
}