a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#定義并實現單鏈表實例解析

2020-01-24 02:39:22
字體:
來源:轉載
供稿:網友

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

具體實現代碼如下所示:

using System;using System.IO;// 構成鏈表的結點定義 public class Node { public Object data; public Node next; public Node( Object d )  { data = d; next = null; }}public class List { // 用變量來實現表頭 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; } //鏈表復位,使第一個結點 成為當前結點 public void reset( )  { Pointer = null; } //判斷鏈表是否為空 public bool isEmpty( )  { return (Length == 0); } //判斷當前結點是否 為最后一個結點 public bool isEnd( )  { if (Length == 0)  throw new System.Exception( ); else if (Length == 1)  return true; else  return (cursor( ) == Tail); } //返回當前結點的下一個結點的值, 并使其成為當前結點 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( ); } } //返回當前結點的值 public Object currentNode( )  { Node temp = cursor( ); return temp.data; } //在當前結點前插入一個結點, 并使其成為當前結點 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; } //將當前結點移出鏈表,下一個結點成為當前結點 //如果移出的結點是最后一個結點,則第一個結點成為當前結點 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; } //返回當前結點的指針 private Node cursor( )  { if (Head == null)  throw new System.Exception( ); else if (Pointer == null)  return Head; else  return Pointer.next; } //鏈表的簡單應用舉例 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  {  // 確保用戶看清程序運行結果  Console.Read( ); }  catch (IOException e)  { } }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人在线视频一区二区 | 国产精品一区二区久久久久 | 成人在线免费观看 | 国产激情视频 | 国产成人精品一区一区一区 | 亚洲欧美一区二区三区久久 | 国产91亚洲 | 久久综合九色综合欧美狠狠 | 一区二区av | 97热在线| 99精品欧美一区二区三区综合在线 | 中国特黄毛片 | 综合国产| 久久狠狠| 日韩国产欧美一区 | 成人18视频在线观看 | 日本xxxwww | 国产成人精品免高潮在线观看 | 日韩一区二区在线播放 | 国产精品美女久久久久aⅴ国产馆 | 欧美三级 欧美一级 | 成人高清视频在线观看 | www.久久.com| 久久久久国产一级毛片高清版小说 | 一区二区三区国产精品 | 一级欧美日韩 | 国产精品一区二区三区四区在线观看 | 在线一区 | 亚洲精品乱码久久久久久蜜桃图片 | 精品一区二区免费视频 | 伊人爽 | 美日韩精品视频 | 一区二区三区视频免费在线观看 | 亚洲日韩欧美一区二区在线 | 日韩一二三区 | 男人的天堂在线视频 | 国产精品亚洲一区二区三区在线 | 日韩一区二区在线观看 | 久久精品一区二区三区四区 | 欧美天堂在线观看 | 日本视频网 |