C#数据结构学习

时间:2023-03-10 02:41:11
C#数据结构学习

Collection类学习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace Colloction
{
class Collection:CollectionBase
{
public void Add(Object item) { InnerList.Add(item); }
public void Remove(Object item) { InnerList.Remove(item); }
public void Clear(){InnerList.Clear();}
public int Count() { return InnerList.Count; } }
class Program
{
static void Main(string[] args)
{
Collection names = new Collection();
names.Add("");
names.Add("");
names.Add("");
names.Add("");
foreach (Object name in names) { Console.WriteLine(name); }
Console.WriteLine("总数量: "+names.Count());
names.Remove("");
Console.WriteLine("总数量: " + names.Count());
names.Clear();
Console.WriteLine("总数量: " + names.Count());
}
}
}

泛型

 static void Swap<T>(ref T val1,ref T val2)
{
T temp;
temp = val1;
val1 = val2;
val2 = temp;
}
static void Main(string[] args)
{
int num1 = ;
int num2 = ;
Swap<int>(ref num1, ref num2);
Console.WriteLine(num1);
}

测量时间

            DateTime startTime;
TimeSpan endTime;
startTime = DateTime.Now;
for (int i = ; i < ; i++)
;
endTime = DateTime.Now.Subtract(startTime);
Console.WriteLine(endTime.TotalSeconds);

高级版

       TimeSpan endTime;
for (int i = ; i < ; i++)
;
endTime = Process.GetCurrentProcess().TotalProcessorTime;
Console.WriteLine(endTime);

锯齿状数组

int[] Jan = new int[];
int[] Feb = new int[];
int[][] sales = new int[][]{Jan,Feb};

ArrayList

   static void Main(string[] args)
{
ArrayList grades = new ArrayList();
grades.Add();
grades.Add();
int position = grades.Add();
Console.WriteLine(position);//输出位置
grades.Insert(, );
string[] newNames = new string[] { "孙大海", "海大富" };
grades.InsertRange(, newNames);
foreach (object grade in grades)
Console.WriteLine(grade);
}

选择排序

       for (int i = ; i < ;i++)
{
int min = i; //每行选出一个最小的来跟头上交换
for(int j=i;j<;j++)
{
if (arr[j] < arr[min]) min = j;
}
if(min!=i)
{
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
}

冒泡排序

   for (int i = ; i < ; i++)//每次都拿第一个来冒泡 跑到不能跑的位置 回来再从第一个泡
{
for (int j = ; j < - i; j++)
{
if (arr[j] > arr[j + ])
{
int t = arr[j];
arr[j] = arr[j + ];
arr[j + ] = t;
}
}
}

二分查找

        public static int binSearch(int value)
{
int upperBound, lowerBound, mid;
upperBound = arr.Length - ;
lowerBound = ;
while(lowerBound<=upperBound)
{
mid = (lowerBound + upperBound) / ;
if (arr[mid] == value) return mid;
else
{
if (value < arr[mid])
upperBound = mid - ;
else
lowerBound = mid + ;
}
}
return -;
}
public static int RbinSearch(int value,int lower,int upper)
{
if (lower > upper) return -;
else
{
int mid = (upper + lower)/;
if (value < arr[mid])
{
return RbinSearch(value, lower, mid - );
}
else if (value == arr[mid]) return mid;
else
return RbinSearch(value, mid + , upper);
}
}

class CStack
{
private ArrayList List;
private int p_index;
public CStack()
{
List = new ArrayList();
p_index=-;
}
public int count{get{return List.Count;}}
public void push(object item)
{
List.Add(item);
p_index++;
}
public object pop()
{
object obj = List[p_index];
List.RemoveAt(p_index);
p_index--;
return obj;
}
public object peek()
{
return List[p_index];
}
static void Main(string[] args)
{
CStack alist = new CStack();
string word = "sees";
alist.push(word[]);
alist.push(word[]);
alist.push(word[]);
alist.push(word[]);
Console.WriteLine(alist.peek()); }
}

队列

//写入数据到Queue中
Queue q = new Queue();
for (int i = ; i < ; i++)
{
q.Enqueue(i);
} //循环输出Queue所有数据
Console.WriteLine("开始输出Queue数据");
while (q.Count > )
{
Console.WriteLine(q.Dequeue());
} //-------------------------------------分割线------------------------------------// //写入数据到Stack中
Stack s = new Stack();
for (int i = ; i < ; i++)
{
s.Push(i);
} //循环输出所有Stack数据
Console.WriteLine("开始输出Stack数据");
while (s.Count > )
{
Console.WriteLine(s.Pop());
}