-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cs
102 lines (90 loc) · 2.69 KB
/
LinkedList.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
using System;
using LinkedListNode;
// Version: 1.0
// Date: 11/15/2020
// Author: Yvonne Yeung
// How it works: When creating a new LinkedList, you must already know the first
// value of the first node, or generate a null LinkedList to
// later.
// Limitations: LinkedList only stores headNode, so users must traverse starting
// at the headNode.
// As a requirement that the LinkedList is expected to be sorted the
// only way to insert is through InsertSorted(). This method always
// looks for a place in the list to insert sorted everything sorted
// so this methods runtime worst case is O(n) where n is the is the
// size of the list.
namespace LinkedList
{
public class LinkedList
{
Node headNode;
uint sizeOfList;
public LinkedList(int val)
{
headNode = new Node(val) ;
sizeOfList = 1;
}
public uint GetSize() { return sizeOfList; }
public Node GetHeadNode() { return headNode; }
public void InsertSorted(int val)
{
Node nodeToAdd = new Node(val);
// if headNode is a smaller value than val, it remains the head.
if (headNode.GetValue() < val)
{
// Check where this new node will be inserted
Node currentNode = headNode;
if (headNode.GetNextNode() != null)
{
while (currentNode.GetNextNode() != null)
{
// Check if we insert after currentNode
if (nodeToAdd.GetValue() > currentNode.GetValue())
{
// Check if nextNode is higher
if (nodeToAdd.GetValue() > currentNode.GetNextNode().GetValue())
{
currentNode = currentNode.GetNextNode();
continue;
}
// The new node is not higher so insert after our currentNode
// Set the remaining portion of our list into the node to add
nodeToAdd.SetNextNode(currentNode.GetNextNode());
// Add the new node to the list
currentNode.SetNextNode(nodeToAdd);
sizeOfList++;
return;
}
currentNode = currentNode.GetNextNode();
}
// We've made it to the end of our list! Add the node at the end
currentNode.SetNextNode(nodeToAdd);
sizeOfList++;
}
// there is no next node on the head, so we can just add it
else
{
headNode.SetNextNode(nodeToAdd);
sizeOfList++;
}
}
// headNode is larger than value, it is no longer the head
else
{
nodeToAdd.SetNextNode(headNode);
headNode = nodeToAdd;
sizeOfList++;
}
}
public void PrintList()
{
Node currentNode = headNode;
Console.WriteLine(currentNode.GetValue());
while (currentNode.GetNextNode() != null)
{
currentNode = currentNode.GetNextNode();
Console.WriteLine(currentNode.GetValue());
}
}
}
}