-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLinkedList.java
More file actions
144 lines (125 loc) · 3.27 KB
/
SimpleLinkedList.java
File metadata and controls
144 lines (125 loc) · 3.27 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
import java.util.NoSuchElementException;
/**
* A simple linked list of strings with limited functionality. Items may only be
* added and removed at the beginning of the list.
*
* @author Farzana Rahman
*
* @Edited by Maricel Vicente
*
*/
public class SimpleLinkedList
{
private int size;
private Node head;
/**
* Create an empty linked list.
*/
public SimpleLinkedList()
{
size = 0;
head = null;
}
/**
* @return The number of elements stored in the list
*/
public int size()
{
return size;
}
/**
* Add an item at position 0 in the list.
*
* @param item The item to add
*/
public void addAtStart(String item)
{
head = new Node(head, item);
size++;
}
/**
* Return true if this list contains the indicated item.
*
* @param item The item to search for
* @return true if the item is in the list, false otherwise
*/
public boolean contains(String item)
{
// COMPLETE THIS
// UNFINISHED. Look at the get method if you need a reminder on how to
// loop through a linked structure. For this method, a while loop will
// be more appropriate than a for loop.
//
// Note: This method should NOT CALL get. The resulting implementation
// would be very inefficient since get must loop through the list from
// the beginning on each call.
Node current = head;
while (current != null) {
if (current.getValue() == item) {
return true;
}
else {
return false;
}
}
throw new UnsupportedOperationException();
}
/**
* Add an item to the end of the list.
*
* @param item The item to add
*/
public void addAtEnd(String item)
{
// COMPLETE THIS
// UNFINISHED. There are two cases: if the list is currently empty then
// the new node will be the head. Otherwise the new node needs to go
// after the current last node.
Node current = head;
if (head == null) {
head = new Node(head, item);
}
while (current != null) {
current = current.getNext();
}
current = new Node(head, item);
throw new UnsupportedOperationException();
}
/**
* Remove and return the item stored at position 0 in the list.
*
* @return The item at position 0
* @throws NoSuchElementException if the list is empty
*/
public String removeAtStart()
{
if (size == 0)
{
throw new NoSuchElementException();
}
String result;
result = head.getValue();
head = head.getNext();
size--;
return result;
}
/**
* Return the item stored at the indicated index.
*
* @param index The index
* @return The element stored at the indicated index
*/
public String get(int index)
{
if (index >= size || index < 0)
{
throw new IndexOutOfBoundsException();
}
Node curNode = head;
for (int i = 0; i < index; i++)
{
curNode = curNode.getNext();
}
return curNode.getValue();
}
}