C#資料結構之迴圈鏈表的執行個體代碼

來源:互聯網
上載者:User

複製代碼 代碼如下: 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;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.