/
Program.cs
126 lines (118 loc) · 3.48 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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections;
namespace LinkedListQuickSort
{
class LinkedList
{
LinkedNode? head = null;
int Length = 0;
bool IsNull = true;
public LinkedList() { }
public LinkedList(int num)
{
head = new LinkedNode(num);
IsNull = false;
Length = 1;
}
public LinkedList(LinkedNode newHead)
{
head = newHead;
IsNull = false;
Length = 1;
}
public void Add(int num)
{
if (this.IsNull) { head = new LinkedNode(num); IsNull = false; Length++; }
else
{
this.GetLast().Add(num);
Length++;
}
}
public void Add(LinkedNode node)
{
if (this.IsNull) { head = node; IsNull = false; Length++; }
else
{
this.GetLast().next = node;
Length++;
}
}
public void Add(LinkedList list)
{
if (this.IsNull) { head = list.head; IsNull = false; Length = list.Length; }
else
{
this.GetLast().next = list.head;
Length = Length +list.Length;
}
}
public LinkedNode GetLast()
{
if (this.IsNull) return null;
else {return head.GetLast();}
}
public LinkedList QuickSort()
{
if (IsNull||Length ==1) return this;
else
{
LinkedNode pivot = GetLast();
LinkedList LessThan = new LinkedList();
LinkedList GreaterThan = new LinkedList();
int index = 0;
LinkedNode currentNode = this.head;
while (index < Length - 1)
{
index++;
if (currentNode.data < pivot.data) { LessThan.Add(currentNode.data); }
else { GreaterThan.Add(currentNode.data); }
currentNode = currentNode.next;
}
LinkedList sortedLessThan = LessThan.QuickSort();
LinkedList sortedGreaterThan = GreaterThan.QuickSort();
sortedLessThan.Add(pivot);
sortedLessThan.Add(sortedGreaterThan);
return sortedLessThan;
}
}
public void PrintAll()
{
if (IsNull) { Console.WriteLine("Null List"); }
else { head.PrintAll(); }
}
}
class LinkedNode
{
public int data { get; set; }
public LinkedNode? next { get; set; } = null ;
public LinkedNode(int num)
{
data = num;
}
public LinkedNode(int num, LinkedNode newNext)
{
data = num;
next = newNext;
}
public void Add(int num)
{
this.next = new LinkedNode(num);
}
public LinkedNode GetLast()
{
if (next == null) { return this; }
else { return next.GetLast(); }
}
public void PrintAll()
{
Console.Write(data);
if (this.next != null) { next.PrintAll(); }
}
static void Main(string[] args)
{
}
}
}