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 : 数据结构学习笔记
--------------------------数据结构 --------------------数据结构分 线性数据结构给非线性数据结构 数据和结合 线性表(顺序存储方式)特点:有且仅有一个开始 ...
随机推荐
- hdu 2844 Coins (多重背包+二进制优化)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...
- 【AtCoder078D】Fennec VS. Snuke
AtCoder Regular Contest 078 D - Fennec VS. Snuke 题意 给一个树,1是白色,n是黑色,其它没有颜色.Fennec每次可以染白色点的直接邻居为白色.Snu ...
- Nginx 添加 PHP 支持
背景介绍默认安装的Nginx是无法打开php文件的,需要修改相关配置才能支持php 安装yum -y install epel-release yum -y install nginx yum ins ...
- SCOI2016 Day1 简要题解
目录 「SCOI2016」背单词 题意 题解 代码 「SCOI2016」幸运数字 题意 题解 总结 代码 「SCOI2016」萌萌哒 题意 题解 总结 代码 「SCOI2016」背单词 题意 这出题人 ...
- Android如何在一个TextView中实现多种文本风格?
本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术,本文为大家讲解Android中,如何在一个 ...
- 【BZOJ3561】DZY Loves Math VI (数论)
[BZOJ3561]DZY Loves Math VI (数论) 题面 BZOJ 题解 \[\begin{aligned} ans&=\sum_{i=1}^n\sum_{j=1}^m\sum_ ...
- 【转】C++ const 关键字总结
const是一个C++语言的限定符,它限定一个变量不允许被改变.使用const在一定程度上可以提高程序的安全性和可靠性.另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一 ...
- hexo+next主题目录解析
默认目录结构: . ├── .deploy ├── public ├── scaffolds ├── scripts ├── source | ├── _drafts | └── _posts ├── ...
- javascript之奇淫技巧
最近准备面试,复习一下javascript,整理了一些javascript的奇淫技巧~ //为兼容ie的模拟Object.keys() Object.showkeys = function(obj) ...
- 【模板】ac自动机
本来是真的特别不想写这个的 但是有段时间洛谷天天智推这个可能是我太菜了 然后觉得这个也不难 乘着今早没事写下 来这保存下 方便下次食用 #include <bits/stdc++.h> u ...