-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
164 lines (149 loc) · 2.97 KB
/
Copy pathLinkedList.java
File metadata and controls
164 lines (149 loc) · 2.97 KB
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
/**
* Implementation of Singly Linked List
*
*/
public class LinkedList<E> {
Node head;
public class Node {
private E contents;
private Node next;
/**
* Constructs a new Node
*/
public Node(E obj) {
contents = obj;
}
public E getContents() {
return contents;
}
public Node getNext() {
return next;
}
}
/**
* Constructs an empty LinkedList
*/
public LinkedList() {}
/**
* Constructs an empty LinkedList with one object
*/
public LinkedList(E obj) {
head = new Node(obj);
}
/**
* Adds to the end of the LinkedList
*/
public boolean add(E e) {
Node toAdd = new Node(e);
if(head == null) {
head = toAdd;
return true;
}
Node n = head;
while(n.next != null) {
n = n.next;
}
n.next = toAdd;
return true;
}
/**
* Inserts the element at the specified index
*/
public void add(int index, E element) {
Node toAdd = new Node(element);
Node temp = head;
if(index == 0) {
head = toAdd;
head.next = temp;
return;
}
for(int i = 1; i < index; i++) {
if(temp.next != null) {
temp = temp.next;
}
else return;
}
Node otherTemp = temp.next;
temp.next = toAdd;
toAdd.next = otherTemp;
}
/**
* Removes all elements from the LinkedList
*/
public void clear() {
head = null;
}
/**
* Checks if the LinkedList contains the specified element
*/
public boolean contains(Object o) {
Node n = head;
while(n != null) {
if(n.contents.equals(o)) return true;
if(n.next != null) n = n.next;
else return false;
}
return false;
}
/**
* Returns the number of nodes in the LinkedList
*/
public int size() {
Node n = head;
int count = 0;
while(n != null) {
count++;
n = n.next;
}
return count;
}
/**
* Removes an element at the given index; returns the contents of the node removed
*/
public E remove(int ind) {
Node n = head;
if(ind == 0) {
E obj = head.contents;
head = null;
return obj;
}
for(int i = 0; i < ind-1; i++) {
n = n.next;
}
Node after = n.next.next;
Node toReturn = n.next;
n.next = after;
return toReturn.contents;
}
/**
* Removes the first node in the Linked List containing an object, returns
* true if a node is removed, false otherwise
*/
public boolean remove(E obj) {
Node n = head;
while(n != null) {
if(n.getContents().equals(obj)) {
return true;
}
n = n.next;
}
return false;
}
public String toString() {
String s = "[";
Node h = head;
while(h != null) {
s += h.contents + ", ";
h = h.next;
}
s = s.substring(0, s.length()-2);
s += "]";
return s;
}
/**
* Returns the head of the LinkedList (used in testing)
*/
public Node getHead() {
return head;
}
}