| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:单向链表在C#中的实现
只看楼主 加入收藏
liubaoen
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:104
专家分:117
注 册:2006-6-12
收藏
 问题点数:0 回复次数:0 
单向链表在C#中的实现

/*
* Created by SharpDevelop.
* User: 刘保恩
* Date: 2006-7-6
* Time: 16:55
* 单向链表在C#中的实现
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DS
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void ButTestClick(object sender, System.EventArgs e)
{
Link l=new Link();
for (int i=1;i<100;i++)
{
l.AddElement(i);

}
this.label1.Text+="\r\n";
this.label1.Text+=l.ToString();
}
}


//构造一个结点信息
public class Node
{
//数据成员
public Node Next;
public int Data;
public Node()
{
this.Data=0;
this.Next=null;
}
public Node(int a)
{
this.Data=a;
this.Next=null;
}
}

public class Link
{
public Node Head=new Node();
public int Length;
//类的数据成员
public Link()
{
this.Length=0;
//Head.Next=null;
//Head.Data=0;

}

public void AddElement(int a)
{
Node p=new Node();
Node q=new Node(a);
p=this.Head;//这里不能用p=this.Head.Next否者就会引发一个对象未实例化的错误,Head.Next没有实例化
while(p.Next!=null)
{
p=p.Next;
}
p.Next=q;
this.Length++;
}

public override string ToString()
{
string s="";
s+="Head->";
Node p;
p=this.Head.Next;
while(p!=null)
{
s+=p.Data.ToString();
s+="->";
p=p.Next;
}
return(s);
}

}



}

////
/*
请注意以下问题,一个变量在没有初始化之前是不能使用的,不管是什么类型的值都不能带入运算
即使是和null来比较也会返回一个错误
Object reference not set to an instance of an object
原文的意思是:未将对象引用到对象的实例.
也就是没有实例化一个对象。
当声明了变量,但是没有赋值,仍然为null.用的时候就会报错.

///

*/

搜索更多相关主题的帖子: 链表 
2006-07-06 22:13
快速回复:单向链表在C#中的实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021447 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved