VB.Net数据结构系列 第2章 线性表 2.5 实训指导:约瑟夫问题

Code2-4:

CircularLinkedList.vb:

'循环链表
Public Class CircularLinkedList
    Private count As Integer
    Private Tail As Node
    Private currentPrev As Node

    '节点数量,只读
    Public ReadOnly Property Size As Integer
        Get
            Return count
        End Get
    End Property

    '当前指向的节点,只读
    Public ReadOnly Property Current As Object
        Get
            Return currentPrev.next.item
        End Get
    End Property

    '增加节点
    Public Sub Add(ByVal value As Object)
        Dim newNode As New Node(value)
        If IsNothing(Tail) Then
            Tail = newNode
            Tail.next = newNode
            currentPrev = newNode
        Else
            newNode.next = Tail.next
            Tail.next = newNode
            If currentPrev.Equals(Tail) Then
                currentPrev = newNode
            End If
            Tail = newNode
        End If
        count += 1
    End Sub

    '移除当前节点
    Public Sub RemoveCurrentNode()
        If IsNothing(Tail) Then
            Throw New NullReferenceException("集合中没有任何元素!")
        End If
        If count = 1 Then
            Tail = Nothing
            currentPrev = Nothing
        Else
            If currentPrev.next.Equals(Tail) Then
                Tail = currentPrev
            End If
            currentPrev.next = currentPrev.next.next
        End If
        count -= 1
    End Sub

    '移动当前节点
    Public Sub Move(ByVal [step] As Integer)
        If [step] < 0 Then
            Throw New ArgumentOutOfRangeException("step", "移动步数不能小于0!")
        End If
        If IsNothing(Tail) Then
            Throw New NullReferenceException("集合中没有任何元素!")
        End If
        For i As Integer = 0 To [step] - 1
            currentPrev = currentPrev.next
        Next
    End Sub


    Public Function ToString() As String
        If IsNothing(Tail) Then
            Return String.Empty
        End If
        Dim s As String = ""
        Dim tempNode As Node
        tempNode = Tail.next
        For i As Integer = 0 To count - 1
            s &= tempNode.ToString & " "
            tempNode = tempNode.next
        Next
        Return s
    End Function

    '单个节点
    Private Class Node
        Public item As Object
        Public [next] As Node

        Public Sub New(ByVal value As Object)
            item = value
        End Sub

        Public Function ToString() As String
            Return item.ToString
        End Function
    End Class
End Class

Module1.vb:

Module Module1

    Sub Main()
        Dim cLst As New CircularLinkedList
        Dim s As String = String.Empty
        Console.WriteLine("请输入总人数:")
        Dim count As Integer = Integer.Parse(Console.ReadLine)
        Console.WriteLine("请输入数字M的值:")
        Dim m As Integer = Integer.Parse(Console.ReadLine)
        Console.WriteLine("游戏开始")
        For i As Integer = 1 To count
            cLst.Add(i)
        Next
        Console.WriteLine("所有人:" & cLst.ToString)
        Do While (cLst.Size > 1)
            cLst.Move(m)
            s += cLst.Current.ToString & " "
            cLst.RemoveCurrentNode()
            Console.WriteLine("剩余的人:" & cLst.ToString & "   开始数数的人:" & cLst.Current)
        Loop
        Console.WriteLine("出队顺序:" & s & cLst.Current)
        Console.ReadKey()
    End Sub

End Module

 

你可能感兴趣的:(数据结构)