-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
552 lines (451 loc) · 17.7 KB
/
Program.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using System;
using System.Collections.Generic;
namespace WassonStackNQueues
{
class Program
{
static void Main(string[] args)
{
String input = "";
Console.WriteLine("Would you like to use stack or queue?" +
"\nStack" +
"\nQueue" +
"\nHashTable" +
"\nExit");
while(input != "Exit")
{
input = Console.ReadLine().ToUpper();
switch (input)
{
case "STACK":
StackedList myStack = new StackedList();
Console.WriteLine("This program implements a stack..." +
"\nCommands:\n" +
"\n1: Push item [name]" +
"\n2: Pull next item" +
"\n3: Read next item" +
"\n4: Read all items" +
"\n0: Empty the stack" +
"\nX: Stop");
while (!input.StartsWith("X"))
{
input = Console.ReadLine().ToUpper();
if (input != null)
{
switch (input)
{
case "1":
Console.WriteLine("\nEnter new name:");
String name = Console.ReadLine();
myStack.pushToStack(name);
break;
case "2":
String pullResult = myStack.pullFromStack();
if (pullResult != null)
{
Console.WriteLine("\nItem " + pullResult + " has been pulled.");
}
else
{
Console.WriteLine("\nEmpty Stack.");
}
break;
case "3":
String readResult = myStack.pullFromStack();
if (readResult != null)
{
Console.WriteLine("\nNext item is stack: " + readResult);
}
else
{
Console.WriteLine("\nEmpty Stack.");
}
break;
case "4":
Console.WriteLine("\nItems in stack:\n");
if (myStack != null)
{
myStack.readAll();
}
else
{
Console.WriteLine("No stack.");
}
break;
case "0":
myStack.emptyStack();
break;
case "X":
Console.WriteLine("\n\nStopping...\n\n");
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}
break;
case "QUEUE":
QueueList myQueue = new QueueList();
Console.WriteLine("This program implements a queue..." +
"\nCommands:\n" +
"\n1: Enqueue item [name]" +
"\n2: Dequeue next item" +
"\n3: Read next item" +
"\n4: Read all items" +
"\n0: Empty the queue" +
"\nX: Stop");
while (!input.StartsWith("X"))
{
input = Console.ReadLine().ToUpper();
switch (input)
{
case "1":
Console.WriteLine("\nEnter new name:");
String name = Console.ReadLine();
myQueue.enqueueList(name);
break;
case "2":
String deQResult = myQueue.dequeueList();
if (deQResult != null)
{
Console.WriteLine("\nItem " + deQResult + " has been pulled.");
}
else
{
Console.WriteLine("\nEmpty Queue.");
}
break;
case "3":
String readResult = myQueue.readNext();
if (readResult != null)
{
Console.WriteLine("\nNext item is queue: " + readResult);
}
else
{
Console.WriteLine("\nEmpty Queue.");
}
break;
case "4":
Console.WriteLine("\nItems in queue:\n");
if (myQueue != null)
{
myQueue.readAll();
}
else
{
Console.WriteLine("No stack.");
}
break;
case "0":
myQueue.emptyList();
break;
case "X":
Console.WriteLine("\n\nStopping...\n\n");
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
break;
case "HASHTABLE":
HashTable<String> myHash = new HashTable<String>(10);
Console.WriteLine("This program implements a hashmap..." +
"\nCommands:\n" +
"\n1: Add item [name][key]" +
"\n2: Get value of item [key]" +
"\n3: Delete item [key]" +
"\nX: Stop");
while (!input.StartsWith("X"))
{
input = Console.ReadLine().ToUpper();
switch (input)
{
case "1":
Console.WriteLine("\nEnter new name:");
String name = Console.ReadLine();
Console.WriteLine("Enter key:");
String keyAdd = Console.ReadLine();
myHash.AddItem(keyAdd, name);
Console.WriteLine("Added " + name);
break;
case "2":
Console.WriteLine("\nEnter key:");
String keyFind = Console.ReadLine();
String hashResult = myHash.GetValue(keyFind);
if (hashResult != null)
{
Console.WriteLine("\nValue of keyed node is:\n" + hashResult);
}
else
{
Console.WriteLine("\nItem not found or empty table.");
}
break;
case "3":
Console.WriteLine("Enter key:");
String keyDelete = Console.ReadLine();
String readResult = myHash.DeleteItem(keyDelete);
if (readResult != null)
{
Console.WriteLine("\nDeleted " + readResult);
}
else
{
Console.WriteLine("\nItem not found or empty table.");
}
break;
case "X":
Console.WriteLine("\n\nStopping...\n\n");
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
break;
case "EXIT":
break;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}
}
class StackedList
{
List<String> stackedList = new List<string>();
public void pushToStack(String name)
{
stackedList.Add(name);
}
public String pullFromStack()
{
if (stackedList.Count != 0)
{
var pulledName = stackedList[stackedList.Count - 1];
stackedList.RemoveAt(stackedList.Count - 1);
return pulledName;
}
else
{
return null;
}
}
public String readNext()
{
if (stackedList != null && stackedList.Count > 0)
{
return stackedList[stackedList.Count - 1];
}
return null;
}
public void readAll()
{
int total = stackedList.Count-1;
while(total >= 0)
{
Console.WriteLine(readIndex(total));
total--;
}
}
public String readIndex(int index)
{
try
{
return stackedList[index];
}
catch (NullReferenceException)
{
return "Index was out of bounds.";
}
}
public void emptyStack()
{
Console.WriteLine("\nConfirm stack deletion (Y/N)...");
String confirmation = Console.ReadLine().ToUpper();
if (confirmation.StartsWith("Y"))
{
stackedList.RemoveRange(0, stackedList.Count);
Console.WriteLine("Confirmation accepted. Stack emptied.");
}
else
{
Console.WriteLine("\nNo confirmation. Stack retained.");
}
}
}
class QueueList
{
List<String> queueList = new List<string>();
public String enqueueList(String name)
{
try
{
queueList.Add(name);
return name;
}
catch (NullReferenceException)
{
return "";
}
}
public String dequeueList()
{
if(queueList.Count > 0)
{
try
{
String itemName = queueList[0];
queueList.RemoveAt(0);
return itemName;
}
catch (NullReferenceException)
{
return "Item not found.";
}
}
else
{
return null;
}
}
public String readNext()
{
if(queueList.Count > 0 && queueList[0] != null)
{
return queueList[0];
}
else
{
return null;
}
}
public void readAll()
{
if(queueList.Count > 0)
{
Console.WriteLine("\nQueued items:\n");
for(int i = 0; i < queueList.Count; i++)
{
Console.WriteLine(queueList[i]);
}
}
else
{
Console.WriteLine("Empty list.");
}
}
public void emptyList()
{
if(queueList.Count > 0)
{
try
{
queueList.RemoveRange(0, queueList.Count);
}
catch (NullReferenceException)
{
Console.WriteLine("Index not found.");
}
}
else
{
Console.WriteLine("Empty List.");
}
}
}
class Node<Generic>
{
public Node<Generic> next;
public String key;
public Generic value;
}
public class HashTable<Generic>
{
Node<Generic>[] buckets;
public HashTable(int size)
{
buckets = new Node<Generic>[size];
}
public Generic GetValue(String key)
{
var (prevNode, keyedNode) = FindNodeByHash(key); //Locate the node via given keys
if(keyedNode == null)
{
Console.WriteLine("Key not found.");
return default;
}
return keyedNode.value;
}
public void AddItem(String key, Generic item)
{
var newNode = new Node<Generic> //newNode has following attributes
{key = key, value = item, next = null};
int position = FindBucketByHash(key); //Locate the appropriate bucket
Node<Generic> nodeList = buckets[position]; //Start at front of bucket
if(nodeList == null) //if this list is empty, start it with this new node
{
buckets[position] = newNode;
}
else
{
while(nodeList.next != null) //if the list has nodes, traverse to the end and tack on the new node
{
nodeList = nodeList.next;
}
nodeList.next = newNode;
}
}
public Generic DeleteItem(String key)
{
var (prevNode, keyedNode) = FindNodeByHash(key); //Locate the node via given key
if(keyedNode == null)
{
Console.WriteLine("Key not found.");
}
else
{
var removedValue = keyedNode.value;
if(prevNode != null)
{
prevNode.next = null;
}
else
{
buckets[FindBucketByHash(key)] = null;
}
return removedValue;
}
return default;
}
public int FindBucketByHash(String key)
{
int hashedKey = key.GetHashCode() % buckets.Length;
if (hashedKey < 0)
{
hashedKey *= -1;
}
return hashedKey; //get the hash code of the key, this identifies the appropriate bucket
}
(Node<Generic>, Node<Generic>) FindNodeByHash(String key) //Locates node with given key value
{
int position = FindBucketByHash(key); //Locate proper bucket
Node<Generic> nodeList = buckets[position]; //List to traverse is the bucket we found
Node<Generic> prevNode = null; //Pointer to parent
while(nodeList != null) //If there are items in the list, traverse it
{
if(nodeList.key == key) //If we find the node, return it and its parent (parent helpful for deletion)
{
return (prevNode, nodeList);
}
prevNode = nodeList; //traversal
nodeList = nodeList.next;
}
return (null, null); //if not found
}
}
}