VB.Netデータ構造シリーズ第2章線形表2.5の実務指導:ジョセフ問題

3494 ワード

Code 2-4:
CurularlinkedList.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
Module 1.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