-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDLinkedList.java
More file actions
133 lines (131 loc) · 3.49 KB
/
DDLinkedList.java
File metadata and controls
133 lines (131 loc) · 3.49 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
/**
*this class implements the state and behaviour of a basic
*doubly linked list ifrastructure of integers
*/
public class DDLinkedList<T>{
private ListElement<T> head,tail;
public ListElement<T> getHead(){
return this.head;
}
public ListElement<T> getTail(){
return this.tail;
}
/**
*Indicates whether or not this list is empty.
*@return true if the list is empty, false otherwise
*/
public boolean isEmpty(){
if(head==null && tail==null){
return true;
}
else {
return false;
}
}
/**
*Add an element holding the T val to the head of the doubly-
*linked-list.
*@param val the T value of the new head object in the list.
*/
protected void addToHead(T val){
ListElement<T> temp=new ListElement<>(val,head);
if (this.isEmpty()){ //list is empty
tail=temp;
}
else { //there are objects in the list
head.setPrev(temp);
}
head=temp;
}
/**
*Add an element holding the T val to the tail of the doubly-
*linked-list.
*@param val the T value of the new tail object in the list.
*/
protected void addToTail(T val){
ListElement<T> temp=new ListElement<>(val);
if(this.isEmpty()){ //list is empty
head=temp;
}
else { //there are objects in the list
tail.setNext(temp);
}
tail=temp;
}
/**
*remove the head object that's in the list and sets the new head obj,
*it delets the reference so the garbage collector will dispose it.
*@return the value of the removed tail object.
*/
protected T removeFromHead(){
T removedVal=head.getVal();
head=head.getNext();
if(head==null){ //only head exists
tail=head;
}
else { //there is more then just the head in the list
head.setPrev(null);
}
return removedVal;
}
/**
*remove the tail object that's in the list and sets the new tail obj,
*it delets the reference so the garbage collector will dispose it.
*@return the value of the removed tail object.
*/
protected T removeFromTail(){
T removedVal=tail.getVal();
tail=tail.getPrev();
if(tail==null){ // if tail was head
head=tail;
}
else { //there are 2 objects and tail needs to be removed
tail.setNext(null);
}
return removedVal;
}
/**
*Add to the list an already allocated element, before a given element.
*@param newElm is the allocated element to be added to the list.
*@param beforeElm is the existing element in the list to add newElm before it.
*/
protected void addElm(ListElement<T> newElm, ListElement<T> beforeElm){ //need to double check the method
if(this.isEmpty()){
System.out.println("addElm::List is Empty!");
return;
}
if(beforeElm==head){
newElm.setNext(head);
head.setPrev(newElm);
head=newElm;
return;
}
else if(beforeElm==null){
tail.setNext(newElm);
newElm.setPrev(tail);
tail=newElm;
return;
}
newElm.setNext(beforeElm);
newElm.setPrev(beforeElm.getPrev());
beforeElm.getPrev().setNext(newElm);
beforeElm.setPrev(newElm);
}
/**
*This method overrides the deafault method toString from the Object class.
*It displays the entire Stack/Queue's items respectively.
*@return disp the entire Stack/Queue's items respectively in a String.
*/
@Override
public String toString()
{
ListElement<T> temp=this.getHead();
if(temp==null) return " ";
String disp="";
while(temp!=null){ //needs to be enhanced for loop
disp+=temp.getVal()+" ";
temp=temp.getNext();
}
return disp;
}
}