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());
}

C#数据结构学习的更多相关文章

  1. 算法设计和数据结构学习_5(BST&AVL&红黑树简单介绍)

    前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm analysis in c++ (second ed ...

  2. 数据结构学习之字符串匹配算法(BF||KMP)

    数据结构学习之字符串匹配算法(BF||KMP) 0x1 实验目的 ​ 通过实验深入了解字符串常用的匹配算法(BF暴力匹配.KMP.优化KMP算法)思想. 0x2 实验要求 ​ 编写出BF暴力匹配.KM ...

  3. 数据结构学习之栈求解n皇后问题

    数据结构学习之栈求解n皇后问题 0x1 目的 ​ 深入掌握栈应用的算法和设计 0x2 内容 ​ 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后 ...

  4. 1.基础: 万丈高楼平地起——Redis基础数据结构 学习记录

    <Redis深度历险:核心原理和应用实践>1.基础: 万丈高楼平地起——Redis基础数据结构 学习记录http://naotu.baidu.com/file/b874e2624d3f37 ...

  5. ES6中Map数据结构学习笔记

    很多东西就是要细细的品读然后做点读书笔记,心理才会踏实- Javascript对象本质上就是键值对的集合(Hash结构),但是键只能是字符串,这有一定的限制. 1234 var d = {}var e ...

  6. linux内核数据结构学习总结

    目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...

  7. 十五分钟介绍 Redis数据结构--学习笔记

    下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...

  8. 邓俊辉数据结构学习-7-BST

    二叉搜索树(Binary-Search-Tree)--BST 要求:AVL树是BBST的一个种类,继承自BST,对于AVL树,不做太多掌握要求 四种旋转,旋转是BBST自平衡的基本,变换,主要掌握旋转 ...

  9. ES6中map数据结构学习

    在项目中遇到一个很恶心的需求,然后发现ES6中的map可以解决,所以简单学习了一下map. Javascript的Object本身就是键值对的数据结构,但实际上属性和值构成的是“字符串-值”对,属性只 ...

  10. GJM : 数据结构学习笔记

    --------------------------数据结构 --------------------数据结构分 线性数据结构给非线性数据结构 数据和结合 线性表(顺序存储方式)特点:有且仅有一个开始 ...

随机推荐

  1. MT【269】含参函数绝对值最大

    设函数$f(x)=ax^2+(2b+1)x-a-2$($a,b\in\mathcal R$,$a\neq 0$). (1) 若$a=-2$,求函数$y=|f(x)|$在$[0,1]$上的最大值$M(b ...

  2. shell getopts用法

    eg:sh test.sh -u tom -p 123456: getopts的使用形式:getopts OPTION_STRING VAR: OPTION_STRING:-u,-p这种自定义选项: ...

  3. 在浏览器中浏览git上项目目录结构

    效果如下,参考:https://gitee.com/oschina/GitCodeTree

  4. bzoj1226/luogu2157 学校食堂 (状压dp)

    我们先约定:(左) 窗口_人人人人人 (右) 可以发现,我们只需要知道最靠左的还没打饭的人 以及它身后7个人的状态 以及上一个打饭的人是谁 因为他左面的就都打过了 右面7个人以后肯定还没打 可以设f[ ...

  5. poj3926 parade (单调队列+dp)

    题意:有n行路,每行路被分成m段,每一段有长度和权值,要求从最下面一行走到最上面一行某个位置,可以从相邻两行的同一列交点往上走,并且在同一行走的长度要<=K,求走过的最大权值 设f[i][j]为 ...

  6. 使用ss命令对tcp连接数和状态的监控性能优化

    之前对tcp的监控采用netstat命令,发现在服务器繁忙的时候效果不理想,这个命令占用大量的cpu有时候高达90%以上,可能会导致业务的不稳定,所以改用ss命令对脚本进行优化 对tcp连接数和状态的 ...

  7. Docker部署Jenkins测试环境

    安装docker环境 yum install epel-release -y && yum install docker -y 如果是高手需要docker-compose的话就再装个d ...

  8. CSS修改滚动条样式

    <div class="qq_bottom">超出部分变滚动条</div> /*//滚动条整体部分*/ .qq_bottom::-webkit-scroll ...

  9. C# Winform多窗体&&构造函数传值

    一.多窗体:三种打开窗体的状态: 最最基础的弹窗: //写在按钮的点击事件内: //实例需要弹出的窗口的类: Form2 f2 = new Form2(); f2.Show(); 1.弹窗窗口: // ...

  10. A1112. Stucked Keyboard

    On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...