C#实现基于链表的内存记事本实例
本文实例讲述了C#实现基于链表的内存记事本。分享给大家供大家参考。具体如下:
User模型:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class User { private string username; public string Username { get { return username; } set { username = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } private string age; public string Age { get { return age; } set { age = value; } } private string phone; public string Phone { get { return phone; } set { phone = value; } } } }
程序的灵魂Controller:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class Controller { private ArrayList a = new ArrayList(); public ArrayList A { get { return a; } set { a = value; } } public void add(User user) { A.Add(user); } public void delete(User user) { if (A.Contains(user)) { A.Remove(user); } else { Console.WriteLine("用户不存在!"); } } public ArrayList select(ArrayList a) { return a; } public User search(string username) { foreach(User user in A) { if (user.Username == username) { return user; } } return null; } } }
Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Controller controller = new Controller(); while (true) { Console.WriteLine("请输入您的操作:"); Console.WriteLine("1,增加用户"); Console.WriteLine("2,删除用户"); Console.WriteLine("3,浏览用户"); Console.WriteLine("4,退出"); string input = Console.ReadLine(); if(input=="1") { User user = new User(); 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("请输入用户姓名"); string username = Console.ReadLine(); if (controller.search(username)!=null) { User user = controller.search(username); controller.delete(user); } else { Console.WriteLine("该用户不存在!"); } } if(input=="3") { foreach(User user in controller.A ) { Console.WriteLine(user.Username); } } } } } }
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)