【愚公系列】2021年11月 C#版 数据结构基本使用(C#版)

一:数组的使用

数组的特点:内存连续存储,节约空间,可以索引访问,读取快,增删慢

//Array:在内存上连续分配的,而且元素类型是一样的
//可以坐标访问  读取快--增删慢,长度不变
Console.WriteLine("***************Array-start******************");
int[] Arraytoint = new int[10];
Arraydata [0] = 1024;
string[] Arraytostring = new string[] { "1024", "520" };
Console.WriteLine("***************Array-end******************");

//ArrayList  不定长的,连续分配的;
//元素没有类型限制,任何元素都是当成object处理,如果是值类型,会有装箱操作
//读取快--增删慢
Console.WriteLine("***************ArrayList-start******************");
ArrayList arrayList = new ArrayList();
arrayList.Add("怒骂");
arrayList.Add("爱了");
arrayList.Add(1024);//add增加长度,规则倍数增加
arrayList[4] = 520;//索引复制,不会增加长度
//删除数据
arrayList.RemoveAt(4);
var value = arrayList[2];
arrayList.RemoveAt(0);
arrayList.Remove(1024);
Console.WriteLine("***************ArrayList-end******************");

//List:也是Array,内存上都是连续摆放;不定长;泛型,保证类型安全,避免装箱拆箱
 //读取快--增删慢
 Console.WriteLine("***************List<T>-start******************");
 List<int> Listtoint = new List<int>() { 100, 200, 300, 400 };
 Listtoint.Add(1024);
 Listtoint.Add(520);
 Listtoint.Add("123");//异常的
 Listtoint[0] = 520;
 List<string> Listtostring = new List<string>();
 Listtostring[0] = "1024";
 Console.WriteLine("***************List<T>-end******************");

二:链表的使用

链表的特点:非连续摆放,存储数据+地址,找数据的话就只能顺序查找,读取慢;增删快。

//LinkedList:泛型的特点;链表,元素不连续分配,每个元素都有记录前后节点
//节点值可以重复
//能不能下标访问?不能,找元素就只能遍历  查找不方便
//增删 就比较方便
Console.WriteLine("***************LinkedList<T>-******************");
LinkedList<int> linkedList = new LinkedList<int>();
//linkedList[3]
linkedList.AddFirst(123);
linkedList.AddLast(456);
bool isContain = linkedList.Contains(123);
LinkedListNode<int> node123 = linkedList.Find(123);  //元素123的位置  从头查找
linkedList.AddBefore(node123, 123);
linkedList.AddBefore(node123, 123);
linkedList.AddAfter(node123, 9);
linkedList.Remove(456);
linkedList.Remove(node123);
linkedList.RemoveFirst();
linkedList.RemoveLast();
linkedList.Clear();
Console.WriteLine("***************LinkedList<T>******************");

//Queue 就是链表  先进先出  放任务延迟执行,A不断写入日志任务  B不断获取任务去执行
Console.WriteLine("***************Queue<T>******************");
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("four");
numbers.Enqueue("five");
foreach (string number in numbers)
{
    Console.WriteLine(number);
}
Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'");
Console.WriteLine($"Peek at next item to dequeue: { numbers.Peek()}");
Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'");
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
foreach (string number in queueCopy)
{
    Console.WriteLine(number);
}
Console.WriteLine($"queueCopy.Contains(\"four\") = {queueCopy.Contains("four")}");
queueCopy.Clear();
Console.WriteLine($"queueCopy.Count = {queueCopy.Count}");

