最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#定義并實現(xiàn)單鏈表實例解析

 更新時間:2014年07月17日 14:40:12   投稿:shichen2014  
這篇文章主要介紹了C#定義并實現(xiàn)單鏈表實例解析,有助于讀者加深對C#實現(xiàn)數(shù)據(jù)結(jié)構(gòu)的理解,需要的朋友可以參考下

本文以實例詳細描述了C#定義并實現(xiàn)單鏈表的過程及原理。一般來說C#定義并實現(xiàn)單鏈表,代碼包括構(gòu)成鏈表的結(jié)點定義、用變量來實現(xiàn)表頭、清空整個鏈表 、鏈表復(fù)位,使第一個結(jié)點成為當前結(jié)點、判斷鏈表是否為空、判斷當前結(jié)點是否為最后一個結(jié)點、返回當前結(jié)點的下一個結(jié)點的值,并使其成為當前結(jié)點、將當前結(jié)點移出鏈表,下一個結(jié)點成為當前結(jié)點等內(nèi)容。

具體實現(xiàn)代碼如下所示:

using System;
using System.IO;
// 構(gòu)成鏈表的結(jié)點定義 
public class Node 
{
 public Object data;
 public Node next;
 public Node( Object d ) 
 {
 data = d;
 next = null;
 }
}
public class List 
{
 // 用變量來實現(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é)點 成為當前結(jié)點
 public void reset( ) 
 {
 Pointer = null;
 }
 //判斷鏈表是否為空
 public bool isEmpty( ) 
 {
 return (Length == 0);
 }
 //判斷當前結(jié)點是否 為最后一個結(jié)點
 public bool isEnd( ) 
 {
 if (Length == 0)
  throw new System.Exception( );
 else if (Length == 1)
  return true;
 else
  return (cursor( ) == Tail);
 }
 //返回當前結(jié)點的下一個結(jié)點的值, 并使其成為當前結(jié)點
 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( );
 }
 }
 //返回當前結(jié)點的值
 public Object currentNode( ) 
 {
 Node temp = cursor( );
 return temp.data;
 }
 //在當前結(jié)點前插入一個結(jié)點, 并使其成為當前結(jié)點
 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;
 }
 //將當前結(jié)點移出鏈表,下一個結(jié)點成為當前結(jié)點
 //如果移出的結(jié)點是最后一個結(jié)點,則第一個結(jié)點成為當前結(jié)點
 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;
 }
 //返回當前結(jié)點的指針
 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 
 {
  // 確保用戶看清程序運行結(jié)果
  Console.Read( );
 } 
 catch (IOException e) 
 {
 }
 }
}

相關(guān)文章

最新評論

祥云县| 莲花县| 疏附县| 临桂县| 宝鸡市| 宾川县| 崇左市| 弥勒县| 韩城市| 巴塘县| 姚安县| 岑溪市| 清丰县| 高密市| 永定县| 平度市| 宁乡县| 林口县| 兰坪| 宁明县| 永丰县| 涡阳县| 隆化县| 丹凤县| 深圳市| 昔阳县| 广南县| 东丰县| 贵德县| 南澳县| 宜兰县| 卫辉市| 古浪县| 墨脱县| 崇明县| 灵寿县| 夏津县| 留坝县| 公主岭市| 凤翔县| 于都县|