C#定義并實(shí)現(xiàn)單鏈表實(shí)例解析
本文以實(shí)例詳細(xì)描述了C#定義并實(shí)現(xiàn)單鏈表的過程及原理。一般來說C#定義并實(shí)現(xiàn)單鏈表,代碼包括構(gòu)成鏈表的結(jié)點(diǎn)定義、用變量來實(shí)現(xiàn)表頭、清空整個鏈表 、鏈表復(fù)位,使第一個結(jié)點(diǎn)成為當(dāng)前結(jié)點(diǎn)、判斷鏈表是否為空、判斷當(dāng)前結(jié)點(diǎn)是否為最后一個結(jié)點(diǎn)、返回當(dāng)前結(jié)點(diǎn)的下一個結(jié)點(diǎn)的值,并使其成為當(dāng)前結(jié)點(diǎn)、將當(dāng)前結(jié)點(diǎn)移出鏈表,下一個結(jié)點(diǎn)成為當(dāng)前結(jié)點(diǎn)等內(nèi)容。
具體實(shí)現(xiàn)代碼如下所示:
using System;
using System.IO;
// 構(gòu)成鏈表的結(jié)點(diǎn)定義
public class Node
{
public Object data;
public Node next;
public Node( Object d )
{
data = d;
next = null;
}
}
public class List
{
// 用變量來實(shí)現(xiàn)表頭
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;
//清空整個鏈表
public void deleteAll( )
{
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
//鏈表復(fù)位,使第一個結(jié)點(diǎn) 成為當(dāng)前結(jié)點(diǎn)
public void reset( )
{
Pointer = null;
}
//判斷鏈表是否為空
public bool isEmpty( )
{
return (Length == 0);
}
//判斷當(dāng)前結(jié)點(diǎn)是否 為最后一個結(jié)點(diǎn)
public bool isEnd( )
{
if (Length == 0)
throw new System.Exception( );
else if (Length == 1)
return true;
else
return (cursor( ) == Tail);
}
//返回當(dāng)前結(jié)點(diǎn)的下一個結(jié)點(diǎn)的值, 并使其成為當(dāng)前結(jié)點(diǎn)
public Object nextNode( )
{
if (Length == 1)
throw new System.Exception( );
else if (Length == 0)
throw new System.Exception( );
else
{
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data);
else
throw new System.Exception( );
}
}
//返回當(dāng)前結(jié)點(diǎn)的值
public Object currentNode( )
{
Node temp = cursor( );
return temp.data;
}
//在當(dāng)前結(jié)點(diǎn)前插入一個結(jié)點(diǎn), 并使其成為當(dāng)前結(jié)點(diǎn)
public void insert( Object d )
{
Node e = new Node( d );
if (Length == 0)
{
Tail = e;
Head = e;
}
else
{
Node temp = cursor( );
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}
//返回鏈表的大小
public int size( )
{
return Length;
}
//將當(dāng)前結(jié)點(diǎn)移出鏈表,下一個結(jié)點(diǎn)成為當(dāng)前結(jié)點(diǎn)
//如果移出的結(jié)點(diǎn)是最后一個結(jié)點(diǎn),則第一個結(jié)點(diǎn)成為當(dāng)前結(jié)點(diǎn)
public Object remove( )
{
Object temp;
if (Length == 0)
throw new System.Exception( );
else if (Length == 1)
{
temp = Head.data;
deleteAll( );
}
else
{
Node cur = cursor( );
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail)
{
Pointer.next = null;
Tail = Pointer;
reset( );
}
else
Pointer.next = cur.next;
Length--;
}
return temp;
}
//返回當(dāng)前結(jié)點(diǎn)的指針
private Node cursor( )
{
if (Head == null)
throw new System.Exception( );
else if (Pointer == null)
return Head;
else
return Pointer.next;
}
//鏈表的簡單應(yīng)用舉例
public static void Main( )
{
List a = new List();
for (int i = 1; i <= 10; i++)
a.insert( new IntPtr(i));
Console.WriteLine(a.currentNode( ));
while (!a.isEnd( ))
Console.WriteLine(a.nextNode( ));
a.reset();
while (!a.isEnd( ))
{
a.remove( );
}
a.remove( );
a.reset( );
if (a.isEmpty( ))
Console.WriteLine("There is no Node in List!");
Console.WriteLine("You can press return to quit!");
try
{
// 確保用戶看清程序運(yùn)行結(jié)果
Console.Read( );
}
catch (IOException e)
{
}
}
}
- C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實(shí)例詳解
- C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘三 鏈表
- C#實(shí)現(xiàn)的簡單鏈表類實(shí)例
- C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘四 雙向鏈表
- c#泛型學(xué)習(xí)詳解 創(chuàng)建線性鏈表
- C#數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的實(shí)例代碼
- c# 自定義泛型鏈表類的詳解
- C#實(shí)現(xiàn)單鏈表(線性表)完整實(shí)例
- C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法
- C#如何自定義線性節(jié)點(diǎn)鏈表集合
相關(guān)文章
C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
這篇文章主要介紹了C#實(shí)現(xiàn)壓縮和解壓縮的方法,結(jié)合具體實(shí)例形式分析了Gzip和Zip兩種壓縮操作實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06
Winform中實(shí)現(xiàn)圖片格式轉(zhuǎn)換
這篇文章主要介紹了Winform中實(shí)現(xiàn)圖片格式轉(zhuǎn)換的示例代碼,幫助大家更好的理解和使用winform開發(fā),感興趣的朋友可以了解下2020-12-12
C# 設(shè)置防火墻的創(chuàng)建規(guī)則
這篇文章主要介紹了C# 設(shè)置防火墻的創(chuàng)建規(guī)則,幫助大家更好的利用c#操作防火墻,感興趣的朋友可以了解下2020-11-11

