Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions
Namespace Rextester
Public Module Program
Const MAXNODE = 50
Const NullPointer = 0
Structure node
Dim Data As Integer
Dim Pointer As Integer
End Structure
Dim List(MAXNODE) As node
Dim StartPointer As Integer
Dim FreeListPtr As Integer
Sub InitJosephus(n as integer)
Dim i As Integer
List(0).Data = 0
for i = 1 to n
InsertNode(n - i + 1)
next
End Sub
Sub InitialiseList()
StartPointer = NullPointer
FreeListPtr = 1
Dim index as Integer
FOR Index = 1 TO MAXNODE - 2
List(Index).Pointer = Index + 1
next
List(MAXNODE - 1).Pointer = NullPointer
End Sub
Sub InsertNode(NewItem as Integer)
Dim NewNodePtr, ThisNodePtr, PreviousNodePtr as Integer
IF FreeListPtr <> NullPointer THEN
NewNodePtr = FreeListPtr
List(NewNodePtr).Data = NewItem
FreeListPtr = List(FreeListPtr).Pointer
ThisNodePtr = StartPointer
PreviousNodePtr = StartPointer
WHILE ThisNodePtr <> NullPointer AND List(ThisNodePtr).Data < NewItem
PreviousNodePtr = ThisNodePtr
ThisNodePtr = List(ThisNodePtr).Pointer
END WHILE
IF PreviousNodePtr = StartPointer THEN
List(NewNodePtr).Pointer = StartPointer
StartPointer = NewNodePtr
ELSE
List(NewNodePtr).Pointer = List(PreviousNodePtr).Pointer
List(PreviousNodePtr).Pointer = NewNodePtr
END IF
END IF
END Sub
Sub OutputAllNodes()
Dim CurrentNodePtr As Integer = StartPointer
Console.WriteLine()
Console.writeLine("All nodes are:")
While CurrentNodePtr <> 0
console.write(" {0} {1} ", List(CurrentNodePtr).Data, List(CurrentNodePtr).Pointer)
CurrentNodePtr = List(CurrentNodePtr).Pointer
End While
Console.writeLine()
End Sub
Sub Main(args() As string)
InitialiseList()
InitJosephus(5)
OutputAllNodes()
End Sub
End Module
End Namespace
|