-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.java
162 lines (146 loc) · 4.37 KB
/
SinglyLinkedList.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
import java.util.NoSuchElementException;
/**
* Your implementation of a Singly-Linked List.
*/
public class SinglyLinkedList<T> {
/*
* Do not add new instance variables or modify existing ones.
*/
private SinglyLinkedListNode<T> head;
private SinglyLinkedListNode<T> tail;
private int size;
/*
* Do not add a constructor.
*/
/**
* Adds the element to the front of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the front of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToFront(T data) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
if(data == null) {
throw new IllegalArgumentException("Cannot add null to SinglyLinkedList.");
}
if (head == null) {
head = new SinglyLinkedListNode<T>(data);
tail = head;
} else {
SinglyLinkedListNode<T> newNode = new SinglyLinkedListNode<>(data, head);
head = newNode;
}
size++;
}
/**
* Adds the element to the back of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the back of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToBack(T data) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
if (data == null) {
throw new IllegalArgumentException("Cannot add null to SinglyLinkedList.");
}
if (head == null){
head = new SinglyLinkedListNode<T>(data);
tail = head;
} else {
tail.setNext(new SinglyLinkedListNode<>(data));
tail = tail.getNext();
}
size++;
}
/**
* Removes and returns the first data of the list.
*
* Method should run in O(1) time.
*
* @return the data formerly located at the front of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromFront() {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
if(head == null){
throw new NoSuchElementException(" Cannot remove from an empty SinglyLinkedList");
}
T data = head.getData();
if(head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
size--;
return data;
}
/**
* Removes and returns the last data of the list.
*
* Method should run in O(n) time.
*
* @return the data formerly located at the back of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromBack() {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
if (head == null) {
throw new NoSuchElementException("Cannot remove from an empty SinglyLinkedList.");
}
T data = tail.getData();
if(head == tail) {
head = null;
tail = null;
} else {
SinglyLinkedListNode<T> cur = head;
while (cur.getNext() != null && cur.getNext().getNext() != null){
cur = cur.getNext();
}
tail =cur;
tail.setNext(null);
}
size--;
return data;
}
/**
* Returns the head node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the head of the list
*/
public SinglyLinkedListNode<T> getHead() {
// DO NOT MODIFY THIS METHOD!
return head;
}
/**
* Returns the tail node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the tail of the list
*/
public SinglyLinkedListNode<T> getTail() {
// DO NOT MODIFY THIS METHOD!
return tail;
}
/**
* Returns the size of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the size of the list
*/
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
}