Collection类学习

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7.  
  8. namespace Colloction
  9. {
  10. class Collection:CollectionBase
  11. {
  12. public void Add(Object item) { InnerList.Add(item); }
  13. public void Remove(Object item) { InnerList.Remove(item); }
  14. public void Clear(){InnerList.Clear();}
  15. public int Count() { return InnerList.Count; }
  16.  
  17. }
  18. class Program
  19. {
  20. static void Main(string[] args)
  21. {
  22. Collection names = new Collection();
  23. names.Add("");
  24. names.Add("");
  25. names.Add("");
  26. names.Add("");
  27. foreach (Object name in names) { Console.WriteLine(name); }
  28. Console.WriteLine("总数量: "+names.Count());
  29. names.Remove("");
  30. Console.WriteLine("总数量: " + names.Count());
  31. names.Clear();
  32. Console.WriteLine("总数量: " + names.Count());
  33. }
  34. }
  35. }

泛型

  1. static void Swap<T>(ref T val1,ref T val2)
  2. {
  3. T temp;
  4. temp = val1;
  5. val1 = val2;
  6. val2 = temp;
  7. }
  8. static void Main(string[] args)
  9. {
  10. int num1 = ;
  11. int num2 = ;
  12. Swap<int>(ref num1, ref num2);
  13. Console.WriteLine(num1);
  14. }

测量时间

  1. DateTime startTime;
  2. TimeSpan endTime;
  3. startTime = DateTime.Now;
  4. for (int i = ; i < ; i++)
  5. ;
  6. endTime = DateTime.Now.Subtract(startTime);
  7. Console.WriteLine(endTime.TotalSeconds);

高级版

  1.        TimeSpan endTime;
  2. for (int i = ; i < ; i++)
  3. ;
  4. endTime = Process.GetCurrentProcess().TotalProcessorTime;
  5. Console.WriteLine(endTime);

锯齿状数组

  1. int[] Jan = new int[];
  2. int[] Feb = new int[];
  3. int[][] sales = new int[][]{Jan,Feb};

ArrayList

  1.    static void Main(string[] args)
  2. {
  3. ArrayList grades = new ArrayList();
  4. grades.Add();
  5. grades.Add();
  6. int position = grades.Add();
  7. Console.WriteLine(position);//输出位置
  8. grades.Insert(, );
  9. string[] newNames = new string[] { "孙大海", "海大富" };
  10. grades.InsertRange(, newNames);
  11. foreach (object grade in grades)
  12. Console.WriteLine(grade);
  13. }

选择排序

  1.        for (int i = ; i < ;i++)
  2. {
  3. int min = i; //每行选出一个最小的来跟头上交换
  4. for(int j=i;j<;j++)
  5. {
  6. if (arr[j] < arr[min]) min = j;
  7. }
  8. if(min!=i)
  9. {
  10. int t = arr[min];
  11. arr[min] = arr[i];
  12. arr[i] = t;
  13. }
  14. }

冒泡排序

  1.    for (int i = ; i < ; i++)//每次都拿第一个来冒泡 跑到不能跑的位置 回来再从第一个泡
  2. {
  3. for (int j = ; j < - i; j++)
  4. {
  5. if (arr[j] > arr[j + ])
  6. {
  7. int t = arr[j];
  8. arr[j] = arr[j + ];
  9. arr[j + ] = t;
  10. }
  11. }
  12. }

