110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test4
{
class Program
{
public class pepole
{ public string Name {get;set;}
public pepole(string n)
{ this.Name=n;}
}
static void Main(string[] args)
{
//问题1:
{
pepole Jim = new pepole("Jim");
Console.WriteLine(Jim.Name);
}//Jim 对象 在此处 结束生命
//如果这里调用 Console.WriteLine(Jim.Name); 则报错
//我想问问 如何骗过 C# 的垃圾收集器,不对Jim 对象的释放,而我想手动释放。(仅对Jim对象)
//问题1:
{
pepole Jim = new pepole("Jim");
Console.WriteLine(Jim.Name);
//如何在这里 释放 Jim 对象
//使得下面的
//Console.WriteLine(Jim.Name); 会报错呢
}//Jim 对象 在此处 结束生命
Console.ReadKey();
}
}
}
public LinkedListNode AddLast(object node)//增加节点 -- 到最后
{
LinkedListNode newNode = new LinkedListNode(node);
if( first == null ) //
{
first = newNode;
last = first;
current = first;
}
else
{
newNode.Prev=this.last; //少了这一句
this.last.Next = newNode; // 链尾 连新结
this.last = newNode; // 链尾 重置
}
return newNode;
}
else
{
this.last.Next = newNode; // 链尾 连新结
newNode.Prev = this.last;//你添加新节点是没有设置前面的节点,( last = last.Prev; )导致last为null,你在调用last.Next肯定出问题
this.last = newNode; // 链尾 重置
}
//C# Code//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
namespace test2
{
public class person //测试用
{ public string Name { set; get; } }
class Program
{
static void Main(string[] args)
{
person p1 = new person();
p1.Name = "jim";
LinkedList MyList = new LinkedList();
MyList.AddLast(1);
MyList.AddLast("2");
MyList.AddLast(p1);
ListToScreen(MyList); //输出
MyList.AddLast("Add");
MyList.AddLast(1000);
ListToScreen(MyList); //输出
MyList.PopLast(); //弹出一个元素
ListToScreen(MyList); //输出
Console.ReadKey();
}
//---- 输出到LinkedList 所有元素屏幕的 ----
static void ListToScreen(LinkedList ll)
{
StringBuilder PutOut = new StringBuilder(); //输出到屏幕的String
foreach( var ob in ll )
{
if( ob is string )
{
PutOut.Append(ob);
}
if( ob is person )
{
PutOut.Append(((person)ob).Name);
}
if( ob is int )
{
PutOut.Append(ob.ToString());
}
PutOut.Append("\t");
}
Console.WriteLine(PutOut);//执行输出
}
}
//定义数据节点1法
public class LinkedListNode
{
private object value; //data
public LinkedListNode(object value) //构造
{
this.value = value;
}
public object Value
{
get { return value; }
}
private LinkedListNode next;
public LinkedListNode Next
{
get { return next; }
internal set { next = value; }
}
private LinkedListNode prev;
public LinkedListNode Prev
{
get { return prev; }
internal set { prev = value; }
}
LinkedListNode()
{
this.prev = this.next = null;
}
}
//定义链表 管理器
public class LinkedList: IEnumerable
{
private LinkedListNode current; //本链尾节点
private LinkedListNode first; //头节点
public LinkedListNode First
{
get { return first; }
}
private LinkedListNode last; //本链尾节点
public LinkedListNode Last
{
get { return last; }
}
public bool CurrentMoveUp() //当前节点上移
{
if( current == first )
{
return false;
}
else
{
current = current.Prev;
}
return true;
}
public bool CurrentMoveDown() //当前节点下移
{
if( current == last )
{
return false;
}
else
{
current = current.Next;
}
return true;
}
public LinkedListNode AddLast(object node)//增加节点 -- 到最后
{
LinkedListNode newNode = new LinkedListNode(node);
if( first == null ) //
{
first = newNode;
last = first;
current = first;
}
else
{
this.last.Next = newNode; // 链尾 连新结
this.last = newNode; // 链尾 重置
}
return newNode;
}
public LinkedListNode PopLast() //取得最后一个节点,并且LinkedList 不去控制它
{
if( first == null )
{
return null;
}
else
{
LinkedListNode returnNode = last; // 拿到链尾
last = last.Prev; // 链尾 前移
last.Next = null; // 链尾 打结 ///运行到这出错,谁知道为什么!!
return returnNode;
}
}
//还没有增加删除方法
public IEnumerator GetEnumerator() //专为 foreach 服务的
{
LinkedListNode current = first;//头节点
while( current != null )
{
yield return current.Value;
current = current.Next;
}
}
}
}