-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
executable file
·180 lines (162 loc) · 3.8 KB
/
DoublyLinkedList.java
File metadata and controls
executable file
·180 lines (162 loc) · 3.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Librarian Assistant Pro - Version 1.7
* Class: DoublyLinkedList
* Duties:
* The instance of this class is a list of ListNodes
* this class enables us to store information in a list
* Date: September 2008
*/
import java.util.NoSuchElementException;
class DoublyLinkedList
{
//Private fields: head and tail
private ListNode head;
private ListNode tail;
int length;
//No-arg constructor for making a DoublyLinkedList
public DoublyLinkedList()
{
head = null;
tail = null;
length = 0;
}
//isEmpty method should return true if there is no object in the linked list
public boolean isEmpty()
{
if(head == null)
return true;
else
return false;
}
public int size()
{
return length;
}
//getFirst method returns the last object in the linked list
public Object getFirst()
throws NoSuchElementException
{
if(head != null)
return head.getValue(); //Returns the last object on the list
else
throw new NoSuchElementException();
}
//getLast method returns the last object in the linked list
public Object getLast()
throws NoSuchElementException
{
if(tail != null)
return tail.getValue(); //Returns the last object on the list
else
throw new NoSuchElementException();
}
public ListNode get(int elementNum)
{
ListNode temp = head;
for(int i = 0; i < elementNum; i++)
{
if(i == elementNum - 1)
return temp;
else
temp = temp.getNext();
}
return null;
}
//add method adds a new object at the end of the linked list and return true if successful
public void add(Object obj)
{
ListNode node = new ListNode(obj, null, null);
if(head != null)
{
// System.out.println("Here is DLLC. head is not null!");
ListNode newNode = new ListNode (obj,null,head);
head.setPrevious(newNode);
newNode.setNext(head);
head = newNode;
}
else
{
// System.out.println("Here is DLLC. head is freakin null!");
head = node;
tail = node;
}
length++;
}
//remove method gets the last elemnt of the linkedlist and returns it
public Object removeLast()
throws NoSuchElementException
{
Object returnObj;
ListNode tempNode;
if(head != null)
{
tempNode = head;
returnObj = tail.getValue(); //Storing the object that has to be removed
while(tempNode.getNext() != null)
{
if(!tempNode.getNext().getValue().equals(tail.getValue())) //If the two are not equal
tempNode = tempNode.getNext();
else
{
tempNode.setNext(null);
tail = tempNode;
return returnObj;
}
}
return null;
}
else
throw new NoSuchElementException();
}
public Object removeObj(Object obj)
{
Object returnObj;
ListNode tempNode;
ListNode test = null;
if(head != null)
{
tempNode = head;
while(tempNode.getNext() != null)
{
if(tempNode.getNext().getValue().equals(obj)) //If the two are equal
{
returnObj = tempNode.getValue();
tempNode.getPrevious().setNext(tempNode.getNext());
tempNode.getNext().setPrevious(tempNode.getPrevious());
return returnObj;
}
else
{
tempNode = tempNode.getNext();
}
}
return null;
}
else
throw new NoSuchElementException();
}
//Appends otherList at the end of this list.
public void append(DoublyLinkedList otherList)
{
if(this.tail != null && otherList.head != null)
{
this.tail.setNext(otherList.head);
}
length = length + otherList.length;
}
public String toString()
{
String str = "";
ListNode temp = head;
int flag = 0;
while(temp != null)
{
if(flag == 0)
str += temp.getValue();
else
str += ", " + temp.getValue();
temp = temp.getNext();
flag++;
}
return str;
}
}