C语言

C#数据结构之循环链表的实例代码

时间:2022-12-04 17:20:02 C语言 我要投稿
  • 相关推荐

C#数据结构之循环链表的实例代码

  很多人都不知道C#数据结构中循环链表的代码怎么写,下面小编为大家整理了C#数据结构之循环链表的实例代码,希望能帮到大家!

  复制代码 代码如下:

  public class Node

  {

  public object Element;

  public Node Link;

  public Node()

  {

  Element = null;

  Link = null;

  }

  public Node(object theElement)

  {

  Element = theElement;

  Link = null;

  }

  }

  复制代码 代码如下:

  public class LinkedList

  {

  //头结点

  protected Node Header;

  private int count;

  public LinkedList()

  {

  count = 0;

  Header = new Node("header");

  Header.Link = Header;

  }

  public bool IsEmpty()

  {

  return (Header.Link == null);

  }

  public void MakeEmpty()

  {

  Header.Link = null;

  }

  public void PrintList()

  {

  Node current = new Node();

  current = Header;

  while (current.Link.Element.ToString() != "header")

  {

  Console.WriteLine(current.Link.Element);

  current = current.Link;

  }

  }

  private Node FindPrevious(object n)

  {

  Node current = Header;

  while (!(current.Link == null) && current.Link.Element != n)

  {

  current = current.Link;

  }

  return current;

  }

  private Node Find(object item)

  {

  Node current = new Node();

  current = Header.Link;

  while (current.Element != item)

  {

  current = current.Link;

  }

  return current;

  }

  public void Insert(object newItem, object after)

  {

  Node current = new Node();

  Node newNode = new Node(newItem);

  current = Find(after);

  newNode.Link = current.Link;

  current.Link = newNode;

  count++;

  }

  public void Remove(object n)

  {

  Node p = FindPrevious(n);

  if (!(p.Link == null))

  {

  p.Link = p.Link.Link;

  count--;

  }

  }

  public void InsertFirst(object n)

  {

  Node current = new Node(n);

  current.Link = Header;

  Header.Link = current;

  count++;

  }

  public Node Move(int n)

  {

  Node current = Header.Link;

  Node tmp;

  for (int i = 0; i <= n; i++)

  {

  current = current.Link;

  }

  if (current.Element.ToString() == "header")

  {

  current = current.Link;

  }

  tmp = current;

  return tmp;

  }

  public Node GetFirst()

  {

  return Header;

  }

  }

【C#数据结构之循环链表的实例代码】相关文章:

让网站变灰的css代码实例12-02

java中通用的线程池实例代码08-27

浅谈C#语言的特点10-28

WPS文件如何运行代码09-21

C语言跳出循环10-16

20条PHP代码优化技巧05-06

表示实例的英语单词解析02-07

生活实例在地理教学中的运用研修总结10-01

苏州园林的教案实例(通用17篇)12-23

php实现用户登陆简单实例10-14