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. 洛谷P3381 最小费用最大流

    费用流板子 还是一道板子题..先练练手 #include <bits/stdc++.h> #define INF 0x3f3f3f3f #define full(a, b) memset( ...

  2. 贝叶斯定理推导(Bayes' Theorem Induction)

    这里用Venn diagram来不严谨地推导一下贝叶斯定理. 假设A和B为两个不相互独立的事件. 交集(intersection):  上图红色部分即为事件A和事件B的交集. 并集(union):  ...

  3. 安装 linux-dash

    先看看软件的效果图,再介绍安装方法. 通过上图可以看到.软件可以实时监控CPU.内存.网络流量等相关信息,甚至可以监控到硬件信息安装方法:yum -y install httpd php zip un ...

  4. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  5. 763. Partition Labels

    我一开始看数据范围很小,没怎么想就直接暴力了. 暴力的思路是: 对于每一段S的前缀,这个前缀中的每一个字母都不应该在前缀的补集中出现,所以直接循环i:0 to S.length然后对于每一次循环,再循 ...

  6. KVM改NAT为Bridge

    kvm 改nat为桥接在安装一个拥有虚拟化功能的Linux操作系统(此处以CentOS为例),一般我们有两种方法:1.在光盘安装的时候安装好虚拟化包或者PXE服务器上配置好虚拟化包2.手动在没有安装虚 ...

  7. 【Linux】Linux系统中的权限详解

    我们linux服务器上有严格的权限等级,如果权限过高导致误操作会增加服务器的风险.所以对于了解linux系统中的各种权限及要给用户,服务等分配合理的权限十分重要. 一.文件基本权限 首先看下linux ...

  8. A.01.01—模块的输入—低端输入

    汽车电子模块的输入一般包含数字量低端输入.数字量高端输入.模拟量输入.脉宽调制输入.总线信号输入.脉冲信号输入,对于无线信号输入和视频信号音频信号我们不做讨论. 数字量低端输入是应用最为广泛的一种输入 ...

  9. 数据库和Django model 生成和反向生成

    Django 脚本生成数据表 建立映射关系 如果询问时区时间,选1 然后输入timezone.now() python manage.py makemigrations (如果有子应用的话子应用名称填 ...

  10. MATLAB中mesh函数的使用:基于像素强度画3D密度图(create a 3D density plot based on the pixel intensity:mesh function)

    所用的函数非常简单,只需要用到mesh函数,示例代码如下: Ima=imread('F:\pathto\test.jpg'); surf_ima = surf(rgb2gray(Ima)); %黑色的 ...