-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLinkedList1618.java
More file actions
70 lines (47 loc) · 1.48 KB
/
Copy pathTestLinkedList1618.java
File metadata and controls
70 lines (47 loc) · 1.48 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
class Person {
String firstName;
String lastName;
Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
String getLastName() {
return lastName;
}
String getFirstName() {
return firstName;
}
}
// linked list java util 1.4
class Entry {
Person element;
Entry next;
Entry previous;
Entry(Person element, Entry next, Entry previous) {
this.element = element;
this.next = next;
this.previous = previous; }
}
class LinkedList1618 {
Entry header = new Entry(null, null, null); int size;
/ * Constructs an empty Linked List. * / LinkedList1618() {
header.next = header;
header.previous = header; }
/ * Returns the last Element in this List. * /
Person getLast() {
if (size == 0) throw new java.util.NoSuchElementException();
return header.previous.element;
}
/ * Removes and returns the last Element from this List. * /
Person removeLast() {
Entry lastEntry = header.previous;
if (lastEntry == header) throw new java.util.NoSuchElementException();
lastEntry.previous.next = lastEntry.next;
lastEntry.next.previous = lastEntry.previous;
size--;
return lastEntry.element; }
/ * Appends the given element to the end of this List. * /
void addLast(Person p) { Entry newEntry = new Entry(p, header, header.previous); header.previous.next = newEntry; header.previous = newEntry; size++; }
/ * Returns the number of elements in this List. * /
int size() { return size; }
}