This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedList.java
137 lines (116 loc) · 2.99 KB
/
DoubleLinkedList.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
/*
* Name: Adam Kaplan
*
* NetID: akaplan6
*
* Project: #2
*
* Lab Section: TR 4:50PM - 6:05PM (I switched my lab section)
*
* TA: Vishnu Chittaci
*
* I affirm that I have not given or received any unauthorized help on this assignment, and that this work is my own.
*/
public class DoubleLinkedList<T> implements DoublyLinkedList<T>{
private DoubleNode<T> first;
private DoubleNode<T> last;
// Constructor
public DoubleLinkedList(){
first = new DoubleNode<T>(null, last, null);
last = new DoubleNode<T>(null, null, first);
}
@Override
// This methods appends data to the list
// Runtime: O(n)
public void insert(T data) {
DoubleNode<T> node = new DoubleNode<T>(data, last, last.previous);
if(!this.contains(data)){
last.previous.next = node;
last.previous = node;
}
}
// NEW METHOD
// Special method for the Queue to insert the new element at the end of the list.
// Runtime: O(1)
public void insertLast(T data){
DoubleNode<T> node = new DoubleNode<T>(data, last, last.previous);
last.previous.next = node;
last.previous = node;
}
// NEW METHOD
// Special method for the Queue to always delete the first term in the list.
// Runtime: O(1)
public T deleteFirst(){
DoubleNode<T> node = first.next;
first.next.next.previous = first;
first.next = first.next.next;
return node.data;
}
// NEW METHOD
// Special method for the Queue to always return the first item in the list.
// Runtime: O(1)
public T lookupFirst(){
return first.next.data;
}
@Override
// This method deletes an element from the list
public void delete(T data) {
DoubleNode<T> current = first;
while(current != last){
if(current.data == data){
current.previous.next = current.next;
if(current.next != null){
current.next.previous = current.previous;
}
}
current = current.next;
}
}
@Override
// This method tells you if an element is in the list
public boolean contains(T data) {
DoubleNode<T> current = first;
while(current != null){
if(current.data == data)
return true;
current = current.next;
}
return false;
}
@Override
// This method looks up an element in the list and returns it
public T lookup(T data) {
DoubleNode<T> current = first;
while(current != last){
if(current.data == data)
return current.data;
current = current.next;
}
return null;
}
@Override
// This method tells you if the list is empty
public boolean isEmpty() {
return last.previous == first;
}
@Override
// This method prints the list in the right order
// Runtime: O(n)
public void printList() {
DoubleNode<T> current = first.next;
while(current != last){
System.out.print(current.data.toString());
current = current.next;
}
}
@Override
// This method prints the list in reverse order
// Runtime: O(n)
public void printListRev() {
DoubleNode<T> current = last.previous;
while(current != first){
System.out.print(current.data.toString());
current = current.previous;
}
}
}