二分查找

  1. public static int binSearch(int value)
  2. {
  3. int upperBound, lowerBound, mid;
  4. upperBound = arr.Length - ;
  5. lowerBound = ;
  6. while(lowerBound<=upperBound)
  7. {
  8. mid = (lowerBound + upperBound) / ;
  9. if (arr[mid] == value) return mid;
  10. else
  11. {
  12. if (value < arr[mid])
  13. upperBound = mid - ;
  14. else
  15. lowerBound = mid + ;
  16. }
  17. }
  18. return -;
  19. }
  20. public static int RbinSearch(int value,int lower,int upper)
  21. {
  22. if (lower > upper) return -;
  23. else
  24. {
  25. int mid = (upper + lower)/;
  26. if (value < arr[mid])
  27. {
  28. return RbinSearch(value, lower, mid - );
  29. }
  30. else if (value == arr[mid]) return mid;
  31. else
  32. return RbinSearch(value, mid + , upper);
  33. }
  34. }

  1. class CStack
  2. {
  3. private ArrayList List;
  4. private int p_index;
  5. public CStack()
  6. {
  7. List = new ArrayList();
  8. p_index=-;
  9. }
  10. public int count{get{return List.Count;}}
  11. public void push(object item)
  12. {
  13. List.Add(item);
  14. p_index++;
  15. }
  16. public object pop()
  17. {
  18. object obj = List[p_index];
  19. List.RemoveAt(p_index);
  20. p_index--;
  21. return obj;
  22. }
  23. public object peek()
  24. {
  25. return List[p_index];
  26. }
  27. static void Main(string[] args)
  28. {
  29. CStack alist = new CStack();
  30. string word = "sees";
  31. alist.push(word[]);
  32. alist.push(word[]);
  33. alist.push(word[]);
  34. alist.push(word[]);
  35. Console.WriteLine(alist.peek());
  36.  
  37. }
  38. }

队列

  1. //写入数据到Queue中
  2. Queue q = new Queue();
  3. for (int i = ; i < ; i++)
  4. {
  5. q.Enqueue(i);
  6. }
  7.  
  8. //循环输出Queue所有数据
  9. Console.WriteLine("开始输出Queue数据");
  10. while (q.Count > )
  11. {
  12. Console.WriteLine(q.Dequeue());
  13. }
  14.  
  15. //-------------------------------------分割线------------------------------------//
  16.  
  17. //写入数据到Stack中
  18. Stack s = new Stack();
  19. for (int i = ; i < ; i++)
  20. {
  21. s.Push(i);
  22. }
  23.  
  24. //循环输出所有Stack数据
  25. Console.WriteLine("开始输出Stack数据");
  26. while (s.Count > )
  27. {
  28. Console.WriteLine(s.Pop());
  29. }

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. python实用脚本集

    iScript 是Github上 PeterDing 大神写的一个脚本集,由多数的 python 脚本和少数GM脚本组成. 含有以下几个脚本: xiami.py - 下载或播放高品质虾米音乐(xiam ...

  2. 【dfs】p1025 数的划分

    P1025 数的划分 题目描述 将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有 ...

  3. C语言学习记录之一

    1. while语句 2. 循环嵌套 3. 数组 4. 排序 1. while 由于上节课时间有限,介绍完for循环后没有来得及讲while语句.简单来讲,while也是一种循环结构,先看一个例子: ...

  4. 编写高质量代码:改善Java程序的151个建议 --[0~25]

    警惕自增的陷阱 public class Client7 { public static void main(String[] args) { int count=0; for(int i=0; i& ...

  5. A1143. Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  6. snmpwalk

    什么是snmpwalk?snmpwalk是一个SNMP小程序,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. snmpwalk的作用 ...

  7. WebAPI集成SignalR

    WebAPI提供通用数据接口,SignalR提供实时消息传输,两者可以根据实际业务需求进行组合. 环境 版本 操作系统 Windows 10 prefessional 编译器 Visual Studi ...

  8. TCHAR和CHAR类型的互转,string 转lpcwstr

    https://www.cnblogs.com/yuguangyuan/p/5955959.html 没有定义UNICODE,所以它里面的字符串就是简单用" "就行了,创建工程的时 ...

  9. Linux系统调用之内存管理

    brk 改变数据段空间的分配 sbrk 参见brk mlock 内存页面加锁 munlock 内存页面解锁 mlockall 调用进程所有内存页面加锁 munlockall 调用进程所有内存页面解锁 ...

  10. python zip()函数的使用

    解释: 后缀为zip的文件肯定都见过吧?zip是打包压缩好的一个文件,所以,zip()函数也简单的理解为打包压缩函数,将不同个数相同类型的字段结合在一起. 官方定义为:zip() 函数用于将可迭代的对 ...