-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstNameListManip.cs
223 lines (183 loc) · 6.75 KB
/
FirstNameListManip.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedList
{
class FirstNameListManip
{
public Node headFirstName;
private String firstName;
private String lastName;
public FirstNameListManip()
{
headFirstName = null;
}
public String manipTheList(char choice, String firstn, String lastn)
{
firstName = firstn;
lastName = lastn;
Node foundNode;
switch (choice)
{
case '0':
System.Environment.Exit(-1);
break;
case '1'://add a node
addNode();
return firstName + " added";
case '2'://locate a node
if (null == headFirstName)
return "empty list, nothing to find";
foundNode = findNode(firstName);
if (null == foundNode)
return firstName + " is not on the list";
else
return "found the node that contains " + firstName + " and its name is " + foundNode.getFirstName();
case '3'://remove a node
if (null == headFirstName)
return "empty list";
foundNode = findNode(firstName);
if (null == foundNode)
return firstName + " is not on the list";
else
{
removeNode(foundNode);
return firstName + " has been removed";
}
case '4'://display all nodes
return showAllNodes();
default:
break;
}
return null;
}//end ListManip()
private char menu()
{
String choice;
Console.WriteLine("0 = exit\n1 = addNode\n2 = find which node contains a particular name\n3 = remove a node\n4 = show all nodes");
choice = Console.ReadLine();
return Convert.ToChar(choice);
}//end menu()
public void addNode()
{
Node temp;
temp = new Node();
temp = initializeNode(temp);
if (null == headFirstName)
headFirstName = temp;
else
insertNode(temp);
}//end addNode()
private Node findNode(String findItem)
{
Node current = headFirstName;
while (current.getFirstName().CompareTo(findItem) != 0)//names don't match
{
if (null == current.nextFirstName)//reached the end of the list
return null;//findItem is not in the list
current = current.nextFirstName;//found the match
}
return current;
}//end findNode()
private String chooseNode(String message)
{
if (null == headFirstName)
return null;
Console.WriteLine("\n" + message);
showAllNodes();
Console.Write("> ");
return Console.ReadLine();
}//end chooseNode
private Node initializeNode(Node temp)
{
temp.setFirstName(firstName);
temp.setLastName(lastName);
return temp;
}//end initializeNode()
private void insertNode(Node temp)
{
Node current = headFirstName;
Node ip = findInsertionPoint(temp);
//new node goes before head
if (null == ip)
{
temp.nextFirstName = headFirstName;
headFirstName.prevFirstName = temp;
headFirstName = temp;
return;
}
//new node goes in the middle or after the end
temp.prevFirstName = ip;
temp.nextFirstName = ip.nextFirstName;
if (ip.nextFirstName != null)//general case where temp does not go after last node
ip.nextFirstName.prevFirstName = temp;
ip.nextFirstName = temp;
return;
}//end insertNode()
private Node findInsertionPoint(Node temp)
{
//define Insertion Point as the node after which temp should be inserted
Node current = headFirstName;
if (headFirstName.getFirstName().CompareTo(temp.getFirstName()) > 0)
return null;
while (current.getFirstName().CompareTo(temp.getFirstName()) < 0)
{
if (null == current.nextFirstName)
return current;
else
current = current.nextFirstName;
}
return current.prevFirstName;
}//end findInsertionPoint()
private void removeNode(Node foundNode)
{
if (foundNode == headFirstName)
{
if (null == headFirstName.nextFirstName)
{
headFirstName = null;
}
else
{
headFirstName.nextFirstName.prevFirstName = null;
headFirstName = headFirstName.nextFirstName;
}
}
else if (null == foundNode.nextFirstName)//foundNode is last node
{
foundNode.prevFirstName.nextFirstName = null;
}
else //foundNode is in the middle
{
foundNode.prevFirstName.nextFirstName = foundNode.nextFirstName;
foundNode.nextFirstName.prevFirstName = foundNode.prevFirstName;
}
foundNode = null;
}//end removeNode()
private Node findLastNode()
{
Node current = headFirstName;
do
{
if (current.nextFirstName == null)
return current;
current = current.nextFirstName;
} while (true);
}//end findLastNode()
public String showAllNodes()
{
String nodeList = "";
Node current = headFirstName;
if (null == headFirstName)
return "there are no nodes on this list";
while (current != null)
{
nodeList += current.getFirstName() + "\n";
current = current.nextFirstName;
}
return nodeList;
}//end showAllNodes
}
}