-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLList.java
More file actions
151 lines (134 loc) · 4.19 KB
/
DLList.java
File metadata and controls
151 lines (134 loc) · 4.19 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
package Flawless_Feedback;
// A Node is a node in a doubly-linked list.
class DNode
{ // class for nodes in a doubly-linked list
DNode prev; // previous Node in a doubly-linked list
DNode next; // next Node in a doubly-linked list
Object data;
//public char data; // data stored in this Node
DNode()
{ // constructor for head Node
prev = this; // of an empty doubly-linked list
next = this;
data = "Default";
// data = 'H'; // not used except for printing data in list head
}
DNode(Object dataPass)
{ // constructor for a Node with data
prev = null;
next = null;
data = dataPass;
//this.data = data; // set argument data to instance variable data
}
public void append(DNode newNode)
{ // attach newNode after this Node
newNode.prev = this;
newNode.next = next;
if (next != null)
{
next.prev = newNode;
}
next = newNode;
// System.out.println("Node with data " + newNode.data
// + " appended after Node with data " + data);
}
public void insert(DNode newNode)
{ // attach newNode before this Node
newNode.prev = prev;
newNode.next = this;
prev.next = newNode;;
prev = newNode;
// System.out.println("Node with data " + newNode.data
// + " inserted before Node with data " + data);
}
public void remove()
{ // remove this Node
next.prev = prev; // bypass this Node
prev.next = next;
// System.out.println("Node with data " + data + " removed");
}
public String toString(){
return this.data + " - " + this.data;
}
}
class DLList
{
DNode head;
float nodeAvg = 0;
public DLList()
{
head = new DNode();
}
public DLList(String s1)
{
head = new DNode(s1);
}
public DNode find(String wrd1)
{ // find Node containing x
for (DNode current = head.next; current != head; current = current.next)
{
if (current.data.toString().compareToIgnoreCase(wrd1) == 0)
{ // is x contained in current Node?
// System.out.println("Data " + wrd1 + " found");
return current; // return Node containing x
}
}
// System.out.println("Data " + wrd1 + " not found");
return null;
}
//This Get method Added by Matt C
public DNode get(int i)
{
DNode current = this.head;
if (i < 0 || current == null)
{
throw new ArrayIndexOutOfBoundsException();
}
while (i > 0)
{
i--;
current = current.next;
if (current == null)
{
throw new ArrayIndexOutOfBoundsException();
}
}
return current;
}
public String toString()
{
String str = "";
if (head.next == head)
{ // list is empty, only header Node
return "List Empty";
}
str = "list content = ";
for (DNode current = head.next; current != head && current != null; current = current.next)
{
str = str + current.data;
}
return str;
}
public String print()
{ // print content of list
String msgLinkedList;
int count = 0;
if (head == null)
{ // list is empty, only header Node
msgLinkedList = "";
return msgLinkedList;
}
msgLinkedList = "HEAD <->";
for (DNode current = head.next; current != head; current = current.next)
{
if (count > 0)
{
msgLinkedList += "<-->";
}
msgLinkedList += " " + current.data;
count++;
}
msgLinkedList += "<-> TAIL";
return msgLinkedList;
}
}