-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPList.java
More file actions
120 lines (98 loc) · 2.32 KB
/
PList.java
File metadata and controls
120 lines (98 loc) · 2.32 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
//PList is modified DList
//package dict;
import java.io.Serializable;
public class PList implements Serializable{
protected int size = 0;
protected PListNode head;
protected Object type = null;
private static final long serialVersionUID = 2L;
public PList(){
this.head = new PListNode(null, null, null);
this.head.next = head;
this.head.prev = head;
this.type = "";
this.size = 0;
}
public PList(Object myType){
this.head = new PListNode(null, null, null);
this.head.next = head;
this.head.prev = head;
this.type = myType;
this.size = 0;
}
public void insert(Object x){
PListNode duplicate = exists(x);
if(duplicate != null){
remove(duplicate);
}
PListNode temp = this.head.next;
PListNode object = new PListNode(x, head, temp);
this.head.next = object;
temp.prev = object;
this.size++;
}
public void remove(PListNode x){
PListNode xNext = x.next;
PListNode xPrev = x.prev;
x.next = null;
x.prev = null;
xNext.prev = xPrev;
xPrev.next = xNext;
this.size--;
}
public PListNode exists(Object x){
PListNode reference = this.head.next;
while(reference != this.head){
if(!reference.item.equals(x)){
reference = reference.next;
}else{
return reference;
}
}
return null;
}
public Object queryType(){
return this.type;
}
public int length(){
return this.size;
}
public PListNode front(){
return this.head.next;
}
public PListNode back(){
return this.head.prev;
}
public PListNode head(){
return this.head.next;
}
public String toString() {
String result = "[ ";
PListNode current = this.head.next;
while (current != this.head) {
result = result + current.item + " ";
current = current.next;
}
return result + "]";
}
/*
public static void main(String[] argv) {
PList l = new PList("thisType");
l.insert("1");
l.insert("2");
l.insert("3");
System.out.println("l a list of 3 elements: " + l.toString());
l.insert("2");
System.out.println(l.toString());
System.out.println(l.queryType());
System.out.println(l.front().item());
PList emptyList= new PList();
if(emptyList.head == null){
System.out.println("what");
}
if(emptyList.front() != null){
System.out.println("what1");
}
}
*/
}