-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssn2_Utility.cs
More file actions
153 lines (138 loc) · 4.72 KB
/
Copy pathAssn2_Utility.cs
File metadata and controls
153 lines (138 loc) · 4.72 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Assignment_2;
namespace Assn2
{
class Assn2_Utility
{
public static StockNode ReverseList(StockNode n)
{
StockNode head = n;
StockNode current = n;
StockNode firstNodeBeforeReverse = n; // keep track of origional FirstNode
while (true)
{
StockNode temp = current;
// keep track of currentHead in LinkedList "n", for continued access to unprocessed List
if (current.Next != null) { current = current.Next; }
temp.Next = head;
// keep track of head of Reversed List that will be returned post processing
head = temp;
if (current != null)
{
if (current.Next == null)
{
temp = current;
current.Next = head;
head = temp;
// Set the original FirstNode to NULL
firstNodeBeforeReverse.Next = null;
break;
}
}
}
return head;
}
//The main function
public static StockNode MergeSort(StockNode head)
{
if (head == null || head.Next == null)
{
return head;
}
StockNode middle = GetMiddleElement(head); //get the middle of the list
StockNode sHalf = middle.Next;
middle.Next = null; //split the list into two halfs
return Merge(MergeSort(head), MergeSort(sHalf)); //recurse on that
}
//Merge subroutine to merge two sorted lists
public static StockNode Merge(StockNode a, StockNode b)
{
StockNode dummyHead, curr;
dummyHead = new StockNode();
curr = dummyHead;
while (a != null && b != null)
{
if (a.StockHolding.Holdings <= b.StockHolding.Holdings)
{
curr.Next = a;
a = a.Next;
}
else
{
curr.Next = b;
b = b.Next;
}
curr = curr.Next;
}
curr.Next = (a == null) ? b : a;
return dummyHead.Next;
}
//Finding the middle element of the list for splitting
public static StockNode GetMiddleElement(StockNode head)
{
if (head == null)
{
return head;
}
StockNode slowNode, fastNode;
slowNode = fastNode = head;
while (fastNode.Next != null && fastNode.Next.Next != null)
{
slowNode = slowNode.Next; fastNode = fastNode.Next.Next;
}
return slowNode;
}
public static bool Compare(object object1, object object2)
{
if (object1 == null || object2 == null)
{
return false;
}
if (!object1.GetType().Equals(object2.GetType()))
{
return false;
}
Type type = object1.GetType();
if (type.IsPrimitive || typeof(string).Equals(type))
{
return object1.Equals(object2);
}
if (type.IsArray)
{
Array first = object1 as Array;
Array second = object2 as Array;
var en = first.GetEnumerator();
int i = 0;
while (en.MoveNext())
{
if (!Compare(en.Current, second.GetValue(i)))
return false;
i++;
}
}
else
{
foreach (PropertyInfo propInfo in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
{
var val = propInfo.GetValue(object1);
var tval = propInfo.GetValue(object2);
if (!Compare(val, tval))
return false;
}
foreach (FieldInfo fi in type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
{
var val = fi.GetValue(object1);
var tval = fi.GetValue(object2);
if (!Compare(val, tval))
return false;
}
}
return true;
}
}
}