-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.java
More file actions
72 lines (63 loc) · 2.16 KB
/
List.java
File metadata and controls
72 lines (63 loc) · 2.16 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
/** A linked list of character data objects.
* (Actually, a list of Node objects, each holding a reference to a character data object.
* However, users of this class are not aware of the Node objects. As far as they are concerned,
* the class represents a list of CharData objects. Likewise, the API of the class does not
* mention the existence of the Node objects). */
public class List {
// Points to the first node in this list
public Node first;
// The number of elements in this list
public int size;
/** Constructs an empty list. */
public List() {
// Starts with a dummy node
first = new Node(0,0);
size = 0;
}
public void insertLast(double x,double y){
Node lastOne = new Node(x,y);
Node curr = this.first;
while (curr.next!=null){
curr = curr.next;
}
curr.next = lastOne;
size++;
}
public void insertFirst(double x, double y){
Node firstInsert = new Node(x,y);
firstInsert.next = this.first.next;
this.first.next = firstInsert;
size++;
}
public void removeFirst(){
if (this.first.next==null){
System.out.println("Nothing to remove, empty list");
return;
}
this.first.next = this.first.next.next;
size--;
}
/** Returns the number of elements in this list. */
public int getSize() {
return size;
}
/** Returns an iterator over the elements in this list, starting at the given index. */
public ListIterator listIterator(int index) {
// If the list is empty, there is nothing to iterate
if (size == 0) return null;
// Gets the element in position index of this list
Node current = first.next; // skips the dummy
int i = 0;
while (i < index) {
current = current.next;
i++;
}
// Returns an iterator that starts in that element
ListIterator newIterator = new ListIterator(current);
return newIterator;
}
public ListIterator listIterator() {
Node newNode = first.next;
return new ListIterator(newNode);
}
}