C#实现常用的数据结构(一):链表
2008-03-07 22:40
435 查看
http://johnsir.cnblogs.com/articles/331317.html
节点定义及其链表实现,代码如下:
//class ListNode and class list definitions.
using System;
namespace LinkedListLibrary
{
/// <summary>
/// class to represent one node in a list
/// </summary>
class ListNode
{
private object data;
private ListNode next;
//constructer to creat ListNode that refers to dataValue
//and in last node in list
public ListNode(object dataValue):this(dataValue,null)
{
}
//constructer to creat ListNode that refers to dataValue
//and refer to next ListNode in List
public ListNode(object dataValue,ListNode nextNode)
{
data = dataValue;
next = nextNode;
}
//property Next
public ListNode Next
{
get
{
return next;
}
set
{
next = value;
}
}
//property Data
public object Data
{
get{
return data;
}
}
}//end class ListNode
//class List definition
public class List
{
private ListNode firstNode;
private ListNode lastNode;
private string name; //string like "list" to display
//construct empty list with specified name
public List(string listName){
name = listName;
firstNode = lastNode = null;
}
//construct empty list with "list" as its name
public List():this("list"){
}
//Insert object at front of list.If List is empty,
//firstNode and lastNode will refer to same object.
//Otherwise,firstNode refer to new node.
public void InsertAtFront(object insertItem){
lock(this){
if(IsEmpty()){
firstNode = lastNode = new ListNode(insertItem);
}
else{
firstNode = new ListNode(insertItem,firstNode);
}
}
}
//return true if list is empty
public bool IsEmpty(){
lock(this){
return firstNode == null;
}
}
//Insert object at the end of list.If List is empty,
//firstNode and lastNode will refer to same object.
//otherwise,lastNode's Next property refers to new node
public void InsertAtBack(object insertItem){
lock(this){
if(IsEmpty()){
firstNode = lastNode = new ListNode(insertItem);
}
else{
lastNode = lastNode.Next = new ListNode(insertItem);
}
}
}
//remove first node from list
public object RemoveFromFront(){
lock(this){
if(IsEmpty()){
throw new EmptyListException( name );
}
object removeItem = firstNode.Data; //retrieve data
//reset firstNode and lastNode refereances
if(firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.Next;
return removeItem; //return removed data
}
}
//remove last node from list
public object RemoveFromBack()
{
lock(this){
if(IsEmpty()){
throw new EmptyListException( name );
}
object removeItem = lastNode.Data; //retrieve data
//reset firstNode and lastNode refereances
if(firstNode == lastNode)
firstNode = lastNode = null;
else{
ListNode current = firstNode;
//loop while current node is not lastNode
while(current.Next != lastNode){
current = current.Next; //move to next node
}
//current is new lastNode
lastNode = current;
current.Next = null;
}
return removeItem; //return removed data
}
}
//output list contents
virtual public void Print(){
lock(this){
if(IsEmpty()){
Console.WriteLine("Empty " + name) ;
return;
}
Console.Write("The " + name + " is: ");
ListNode current = firstNode;
//output current node data while not at end of list
while(current != null){
Console.Write(current.Data + "");
current = current.Next;
}
Console.WriteLine("/n");
}
}
}// end class List
//class EmptyListException definition
public class EmptyListException : ApplicationException
{
public EmptyListException(string name):base("The " + name + "is empty"){
}
}//end class EmptyListException
}//end namespace linkedlistlibrary
使用上面的链表,代码如下:
using System;
using LinkedListLibrary;
namespace ListTest
{
/// <summary>
/// ListTest 的摘要说明。
/// </summary>
public class ListTest
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
List list = new List();
//create data to store in list
bool aBoolean = true;
char aCharactor = '$';
int anInteger = 34567;
string aString = "hello";
//use List insert methods
list.InsertAtFront(aBoolean);
list.Print();
list.InsertAtFront(aCharactor);
list.Print();
list.InsertAtBack(anInteger);
list.Print();
list.InsertAtBack(aString);
list.Print();
//use list remove methods
object removedObject;
//remove data from list and print after each removal
try
{
removedObject = list.RemoveFromFront();
Console.WriteLine(removedObject + " removed");
list.Print();
removedObject = list.RemoveFromFront();
Console.WriteLine(removedObject + " removed");
list.Print();
removedObject = list.RemoveFromBack();
Console.WriteLine(removedObject + " removed");
list.Print();
removedObject = list.RemoveFromBack();
Console.WriteLine(removedObject + " removed");
list.Print();
}
//process exception if list empty when attempt is made to remove item
catch(EmptyListException emptyListException)
{
Console.Error.WriteLine("/n" + emptyListException);
}
}//end method Main
}//end class ListTest
}
运行结果如下:
发表于 2006-02-15 19:12 johnsir 阅读(564) 评论(1) 编辑 收藏 所属分类: 读书笔记
节点定义及其链表实现,代码如下:
//class ListNode and class list definitions.
using System;
namespace LinkedListLibrary
{
/// <summary>
/// class to represent one node in a list
/// </summary>
class ListNode
{
private object data;
private ListNode next;
//constructer to creat ListNode that refers to dataValue
//and in last node in list
public ListNode(object dataValue):this(dataValue,null)
{
}
//constructer to creat ListNode that refers to dataValue
//and refer to next ListNode in List
public ListNode(object dataValue,ListNode nextNode)
{
data = dataValue;
next = nextNode;
}
//property Next
public ListNode Next
{
get
{
return next;
}
set
{
next = value;
}
}
//property Data
public object Data
{
get{
return data;
}
}
}//end class ListNode
//class List definition
public class List
{
private ListNode firstNode;
private ListNode lastNode;
private string name; //string like "list" to display
//construct empty list with specified name
public List(string listName){
name = listName;
firstNode = lastNode = null;
}
//construct empty list with "list" as its name
public List():this("list"){
}
//Insert object at front of list.If List is empty,
//firstNode and lastNode will refer to same object.
//Otherwise,firstNode refer to new node.
public void InsertAtFront(object insertItem){
lock(this){
if(IsEmpty()){
firstNode = lastNode = new ListNode(insertItem);
}
else{
firstNode = new ListNode(insertItem,firstNode);
}
}
}
//return true if list is empty
public bool IsEmpty(){
lock(this){
return firstNode == null;
}
}
//Insert object at the end of list.If List is empty,
//firstNode and lastNode will refer to same object.
//otherwise,lastNode's Next property refers to new node
public void InsertAtBack(object insertItem){
lock(this){
if(IsEmpty()){
firstNode = lastNode = new ListNode(insertItem);
}
else{
lastNode = lastNode.Next = new ListNode(insertItem);
}
}
}
//remove first node from list
public object RemoveFromFront(){
lock(this){
if(IsEmpty()){
throw new EmptyListException( name );
}
object removeItem = firstNode.Data; //retrieve data
//reset firstNode and lastNode refereances
if(firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.Next;
return removeItem; //return removed data
}
}
//remove last node from list
public object RemoveFromBack()
{
lock(this){
if(IsEmpty()){
throw new EmptyListException( name );
}
object removeItem = lastNode.Data; //retrieve data
//reset firstNode and lastNode refereances
if(firstNode == lastNode)
firstNode = lastNode = null;
else{
ListNode current = firstNode;
//loop while current node is not lastNode
while(current.Next != lastNode){
current = current.Next; //move to next node
}
//current is new lastNode
lastNode = current;
current.Next = null;
}
return removeItem; //return removed data
}
}
//output list contents
virtual public void Print(){
lock(this){
if(IsEmpty()){
Console.WriteLine("Empty " + name) ;
return;
}
Console.Write("The " + name + " is: ");
ListNode current = firstNode;
//output current node data while not at end of list
while(current != null){
Console.Write(current.Data + "");
current = current.Next;
}
Console.WriteLine("/n");
}
}
}// end class List
//class EmptyListException definition
public class EmptyListException : ApplicationException
{
public EmptyListException(string name):base("The " + name + "is empty"){
}
}//end class EmptyListException
}//end namespace linkedlistlibrary
使用上面的链表,代码如下:
using System;
using LinkedListLibrary;
namespace ListTest
{
/// <summary>
/// ListTest 的摘要说明。
/// </summary>
public class ListTest
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
List list = new List();
//create data to store in list
bool aBoolean = true;
char aCharactor = '$';
int anInteger = 34567;
string aString = "hello";
//use List insert methods
list.InsertAtFront(aBoolean);
list.Print();
list.InsertAtFront(aCharactor);
list.Print();
list.InsertAtBack(anInteger);
list.Print();
list.InsertAtBack(aString);
list.Print();
//use list remove methods
object removedObject;
//remove data from list and print after each removal
try
{
removedObject = list.RemoveFromFront();
Console.WriteLine(removedObject + " removed");
list.Print();
removedObject = list.RemoveFromFront();
Console.WriteLine(removedObject + " removed");
list.Print();
removedObject = list.RemoveFromBack();
Console.WriteLine(removedObject + " removed");
list.Print();
removedObject = list.RemoveFromBack();
Console.WriteLine(removedObject + " removed");
list.Print();
}
//process exception if list empty when attempt is made to remove item
catch(EmptyListException emptyListException)
{
Console.Error.WriteLine("/n" + emptyListException);
}
}//end method Main
}//end class ListTest
}
运行结果如下:
发表于 2006-02-15 19:12 johnsir 阅读(564) 评论(1) 编辑 收藏 所属分类: 读书笔记
相关文章推荐
- C#实现常用的数据结构:链表
- C#实现常用的数据结构(一):链表
- 重温数据结构系列随笔:单链表(c#模拟实现)
- 无聊用C#2.0泛型实现的链表(C#数据结构一)
- 数据结构中链表的C#实现部分示例
- 编程中常用的重要的数据结构实现(二)之链表
- C#常用数据结构:数组,ArrayList,List<>,链表,Queue,Stack,Dictionary
- 重温数据结构系列随笔:单链表(c#模拟实现)
- 重温数据结构系列随笔:单链表(c#模拟实现)
- 重温数据结构系列随笔:单链表(c#模拟实现)
- C#实现数据结构中链表示例
- 数据结构-用链表函数实现链表的有序合并
- Java 数据结构链表操作实现代码
- Java中常用数据结构的实现类 Collection和Map
- 一步一步学数据结构之1--1(栈--单链表实现)
- 用C#实现数据结构--树(一)
- C# 使用WebRequest 实现FTP常用功能
- 6.数据结构之通用栈(链表实现)
- c#实现常用排序算法
- 探究数据结构之链表Java代码实现(一)