C#实现基于链表的内存记事本实例
本文实例讲述了C#实现基于链表的内存记事本。分享给大家供大家参考。具体如下:
User模型:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassUser { privatestringusername; publicstringUsername { get{returnusername;} set{username=value;} } privatestringsex; publicstringSex { get{returnsex;} set{sex=value;} } privatestringage; publicstringAge { get{returnage;} set{age=value;} } privatestringphone; publicstringPhone { get{returnphone;} set{phone=value;} } } }
程序的灵魂Controller:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Collections; namespaceConsoleApplication1 { publicclassController { privateArrayLista=newArrayList(); publicArrayListA { get{returna;} set{a=value;} } publicvoidadd(Useruser) { A.Add(user); } publicvoiddelete(Useruser) { if(A.Contains(user)) { A.Remove(user); } else { Console.WriteLine("用户不存在!"); } } publicArrayListselect(ArrayLista) { returna; } publicUsersearch(stringusername) { foreach(UseruserinA) { if(user.Username==username) { returnuser; } } returnnull; } } }
Program.cs:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Collections; namespaceConsoleApplication1 { classProgram { staticvoidMain(string[]args) { Controllercontroller=newController(); while(true) { Console.WriteLine("请输入您的操作:"); Console.WriteLine("1,增加用户"); Console.WriteLine("2,删除用户"); Console.WriteLine("3,浏览用户"); Console.WriteLine("4,退出"); stringinput=Console.ReadLine(); if(input=="1") { Useruser=newUser(); Console.WriteLine("用户姓名:"); user.Username=Console.ReadLine(); Console.WriteLine("用户姓别:"); user.Sex=Console.ReadLine(); Console.WriteLine("用户年龄:"); user.Age=Console.ReadLine(); Console.WriteLine("电话号码:"); user.Phone=Console.ReadLine(); controller.add(user); } if(input=="2") { Console.WriteLine("请输入用户姓名"); stringusername=Console.ReadLine(); if(controller.search(username)!=null) { Useruser=controller.search(username); controller.delete(user); } else { Console.WriteLine("该用户不存在!"); } } if(input=="3") { foreach(Useruserincontroller.A) { Console.WriteLine(user.Username); } } } } } }
希望本文所述对大家的C#程序设计有所帮助。