黑马程序员技术交流社区
标题:
关于c#的引用类型
[打印本页]
作者:
yllogininbh
时间:
2014-6-1 14:46
标题:
关于c#的引用类型
本帖最后由 yllogininbh 于 2014-6-2 19:03 编辑
我觉得c#的引用类型几乎就是指针,所以自己模拟写了一个list集合。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace zhizheng
{
class Program
{
static void Main(string[] args)
{
mylistImplament lis = new mylistImplament();
lis.add(new myStruct() { value="hhhhh 1"});
lis.add(new myStruct() { value = "sdf 2" });
lis.add(new myStruct() { value = "sd34543f 3" });
lis.add(new myStruct() { value = "hhgfgfghhh 4" });
Console.Write(lis);
Console.WriteLine("--------------------------");
lis.remove(new myStruct() { value = "hhgfgfghhh 4" });
Console.Write(lis);
Console.Read();
}
}
public interface mylist
{
void add(myStruct s1);
myStruct remove(myStruct checkValue);
}
public class myStruct
{
public string value = string.Empty;
public myStruct next=null;
}
public class mylistImplament:mylist
{
myStruct head;
myStruct pNow;
myStruct plast;
public void add(myStruct addValue)
{
if (head == null)
{
head = addValue;
pNow = addValue;
}
else
{
plast =pNow;
pNow = addValue;
plast.next = pNow;
}
}
public myStruct remove(myStruct checkValue)
{
if (head != null)
{
pNow = head;
plast = pNow;
while (!pNow.value.Equals(checkValue.value)&&pNow.next!=null)
{
plast = pNow;
if(pNow.next!=null)
pNow = pNow.next;
}
//第一个值为删除目标
if (plast.value.Equals( pNow.value))
{
head = pNow.next;
return pNow;
}
//最后一个值为删除目标
else if (pNow.next == null)
{
plast.next = null;
return pNow;
}
else
{
plast.next = pNow.next;
return pNow;
}
}
else
return null;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (head == null)
return "";
else
{
pNow = head;
sb.Append(pNow.value + "\r\n");
while (pNow.next != null)
{
pNow = pNow.next;
sb.Append(pNow.value+"\r\n");
}
return sb.ToString();
}
}
}
}
复制代码
看吧c#的引用型和指针的用法几乎一样
作者:
许庭洲
时间:
2014-6-1 20:29
值得学习ing!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2