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());
- }
C#数据结构学习的更多相关文章
- 算法设计和数据结构学习_5(BST&AVL&红黑树简单介绍)
前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm analysis in c++ (second ed ...
- 数据结构学习之字符串匹配算法(BF||KMP)
数据结构学习之字符串匹配算法(BF||KMP) 0x1 实验目的 通过实验深入了解字符串常用的匹配算法(BF暴力匹配.KMP.优化KMP算法)思想. 0x2 实验要求 编写出BF暴力匹配.KM ...
- 数据结构学习之栈求解n皇后问题
数据结构学习之栈求解n皇后问题 0x1 目的 深入掌握栈应用的算法和设计 0x2 内容 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后 ...
- 1.基础: 万丈高楼平地起——Redis基础数据结构 学习记录
<Redis深度历险:核心原理和应用实践>1.基础: 万丈高楼平地起——Redis基础数据结构 学习记录http://naotu.baidu.com/file/b874e2624d3f37 ...
- ES6中Map数据结构学习笔记
很多东西就是要细细的品读然后做点读书笔记,心理才会踏实- Javascript对象本质上就是键值对的集合(Hash结构),但是键只能是字符串,这有一定的限制. 1234 var d = {}var e ...
- linux内核数据结构学习总结
目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...
- 十五分钟介绍 Redis数据结构--学习笔记
下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...
- 邓俊辉数据结构学习-7-BST
二叉搜索树(Binary-Search-Tree)--BST 要求:AVL树是BBST的一个种类,继承自BST,对于AVL树,不做太多掌握要求 四种旋转,旋转是BBST自平衡的基本,变换,主要掌握旋转 ...
- ES6中map数据结构学习
在项目中遇到一个很恶心的需求,然后发现ES6中的map可以解决,所以简单学习了一下map. Javascript的Object本身就是键值对的数据结构,但实际上属性和值构成的是“字符串-值”对,属性只 ...
- GJM : 数据结构学习笔记
--------------------------数据结构 --------------------数据结构分 线性数据结构给非线性数据结构 数据和结合 线性表(顺序存储方式)特点:有且仅有一个开始 ...
随机推荐
- python实用脚本集
iScript 是Github上 PeterDing 大神写的一个脚本集,由多数的 python 脚本和少数GM脚本组成. 含有以下几个脚本: xiami.py - 下载或播放高品质虾米音乐(xiam ...
- 【dfs】p1025 数的划分
P1025 数的划分 题目描述 将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有 ...
- C语言学习记录之一
1. while语句 2. 循环嵌套 3. 数组 4. 排序 1. while 由于上节课时间有限,介绍完for循环后没有来得及讲while语句.简单来讲,while也是一种循环结构,先看一个例子: ...
- 编写高质量代码:改善Java程序的151个建议 --[0~25]
警惕自增的陷阱 public class Client7 { public static void main(String[] args) { int count=0; for(int i=0; i& ...
- 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 ...
- snmpwalk
什么是snmpwalk?snmpwalk是一个SNMP小程序,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. snmpwalk的作用 ...
- WebAPI集成SignalR
WebAPI提供通用数据接口,SignalR提供实时消息传输,两者可以根据实际业务需求进行组合. 环境 版本 操作系统 Windows 10 prefessional 编译器 Visual Studi ...
- TCHAR和CHAR类型的互转,string 转lpcwstr
https://www.cnblogs.com/yuguangyuan/p/5955959.html 没有定义UNICODE,所以它里面的字符串就是简单用" "就行了,创建工程的时 ...
- Linux系统调用之内存管理
brk 改变数据段空间的分配 sbrk 参见brk mlock 内存页面加锁 munlock 内存页面解锁 mlockall 调用进程所有内存页面加锁 munlockall 调用进程所有内存页面解锁 ...
- python zip()函数的使用
解释: 后缀为zip的文件肯定都见过吧?zip是打包压缩好的一个文件,所以,zip()函数也简单的理解为打包压缩函数,将不同个数相同类型的字段结合在一起. 官方定义为:zip() 函数用于将可迭代的对 ...