-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2720_DoublyNameCardList.java
More file actions
197 lines (147 loc) · 5.49 KB
/
CSc2720_DoublyNameCardList.java
File metadata and controls
197 lines (147 loc) · 5.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Program Name: Lab 6/DoublyNameCardList
* Name: Stephanie L. Wyckoff
* Date: 2/18/2017
* Course: Data Structures Lab
* Professor: Duan
* TA: Pelin Icer, Shubin Wu
*
* Description: Constructor List class which takes in NameCard objects and indexes and adds/deletes to/from the list as needed.
*
* Input: NameCard objects and indexes
*
* Output: none.
*/
public class DoublyNameCardList {
private NameCard head;
private NameCard tail;
int count;
//adds node to end
public void addTail(NameCard nc){
System.out.println("\nAdding new node to end of list.\n");
//if the list has zero nodes, the new node is both the head and the tail so both head and tail are set to the new node
if(count == 0){
head = nc;
tail = nc;
}
//if the list has at least one node
else{
tail.setNext(nc); //make tail node point to nc as next
nc.setPrev(tail); //make former tail point back to nc as previous
tail = nc; //new node nc is now tail of list
}
count++; //increase count of nodes in list
}
//adds node to head
public void addHead(NameCard nc){
System.out.println("\nAdding new node to begining of list.\n");
//if the list has zero nodes, the new node is both the head and the tail so both head and tail are set to the new node
if(count == 0){
head = nc;
tail = nc;
}
//if list has at least one node
else{
nc.setNext(head); //make nc node point to current head node as next
head.setPrev(nc); //make current head node point to nc node as previous
head = nc; //nc is now head node
}
count++;
}
//adds new node after given index
public void add(int index, NameCard nc){
System.out.println("\nAdding new node after index: " + index + ".\n");
if(index == 0){
this.addHead(nc); //if node index is zero, call method addHead() to add nc to head of list
}
else if(index >= count){
this.addTail(nc); //if node index is equal to or greater than the total number of nodes, call method addEnd() and add nc to tail of list
}
else{ //ends with nodes in this order: current <-> nc <-> nextNC
NameCard current = this.get(index); //calls method get() with index value, sets node at this index as node current
NameCard nextNC = current.getNext(); //makes new node nextNC equal to node after node current
nc.setNext(nextNC); //make nc node point to nextNC as next
nc.setPrev(current); //make nc node point to node current as prev
current.setNext(nc); //make node current point to nc as next
nextNC.setPrev(nc); //make nextNC point to nc as prev
count++;
}
}
//get NameCard from given index
public NameCard get(int index){
if(index >= count){ //if the index provided is equal to or greater than the number of nodes in list, returns null as index because that index does not exist
return null;
}
NameCard n = head; //makes new node n, sets it at head
int i = 0;
while(i < index){ //iterates through node list until reaching the node equal to index
n = n.getNext(); //sets new node n as the node after the index
i++;
}
return n; //returns new node n at index+1
}
//counts number of nodes in list
public int size(){
return count;
}
//remove node
public boolean delete(int index){
System.out.println("\nDeleting node at index: " + index + "\n");
if(index < 0 || index >= size()){ //if index is less than or greater than or equal to number of nodes, then the node does not exist
return false;
}
NameCard current = head; //makes new node current set as head
for(int i = 0; i < index-1; i++){ //iterates through node list from 0 to index-1 similar to arrays
if(current.getNext() == null){ //if node at index-1 does not have a next then node at index does not exist
return false;
}
current = current.getNext(); //if node at index-1 does have a next, then current is set as node at index
}
current.setNext(current.getNext().getNext()); //node at index-1 points to index+1
current.setPrev(current.getPrev().getPrev()); //node at index+1 points to index-1
count--;
return true;
}
//remove head node
public void deleteHead(){
System.out.println("\nDeleting node at begining of list.\n");
if(count == 0){ //if count is zero, the list is empty or doesnt exist
System.out.println("\nThe list is empty.");
}
else{
head = head.getNext(); //head.next is now head
count--;
}
}
//delete tail node
public void deleteTail(){
System.out.println("\nDeleting node to end of list.\n");
if(count == 0){
System.out.println("\nThe list is empty.\n");
}
else if(count == 1){ //if list has only one node, call method deleteHead()
this.deleteHead();
}
else{
NameCard prevTail = tail.getPrev(); //set new node prevTail as the node before the tail
tail = prevTail; //make new node prevTail new tail
}
count--;
}
//check if node already exits
public boolean exist(NameCard nc){
System.out.println("\nChecking if node exists.\n");
NameCard check = head; //set new node check as head
for(int i = 0; i < count; i++){ //iterate through node list from zero to count-1
//if any node in list is has same name and age and company as given node, return true
if(nc.getName().equals(check.getName())){
if(nc.getAge() == check.getAge()){
if(nc.getCom().equals(check.getCom())){
return true;
}
}
}
}
return false; //if any node in list has same name or same age or same company BUT ALL ARE NOT SAME or is completely different, return false
}
}