Uploaded by טום יורייב

Utility Functinons

advertisement
Basic Functions
CreateRndArr(int length)
CreateRndArr(int length, int min, int max)
CreateArr()
CleanString(string str)
ToLowerCase(string str)
ToUpperCase(string str)
BiggestToSmallest(int[] arr)
SmallestToBiggest(int[] arr)
BiggestToSmallest(int[] arr, int start, int end)
SmallestToBiggest(int[] arr, int start, int end)
PrintArr(int[] arr)
ArrAvg(int[] arr)
ExpandArrInt(int[] arr, int num)
RandomNum(int minimum, int maximum)
IsInArray(int[] arr, int value)
MaxNum(int[] arr)
MaxValueIndex(int[] arr)
MinNum(int[] arr)
MinValueIndex(int[] arr)
CopyIntArr(int[] arr)
OneValueArray(int length, int value)
Recursive
Factorial(int n)
Power(int x, int y)
Odd(int n)
Even(int n)
Fibonachi(int n)
Polyndrom(int[] arr, int start, int end)
ChangeNum(int[] arr, int start, int end)
Doubling(int[] arr)
CheckIfInArr(int[] arr, int num)
EvenNum(int[] arr)
OneNum(int[] arr)
Maximum(int[] arr)
BiggerThenNumber(int[] arr, int num)
Linked list (node)
Build<T>(T[] arr)
BuildSorted(int[] arr)
GenerateList(int length, int min, int max)
AddToList<T>(Node<T> node, T value)
AddToList<T>(Node<T> node, Node<T> pos, T value)
AddToSortedList(Node<int> node, int value)
RemoveFromList(Node<int> node, int value)
RemoveFromList<T>(Node<T> node, Node<T> pos)
2
2
2
2
2
2
3
3
3
3
4
4
4
4
4
4
4
4
5
5
5
5
5
5
6
6
6
6
6
6
7
7
7
7
7
7
8
8
8
8
9
9
Linked list (node)
GetLast<T>(Node<T> node)
GetPrevious<T>(Node<T> node, Node<T> pos)
GetPosition<T>(Node<T> node, int index)
IsIn(Node<int> node, int value)
Find(Node<int> node, int value)
ListLength<T>(Node<T> node)
SumList(Node<int> node)
ListMax(Node<int> node)
ListMin(Node<int> node)
CountValue(Node<int> node, int value)
IsAscending(Node<int> node)
IsDescending(Node<int> node)
MergeLists(Node<int> node1, Node<int> node2)
ReverseList<T>(Node<T> node)
AddToEndOfList<T>(Node<T> node, Node<T> value)
UpOne(Node<int> first)
EvenNum(Node<int> first)
FollowNum(Node<int> first)
Avg(Node<int> first)
AddOneToAll(Node<int> first)
MakePositive(Node<int> first)
ReverseNode(Node<string> first)
GetNegative(Node<double> first)
DeleteEven(Node<int> first)
DeleteNegative(Node<int> first)
DeleteDuplicated(Node<int> first)
DeleteNum(Node<int> first, int num)
HowMany(Node<int> first, int num)
PrintBetweenTwoNumbers(Node<int> first, int m, int n)
MaxSum(Node<int> first)
LotteryNum()
Queue
CreateQueue(int[] values)
CreateQueue(int length, int minimum, int maximum)
Duplicate<T>(Queue<T> queue)
SumQueue(Queue<int> queue)
LengthQueue<T>(Queue<T> queue)
AverageQueue(Queue<int> queue)
MaxValueQueue(Queue<int> queue)
MinValueQueue(Queue<int> queue)
IsInQueue(Queue<int> queue, int value)
ReverseQueue<T>(Queue<T> queue)
1
9
9
10
10
10
10
10
10
11
11
11
11
12
12
12
13
13
13
13
13
14
14
14
14
15
15
15
15
16
16
16
16
17
17
17
17
17
17
18
18
18
// BASIC FUNCTIONS
// create an array with a given length and values
between -10 to 10
public static int[] CreateRndArr(int length) {
int[] arr = new int[length];
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++) {
arr[i] = rnd.Next(-10, 10);
}
return arr;
}
// create an array with a given length and a given range
for the values
public static int[] CreateRndArr(int length, int min, int
max) {
int[] arr = new int[length];
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++) {
arr[i] = rnd.Next(min, max);
}
return arr;
}
// create an array with a length and values given by the
user
public static int[] CreateArr() {
Console.WriteLine("Please enter the array length");
int length = int.Parse(Console.ReadLine());
int[] arr = new int[length];
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("please enter a number");
arr[i] = int.Parse(Console.ReadLine());
}
return arr;
}
// remove special characters from a string, return a
string that contain only letters
public static string CleanString(string str) {
string cleanStr = "";
for (int i = 0; i < str.Length; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
cleanStr += str[i];
}
else if (str[i] >= 'a' && str[i] <= 'z') {
cleanStr += str[i];
}
}
return cleanStr;
}
// convert the letters in the given string to lower case
public static string ToLowerCase(string str) {
string lowerCase = "";
for (int i = 0; i < str.Length; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
lowerCase += str[i] + 32;
}
else {
lowerCase += str[i];
}
}
return lowerCase;
}
// convert the letters in the given string to upper case
public static string ToUpperCase(string str) {
string upperCase = "";
for (int i = 0; i < str.Length; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
upperCase += str[i] - 32;
}
else {
upperCase += str[i];
}
}
return upperCase;
}
2
// sort the given array (sort: highest value to lowest
value)
public static int[] BiggestToSmallest(int[] arr) {
int Temp;
int PlaceHolder1 = arr.Length - 1;
int PlaceHolder2 = 0;
for (int x = 0; x < arr.Length - 1; x++) {
while (PlaceHolder2 < PlaceHolder1) {
if (arr[PlaceHolder2] < arr[PlaceHolder1]) {
Temp = arr[PlaceHolder1];
arr[PlaceHolder1] = arr[PlaceHolder2];
arr[PlaceHolder2] = Temp;
}
PlaceHolder2++;
}
PlaceHolder1--;
PlaceHolder2 = 0;
}
return arr;
}
// sort the given array (sort: lowest value to highest
value)
public static int[] SmallestToBiggest(int[] arr) {
int Temp;
int PlaceHolder1 = arr.Length - 1;
int PlaceHolder2 = 0;
for (int x = 0; x < arr.Length - 1; x++) {
while (PlaceHolder2 < PlaceHolder1) {
if (arr[PlaceHolder2] > arr[PlaceHolder1]) {
Temp = arr[PlaceHolder1];
arr[PlaceHolder1] = arr[PlaceHolder2];
arr[PlaceHolder2] = Temp;
}
PlaceHolder2++;
}
PlaceHolder1--;
PlaceHolder2 = 0;
}
return arr;
}
// sort the given array between the given range (sort:
highest value to lowest value)
public static int[] BiggestToSmallest(int[] arr, int start,
int last) {
int Temp;
int PlaceHolder1 = last;
int PlaceHolder2 = start;
for (int x = 0; x < arr.Length - 1; x++) {
while (PlaceHolder2 < PlaceHolder1) {
if (arr[PlaceHolder2] < arr[PlaceHolder1]) {
Temp = arr[PlaceHolder1];
arr[PlaceHolder1] = arr[PlaceHolder2];
arr[PlaceHolder2] = Temp;
}
PlaceHolder2++;
}
PlaceHolder1--;
PlaceHolder2 = 0;
}
return arr;
}
// sorted the given array between the given range (sort:
lowest value to highest value)
public static int[] SmallestToBiggest(int[] arr, int start, int
last) {
int Temp;
int PlaceHolder1 = last;
int PlaceHolder2 = start;
for (int x = 0; x < arr.Length - 1; x++) {
while (PlaceHolder2 < PlaceHolder1) {
if (arr[PlaceHolder2] > arr[PlaceHolder1]) {
Temp = arr[PlaceHolder1];
arr[PlaceHolder1] = arr[PlaceHolder2];
arr[PlaceHolder2] = Temp;
}
PlaceHolder2++;
}
PlaceHolder1--;
PlaceHolder2 = 0;
}
return arr;
}
3
// print the given array
public static void PrintArr(int[] arr) {
for (int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + ", ");
}
}
// calculate the given array average
public static double ArrAvg(int[] arr) {
double sum = 0;
for (int i = 0; i < arr.Length; i++) {
sum += arr[i];
}
return sum / arr.Length;
}
// add a value to the end of the given array
public static int[] ExpandArrInt(int[] arr, int num) {
int[] newArr = new int[arr.Length + 1];
for (int i = 0; i < arr.Length; i++) {
newArr[i] = arr[i];
}
newArr[newArr.Length - 1] = num;
return newArr;
}
// return a random number between the given range
public static int RandomNum(int minimum, int
maximum) {
Random rnd = new Random();
return rnd.Next(minimum, maximum);
}
// check if a certain value appears in the given array,
return true if it is
public static bool IsInArray(int[] arr, int value) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == value) {
return true;
}
}
return false;
}
// return the maximum value in the given array
public static int MaxNum(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.Length; i++) {
max = Math.Max(max, arr[i]);
}
return max;
}
// return the maximum value in the given array index
public static int MaxValueIndex(int[] arr) {
int maxValue = arr[0];
int maxValueIndex = 0;
for (int i = 1; i < arr.Length; i++) {
if (maxValue > arr[i]) {
maxValue = arr[i];
maxValueIndex = i;
}
}
return maxValueIndex;
}
// return the minimum value in the given array
public static int MinNum(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.Length; i++) {
min = Math.Min(min, arr[i]);
}
return min;
}
4
// return the minimum value in the given array index
public static int MinValueIndex(int[] arr) {
int minValue = arr[0];
int minValueIndex = 0;
for (int i = 1; i < arr.Length; i++) {
if (minValue > arr[i]) {
minValue = arr[i];
minValueIndex = i;
}
}
return minValueIndex;
}
// duplicate an int array
public static int[] CopyIntArr(int[] arr) {
int[] duplicateArr = new int[arr.Length];
for (int i = 0; i < arr.Length; i++) {
duplicateArr[i] = arr[i];
}
return duplicateArr;
}
// create an array that contains the same value in every
cell
public static int[] OneValueArray(int length, int value) {
int[] arr = new int[length];
for (int i = 0; i < arr.Length; i++) {
arr[i] = value;
}
return arr;
}
// RECURSIVE
// calculate the power of x^y
public static int Power(int x, int y) {
if (y == 0) {
return 1;
}
return x * Power(x, y - 1);
}
// calculate the sum of every odd number between 1 to
n
public static int Odd(int n) {
if (n % 2 == 0) {
n--;
}
if (n == 1) {
return 1;
}
return n * Odd(n - 2);
}
// calculate the factorial of n
public static int Factorial(int n) {
if (n == 1) {
return 1;
}
return n * Factorial(n - 1);
}
5
// calculate the sum of every even number between 1
to n
public static int Even(int n) {
if (n % 2 == 1) {
n--;
}
if (n == 2) {
return 2;
}
return n * Even(n - 2);
}
// calculate the Fibonacci until the n number
public static int Fibonacci(int n) {
if (n == 1 || n == 2) {
return 1;
}
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
// check if an array is palindromic [1,2,3,2,1]
public static bool palindromic(int[] arr, int start, int
end) {
if (arr[start] != arr[end]) {
return false;
}
if (start >= end) {
return true;
}
return palindromic(arr, start + 1, end - 1);
}
// change the order of the values in the array
public static void ChangeNum(int[] arr, int start, int end)
{
if (start < end) {
int x = arr[start];
arr[start] = arr[end];
arr[end] = x;
ChangeNum(arr, start + 1, end - 1);
}
}
// multiply all of the odd numbers
public static int Doubling(int[] arr) {
return Doubling(arr, 0);
}
// check if a value is in the array
public static bool CheckIfInArr(int[] arr, int num) {
return CheckIfInArr(arr, num, 0);
}
// helper function for Doubling
public static int Doubling(int[] arr, int i) {
if (i > arr.Length) {
return 1;
}
if (arr[i] % 2 == 1) {
return Doubling(arr, i + 1) * arr[i];
}
return Doubling(arr, i + 1);
}
// helper function for CheckIfInArr
public static bool CheckIfInArr(int[] arr, int num, int i) {
if (i == arr.Length) {
return false;
}
if (arr[i] == num) {
return true;
}
return CheckIfInArr(arr, num, i + 1);
}
6
// check if the array has only even numbers
public static bool EvenNum(int[] arr) {
return EvenNum(arr, 0);
}
// check if there is at least on even number in the array
public static bool OneNum(int[] arr) {
return OneNum(arr, 0);
}
// helper function for EvenNum
public static bool EvenNum(int[] arr, int i) {
if (i == arr.Length) {
return true;
}
if (arr[i] % 2 == 1) {
return false;
}
return EvenNum(arr, i + 1);
}
// helper function for OneNum
public static bool OneNum(int[] arr, int i) {
if (i == arr.Length) {
return false;
}
if (arr[i] % 2 == 0) {
return true;
}
return OneNum(arr, i + 1);
}
// return the maximum value in the array
public static int Maximum(int[] arr) {
return Maximum(arr, 0);
}
// helper function for Maximum
public static int Maximum(int[] arr, int i) {
if (i == arr.Length - 1) {
return arr[i];
}
return Math.Max(arr[i], Maximum(arr, i + 1));
}
// check how many numbers in the array are bigger then
num
public static void BiggerThenNumber(int[] arr, int num) {
BigerThenNumber(arr, num, 0);
}
// helper function for BiggerThenNumber
public static void BiggerThenNumber(int[] arr, int num,
int i) {
if (i != arr.Length) {
if (arr[i] > num) {
Console.Write(arr[i]);
}
BiggerThenNumber(arr, num, i + 1);
}
}
// LINKED LIST (NODE)
// build a node from a sorted array
public static Node<int> BuildSorted(int[] arr) {
int[] sortedArr = SmallestToBiggest(arr);
return Build(sortedArr);
}
// build a node from an array
public static Node<T> Build<T>(T[] arr) {
Node<T> node = new Node<T>(arr[arr.Length - 1]);
for (int i = arr.Length - 2; i >= 0; i--) {
node = new Node<T>(arr[i], node);
}
return node;
}
7
// generates a list with a given length and a given value
range
public static Node<int> GenerateList(int length, int min,
int max) {
Random rand = new Random();
Node<int> node = new Node<int>(rand.Next(min,
max));
int n = rand.Next(4, 10);
for (int i = 0; i < n; i++) {
node = new Node<int>(rand.Next(min, max),
node);
}
return node;
}
// add a value to the start of the given list
public static Node<T> AddToList<T>(Node<T> node, T
value) {
Node<T> newNode = new Node<T>(value, node);
return newNode;
// add a value to the given list
public static Node<T> AddToList<T>(Node<T> node,
Node<T> pos, T value) {
if (pos == null) {
return AddToList(node, value);
}
Node<T> newNode = new Node<T>(value);
Node<T> p = node;
while (p != null) {
if (p == pos) {
newNode.SetNext(p.GetNext());
p.SetNext(newNode);
}
p = p.GetNext();
}
return node;
}
// add a value to a sorted list (the given list)
public static Node<int> AddToSortedList(Node<int>
node, int value) {
if (value < node.GetValue()) {
return AddToList(node, value);
}
Node<int> p = node;
while (p.GetNext() != null) {
if (value < p.GetNext().GetValue()) {
return AddToList(node, p, value);
}
p = p.GetNext();
}
return AddToList(node, p, value);
}
8
// remove the first appearance of the given value from
the given list
public static Node<int> RemoveFromList(Node<int>
node, int value) {
if (node.GetValue() == value) {
return node.GetNext();
}
Node<int> p = node.GetNext();
Node<int> prev = node;
while (p != null) {
if (p.GetValue() == value) {
prev.SetNext(p.GetNext());
return node;
}
prev = p;
p = p.GetNext();
}
return node;
}
// remove the given node from the given list
public static Node<T> RemoveFromList<T>(Node<T>
node, Node<T> pos) {
if (pos == null) {
return node;
}
else if (pos == node) {
return node.GetNext();
}
Node<T> p = node.GetNext();
while (p != null) {
if (p == pos) {
GetPrevious(node, p).SetNext(p.GetNext());
return node;
}
p = p.GetNext();
}
return node;
}
// return the last node in the given list
public static Node<T> GetLast<T>(Node<T> node) {
Node<T> p = node;
while (p.GetNext() != null) {
p = p.GetNext();
}
return p;
}
// return the node before a given node in the given list
public static Node<T> GetPrevious<T>(Node<T> node,
Node<T> pos) {
Node<T> p = node;
Node<T> previous = null;
while (p != null) {
if (p == pos) {
return previous;
}
previous = p;
p = p.GetNext();
}
return null;
}
9
// return a node from the given list by his number in
the list
public static Node<T> GetPosition<T>(Node<T> node,
int index) {
Node<T> p = node;
for (int k = 0; p != null; k++) {
if (k == index) {
return p;
}
p = p.GetNext();
}
return null;
}
// check if a certain value appears in the given list, return
true if it is
public static bool IsIn(Node<int> node, int value) {
Node<int> p = node;
while (p != null) {
if (p.GetValue() == value) {
return true;
}
p = p.GetNext();
}
return false;
}
// return a node from the given list with the same value
as the given value
public static Node<int> Find(Node<int> node, int value)
{
Node<int> p = node;
while (p != null) {
if (p.GetValue() == value) {
return p;
}
p = p.GetNext();
}
return null;
}
// return the length of the given list
public static int ListLength<T>(Node<T> node) {
int length = 1;
Node<T> p = node.GetNext();
while (p != null) {
length++;
p = p.GetNext();
}
return length;
}
// return the sum of the given list
public static int SumList(Node<int> node) {
int sum = node.GetValue();
Node<int> p = node.GetNext();
while (p != null) {
sum += p.GetValue();
p = p.GetNext();
}
return sum;
}
// return the maximum value in the given list
public static Node<int> ListMax(Node<int> node) {
Node<int> max = node;
Node<int> p = node.GetNext();
while (p != null) {
if (p.GetValue() > max.GetValue()) {
max = p;
}
p = p.GetNext();
}
return max;
}
10
// return the minimum value in the given list
public static Node<int> ListMin(Node<int> node) {
Node<int> min = node;
Node<int> p = node.GetNext();
while (p != null) {
if (p.GetValue() < min.GetValue()) {
min = p;
}
p = p.GetNext();
}
return min;
}
// check how many times a certain value appears in the
given list
public static int CountValue(Node<int> node, int value) {
int count = 0;
Node<int> p = node;
while (p != null) {
if (p.GetValue() == value) {
count++;
}
p = p.GetNext();
}
return count;
}
// check if the given list is ascending, return true if it is
public static bool IsAscending(Node<int> node) {
if (node.GetNext() == null) {
return true;
}
Node<int> p = node.GetNext();
Node<int> prev = node;
while (p != null) {
if (p.GetValue() < prev.GetValue()) {
return false;
}
prev = p;
p = p.GetNext();
}
return true;
}
// check if the given list is descending, return true if it is
public static bool IsDescending(Node<int> node) {
if (node.GetNext() == null) {
return true;
}
Node<int> p = node.GetNext();
Node<int> prev = node;
while (p != null) {
if (p.GetValue() > prev.GetValue()) {
return false;
}
prev = p;
p = p.GetNext();
}
return true;
}
11
// merge 2 lists
public static Node<int> MergeLists(Node<int> node1,
Node<int> node2) {
if (!IsAscending(node1) || !IsAscending(node2)) {
return null;
}
Node<int> start = null;
Node<int> p = null;
Node<int> p1 = node1;
Node<int> p2 = node2;
if (p1.GetValue() < p2.GetValue()) {
p = node1;
start = p;
p1 = p1.GetNext();
}
else if (p1.GetValue() > p2.GetValue()) {
p = node2;
start = p;
p2 = p2.GetNext();
}
else {
p = node1;
start = p;
p1 = p1.GetNext();
p2 = p2.GetNext();
}
while (p1 != null || p2 != null) {
if (p1 == null) {
while (p2 != null) {
p.SetNext(p2);
p2 = p2.GetNext();
p = p.GetNext();
}
break;
}
else if (p2 == null) {
while (p1 != null) {
p.SetNext(p1);
p1 = p1.GetNext();
p = p.GetNext();
// reverse the given list
public static Node<T> ReverseList<T>(Node<T> node) {
Node<T> p = GetLast(node);
Node<T> start = p;
while (p != null) {
p.SetNext(GetPrevious(node, p));
p = p.GetNext();
}
return start;
}
// add a value to the end of a given list
public static Node<T> AddToEndOfList<T>(Node<T>
node, Node<T> value) {
GetLast(node).SetNext(value);
return node;
}
12
}
break;
}
if (p1.GetValue() < p2.GetValue()) {
p.SetNext(p1);
p1 = p1.GetNext();
}
else if (p1.GetValue() > p2.GetValue()) {
p.SetNext(p2);
p2 = p2.GetNext();
}
else {
p.SetNext(p1);
p1 = p1.GetNext();
p2 = p2.GetNext();
}
p = p.GetNext();
// add after every even number a number that is bigger
by one
public static void UpOne(Node<int> first) {
Node<int> p = first, q;
while (p != null) {
if (p.GetValue() % 2 == 0) {
q = new Node<int>(0);
q.SetNext(p.GetNext());
p.SetNext(q);
}
p = p.GetNext();
}
}
}
return start;
}
// check if all the numbers in the list are even
public static bool EvenNum(Node<int> first) {
Node<int> p = first;
while (p != null && p.GetValue() % 2 == 0) {
p = p.GetNext();
}
if (p != null) {
return true;
}
return false;
}
// check if the numbers are in order
public static bool FollowNum(Node<int> first) {
Node<int> p = first;
while (p.GetNext() != null) {
if (p.GetValue() + 1 == p.GetNext().GetValue()) {
return false;
}
p = p.GetNext();
}
return true;
}
// return the average of the list
public static double Avg(Node<int> first) {
return (double)SumList(first) / ListLength(first);
}
// add 1 to every number
public static void AddOneToAll(Node<int> first) {
Node<int> p = first;
while (p != null) {
p.SetValue(p.GetValue() + 1);
p = p.GetNext();
}
}
13
// change every negative value to positive
public static void MakePositive(Node<int> first) {
Node<int> p = first;
while (p != null) {
if (p.GetValue() < 0) {
p.SetValue(p.GetValue() * (-1));
}
p = p.GetNext();
}
}
// reverse the list
public static Node<string> ReverseNode(Node<string>
first) {
Node<string> p = first, q;
Node<string> second = new
Node<string>(p.GetValue());
p = p.GetNext();
while (p != null) {
q = new Node<String>(p.GetValue());
q.SetNext(second);
second = q;
p = p.GetNext();
}
return second;
}
// return all the negative numbers in the list
public static Node<double>
GetNegative(Node<double> first) {
Node<double> p = first, q = new Node<double>(0.0),
r = q;
while (p != null) {
if (p.GetValue() < 0) {
q.SetNext(new Node<double>(p.GetValue()));
q = q.GetNext();
}
p = p.GetNext();
}
return r;
}
// remove all the numbers with even index
public static void DeleteEven(Node<int> first) {
Node<int> p = first, q;
while (p != null && p.GetNext() != null) {
q = p.GetNext();
p.SetNext(q.GetNext());
q.SetNext(null);
p = p.GetNext();
}
}
14
// remove all the negative numbers in the list
public static Node<int> DeleteNegative(Node<int> first)
{
Node<int> p = first, q = null;
while (p != null && p.GetValue() < 0) {
q = p;
p = p.GetNext();
}
if (q != null) {
q.SetNext(null);
}
first = p;
while (p.GetNext() != null) {
if (p.GetNext().GetValue() < 0) {
p.SetNext((p.GetNext().GetNext()));
}
else {
p = p.GetNext();
}
}
return first;
}
// remove all the duplicates in the list
public static Node<int> DeleteDuplicated(Node<int>
first) {
Node<int> second = new Node<int>(first.GetValue());
Node<int> p = first.GetNext(), q = second;
while (p != null) {
if (!IsIn(second, p.GetValue())) {
q.SetNext(new Node<int>(p.GetValue()));
q = q.GetNext();
}
p = p.GetNext();
}
return second;
}
// remove every time the number num appears in the
list
public static Node<int> DeleteNum(Node<int> first, int
num) {
Node<int> q = first, p = first.GetNext();
while (first != null && first.GetValue() == num) {
first = first.GetNext();
}
while (p != null) {
if (p.GetValue() == num) {
q.SetNext(p.GetNext());
p.SetNext(null);
p = q.GetNext();
}
else {
q = q.GetNext();
p = p.GetNext();
}
}
return first;
}
// check how many times in a row the value num
appears
public static int HowMany(Node<int> first, int num) {
int counter = 0;
Node<int> p = first;
if (!IsIn(first, num)) {
return 0;
}
while (p != null) {
if (p.GetValue() == num && p.GetNext().GetValue()
== num)
counter++;
p = p.GetNext();
}
return counter;
}
15
// print every value between m to n
public static void
PrintBetweenTwoNumbers(Node<int> first, int m, int n)
{
Node<int> p = first;
for (int i = 0; i < n; i++) {
if (i >= m && i < n) {
Console.WriteLine(p.GetValue() + " ");
}
p = p.GetNext();
}
}
// return the maximum sum of the two close cells
public static int MaxSum(Node<int> first) {
Node<int> p = first;
int sum = 0, max = 0;
while (p.GetNext() != null) {
sum = p.GetValue() + p.GetNext().GetValue();
if (sum > max) {
max = sum;
sum = 0;
}
p = p.GetNext();
}
return max;
}
// create a random list with 2 digits numbers with no
duplicates
public static Node<int> LotteryNum() {
int count = 1;
int num;
Random rand = new Random();
Node<int> first = new Node<int>(rand.Next((99 - 10)
+ 10));
Node<int> p = first;
while (count != 50) {
num = rand.Next((99 - 10) + 10);
if (!IsIn(first, num)) {
count++;
p.SetNext(new Node<int>(num));
p = p.GetNext();
}
}
return first;
}
// QUEUE
// create a queue with values from an array
public static Queue<int> CreateQueue(int[] values) {
Queue<int> queue = new Queue<int>();
for (int i = 0; i < values.Length; i++) {
queue.Insert(values[i]);
}
return queue;
}
16
// create a queue with a given length and values
between a given range (minimum to maximum)
public static Queue<int> CreateQueue(int length, int
minimum, int maximum) {
Queue<int> queue = new Queue<int>();
Random rand = new Random();
for (int i = 0;i < length; i++) {
queue.Insert(rand.Next(minimum, maximum));
}
return queue;
}
// duplicate the given queue
public static Queue<T> Duplicate<T>(Queue<T> queue) {
Queue<T> temp = new Queue<T>();
Queue<T> copy = new Queue<T>();
while (!queue.IsEmpty()) {
temp.Insert(queue.Remove());
}
while (!temp.IsEmpty()) {
T value = temp.Remove();
queue.Insert(value);
copy.Insert(value);
}
return copy;
}
// return the sum of the given queue
public static int SumQueue(Queue<int> queue) {
Queue<int> copy = Duplicate(queue);
int sum = 0;
while (!copy.IsEmpty()) {
sum += copy.Remove();
}
return sum;
}
// return the length of the given queue
public static int LengthQueue<T>(Queue<T> queue) {
Queue<T> copy = Duplicate(queue);
int length = 0;
while (!copy.IsEmpty()) {
copy.Remove();
length++;
}
return length;
}
// calculate the average of the given queue
public static double AverageQueue(Queue<int> queue)
{
return (double)(SumQueue(queue) /
(double)LengthQueue(queue));
}
// return the maximum value in the given queue
public static int MaxValueQueue(Queue<int> queue) {
Queue<int> copy = Duplicate(queue);
int maxValue = copy.Remove();
while (!copy.IsEmpty()) {
if (maxValue < copy.head()) {
maxValue = copy.Remove();
}
else {
copy.Remove();
}
}
return maxValue;
}
17
// return the minimum value in the given queue
public static int MinValueQueue(Queue<int> queue) {
Queue<int> copy = Duplicate(queue);
int minValue = copy.Remove();
while (!copy.IsEmpty()) {
if (minValue > copy.head()) {
minValue = copy.Remove();
}
else {
copy.Remove();
}
}
return minValue;
}
// check if a certain value appears in the given queue,
return true if it is
public static bool IsInQueue(Queue<int> queue, int
value) {
Queue<int> copy = Duplicate(queue);
while (!copy.IsEmpty()) {
if (copy.Remove() == value) {
return true;
}
}
return false;
}
// change the order of the value in the given queue to
end to start (reverse the order)
public static void ReverseQueue<T>(Queue<T> queue) {
Stack<T> helper = new Stack<T>();
while (!queue.IsEmpty()) {
helper.Push(queue.Remove());
}
while (!helper.IsEmpty()) {
queue.Insert(helper.Pop());
}
}
18
Download