//Stack 就是链表  先进后出  解析表达式目录树的时候,先产生的数据后使用
//操作记录为命令,撤销的时候是倒序的
Console.WriteLine("***************Stack<T>******************");
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");//放进去
foreach (string number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine($"Pop '{numbers.Pop()}'");//获取并移除
Console.WriteLine($"Peek at next item to dequeue: { numbers.Peek()}");//获取不移除
Console.WriteLine($"Pop '{numbers.Pop()}'");
Stack<string> stackCopy = new Stack<string>(numbers.ToArray());
foreach (string number in stackCopy)
{
Console.WriteLine(number);
}
Console.WriteLine($"stackCopy.Contains(\"four\") = {stackCopy.Contains("four")}");
stackCopy.Clear();
Console.WriteLine($"stackCopy.Count = {stackCopy.Count}");

三:集合Set的使用

集合的特性:set纯粹的集合,容器,东西丢进去,唯一性 无序的。

//集合:hash分布,元素间没关系,动态增加容量  去重
//统计用户IP;IP投票   交叉并补--二次好友/间接关注/粉丝合集
Console.WriteLine("***************HashSet<string>******************");
HashSet<string> hashSet = new HashSet<string>();
hashSet.Add("123");
hashSet.Add("689");
hashSet.Add("456");
hashSet.Add("12435");
hashSet.Add("12435");
hashSet.Add("12435");
//hashSet[0];
foreach (var item in hashSet)
{
Console.WriteLine(item);
}
Console.WriteLine(hashSet.Count);
Console.WriteLine(hashSet.Contains("12345"));

{
HashSet<string> hashSet1 = new HashSet<string>();
hashSet1.Add("123");
hashSet1.Add("689");
hashSet1.Add("789");
hashSet1.Add("12435");
hashSet1.Add("12435");
hashSet1.Add("12435");
hashSet1.SymmetricExceptWith(hashSet);//补
hashSet1.UnionWith(hashSet);//并
hashSet1.ExceptWith(hashSet);//差
hashSet1.IntersectWith(hashSet);//交
//风--亡五  找出共同的好友
}
hashSet.ToList();
hashSet.Clear();

//排序的集合:去重  而且排序  
//统计排名--每统计一个就丢进去集合
Console.WriteLine("***************SortedSet<string>******************");
SortedSet<string> sortedSet = new SortedSet<string>();
//IComparer<T> comparer  自定义对象要排序,就用这个指定
sortedSet.Add("123");
sortedSet.Add("689");
sortedSet.Add("456");
sortedSet.Add("12435");
sortedSet.Add("12435");
sortedSet.Add("12435");

foreach (var item in sortedSet)
{
Console.WriteLine(item);
}
Console.WriteLine(sortedSet.Count);
Console.WriteLine(sortedSet.Contains("12345"));
{
SortedSet<string> sortedSet1 = new SortedSet<string>();
sortedSet1.Add("123");
sortedSet1.Add("689");
sortedSet1.Add("456");
sortedSet1.Add("12435");
sortedSet1.Add("12435");
sortedSet1.Add("12435");
sortedSet1.SymmetricExceptWith(sortedSet);//补
sortedSet1.UnionWith(sortedSet);//并
sortedSet1.ExceptWith(sortedSet);//差
sortedSet1.IntersectWith(sortedSet);//交
}

sortedSet.ToList();
sortedSet.Clear();

四:字典key-value的使用

key-value的特点: 读取&增删都快? 有 hash散列 字典
key-value,一段连续有限空间放value(开辟的空间比用到的多,hash是用空间换性能),基于key散列计算得到地址索引,这样读取快
增删也快,删除时也是计算位置,增加也不影响别人 肯定会出现2个key(散列冲突),散列结果一致18,可以让第二次的+1,
可能会造成效率的降低,尤其是数据量大的情况下,以前测试过dictionary在3w条左右性能就开始下降的厉害

//Hashtable key-value  体积可以动态增加 拿着key计算一个地址,然后放入key - value
//object-装箱拆箱  如果不同的key得到相同的地址,第二个在前面地址上 + 1
//查找的时候,如果地址对应数据的key不对,那就 + 1查找。。
//浪费了空间,Hashtable是基于数组实现
//查找个数据  一次定位; 增删 一次定位;  增删查改 都很快
//浪费空间,数据太多,重复定位定位,效率就下去了
Console.WriteLine("***************Hashtable******************");
Hashtable table = new Hashtable();
table.Add("123", "456");
table[234] = 456;
table[234] = 567;
table[32] = 4562;
table[1] = 456;
table["eleven"] = 456;
foreach (DictionaryEntry objDE in table)
{
Console.WriteLine(objDE.Key.ToString());
Console.WriteLine(objDE.Value.ToString());
}
//线程安全
Hashtable.Synchronized(table);//只有一个线程写  多个线程读

//字典:泛型;key - value,增删查改 都很快;有序的
//  字典不是线程安全 ConcurrentDictionary
Console.WriteLine("***************Dictionary******************");
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
dic.Add(4, "HuHu1");
dic[4] = "HuHu";
dic.Add(4, "HuHu");
foreach (var item in dic)
{
Console.WriteLine($"Key:{item.Key}, Value:{item.Value}");
}

//经过排序后的
Console.WriteLine("***************SortedDictionary******************");
SortedDictionary<int, string> dic = new SortedDictionary<int, string>();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
dic.Add(4, "HuHu1");
dic[4] = "HuHu";
dic.Add(4, "HuHu");
foreach (var item in dic)
{
 Console.WriteLine($"Key:{item.Key}, Value:{item.Value}");
}

//经过排序后的
Console.WriteLine("***************SortedList******************");
SortedList sortedList = new SortedList();//IComparer
sortedList.Add("First", "Hello");
sortedList.Add("Second", "World");
sortedList.Add("Third", "!");

sortedList["Third"] = "~~";//
sortedList.Add("Fourth", "!");
sortedList.Add("Fourth", "!");//重复的Key Add会错
sortedList["Fourth"] = "!!!";
var keyList = sortedList.GetKeyList();
var valueList = sortedList.GetValueList();

sortedList.TrimToSize();//用于最小化集合的内存开销

sortedList.Remove("Third");
sortedList.RemoveAt(0);
sortedList.Clear();

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,为您设计一个.net本的权限系统的数据库,需要考虑以下几个方面: 1. 用户表:包括用户ID、用户名、密码、邮箱、手机号等基本信息。 2. 角色表:包括角色ID、角色名称、角色描述等信息。 3. 权限表:包括权限ID、权限名称、权限描述等信息。 4. 用户角色关联表:用于记录用户与角色之间的关系。 5. 角色权限关联表:用于记录角色与权限之间的关系。 6. 日志表:用于记录用户的操作日志,包括操作时间、操作人员、操作内容等信息。 根据以上需求,可以设计出以下的数据库表结构: 用户表(User): UserID int primary key UserName varchar(50) not null Password varchar(50) not null Email varchar(50) not null Mobile varchar(20) not null 角色表(Role): RoleID int primary key RoleName varchar(50) not null Description varchar(200) not null 权限表(Permission): PermissionID int primary key PermissionName varchar(50) not null Description varchar(200) not null 用户角色关联表(UserRole): UserID int not null RoleID int not null primary key(UserID, RoleID) 外键(UserID) references User(UserID) 外键(RoleID) references Role(RoleID) 角色权限关联表(RolePermission): RoleID int not null PermissionID int not null primary key(RoleID, PermissionID) 外键(RoleID) references Role(RoleID) 外键(PermissionID) references Permission(PermissionID) 日志表(Log): LogID int primary key UserID int not null OperationTime datetime not null OperationContent varchar(200) not null 外键(UserID) references User(UserID) 以上是我为您设计的.net本的权限系统的数据库,希望能够满足您的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚公搬代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值