資料結構(C#):迴圈鏈表

來源:互聯網
上載者:User
迴圈鏈表可以是單鏈表,也可以是雙鏈表。鏈表的尾節點的後繼節點指向頭結點便成了迴圈鏈表。
我們在這裡繼承雙鏈表實現迴圈鏈表,當到達雙鏈表的表尾時,讓遊標指向第0個節點;當到達雙鏈表的開頭時,讓遊標指向結尾節點,這樣就實現了迴圈雙鏈表。結尾用一個經典的約瑟夫問題來作迴圈鏈表的應用樣本。

1.迴圈鏈表代碼:

/*
* File     :   CircularlyLinkedList.cs
* Author   :   Zhenxing Zhou
* Date     :   2008-12-07
* Blog     :   http://www.xianfen.net/
*/
using System;

namespace Xianfen.Net.DataStructure
{
    public class CircularlyLinkedList<T> : DoubleLinkedList<T>
    {
        private DoubleLinkedListNode<T> m_CurrentNode;
        private int m_CurrentIndex;

        public int CurrentIndex
        {
            get { return m_CurrentIndex; }
        }

        public CircularlyLinkedList()
            : base()
        {
            m_CurrentNode = m_Head.Next;
            m_CurrentIndex = 0;
        }

        public CircularlyLinkedList(T t)
            : base(t)
        {
            m_CurrentNode = m_Head.Next;
            m_CurrentIndex = 0;
        }

        public T GetCurrent()
        {
            if (m_Count == 0)
            {
                throw new IndexOutOfRangeException();
            }

            return m_CurrentNode.Value;
        }

        public T GetNext()
        {
            if (m_Count == 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (m_CurrentNode != null)
            {
                m_CurrentNode = m_CurrentNode.Next;
                m_CurrentIndex++;
            }

            if (m_CurrentNode == null)
            {
                m_CurrentNode = m_Head.Next;
                m_CurrentIndex = 0;
            }

            return m_CurrentNode.Value;
        }

        public T GetPrevious()
        {
            if (m_Count == 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (m_CurrentNode != null)
            {
                m_CurrentNode = m_CurrentNode.Prior;
                m_CurrentIndex--;
            }

            if (m_CurrentNode == null || m_CurrentNode == m_Head)
            {
                m_CurrentNode = m_Tail;
                m_CurrentIndex = m_Count - 1;
            }

            return m_CurrentNode.Value;
        }
    }
}

2.用迴圈鏈表解決約瑟夫問題
問題描述:N個人圍成圓圈,從1開始報數,到第M個人令其出列,然後下一個人繼續從1開始報數,到第M個人令其出列,如此下去,直到只剩一個人為止。顯示最後一個人為剩者。
代碼:const int M = 9;
const int N = 7;
CircularlyLinkedList<int> list = new CircularlyLinkedList<int>();

//填充迴圈鏈表
for (int i = 1; i < M; i++)
{
    list.Add(i);
}

int tempCounter = 0;

while (list.Count > 1)
{
    tempCounter++; 
    list.GetPrevious(); 

    // 選中者出列
    if (tempCounter == N)
    {
        tempCounter = 0;
        Console.WriteLine(list.GetCurrent() + " 出列!");
        list.RemoveAt(list.CurrentIndex);
    }
}

Console.WriteLine(list.GetNext() + " 為剩者");

運行結果:2 出列!
3 出列!
1 出列!
7 出列!
4 出列!
8 出列!
6 出列!
5 為剩者

如果把上面高亮顯示的語句改為:list.GetNext();
運行結果變為:7 出列!
6 出列!
8 出列!
2 出列!
5 出列!
1 出列!
3 出列!
4 為剩者

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.