在不使用面向对象编程的情况下创建有序列表的链表

create linked list of ordered list without using object-oriented programming

当从 main 函数传递时,以下代码应该将 6 4 3 输出为 3 4 6。我需要使用以下标识符的解决方案。我找到了替代的面向对象的解决方案,但我不能使用 null。当我尝试引入这些概念时,我无法使用以下所有标识符。我之所以如此关注以下代码,是因为此解决方案以伪代码形式在 A 级计算机科学书籍中提供。 我还没有理解像 startpointer

这样的标识符的必要性
Dim nullPointer As Integer = -1

    'nullpointer should be set to -1 if using array element with index 0
    Public Structure ListNode
        Public Data As Integer
        Public Pointer As Integer
    End Structure
    'declare record type to store data and pointer


    Dim StartPointer As Integer
    Dim FreeListPtr As Integer
    Dim List(6) As ListNode
    Dim newItem As Integer
    Dim NewNodePtr As Integer
    Dim ThisNodePtr As Integer
    Dim previousNodePtr As Integer
Sub InitialiseList()
        StartPointer = nullPointer 'set start pointer
        FreeListPtr = 1            'set starting position of free list
        For index As Integer = 0 To 5 'link all nodes to make free list
            List(index).Pointer = index + 1
        Next
        List(6).Pointer = nullPointer 'last node of free list    
    End Sub
 Sub InsertNode(ByVal newItem)
        StartPointer = 0
        If FreeListPtr <> nullPointer Then 'there is space in the array
            'take node from free list and store data item
            NewNodePtr = FreeListPtr
            List(NewNodePtr).Data = newItem
            FreeListPtr = List(FreeListPtr).Pointer 'find insertion point
            ThisNodePtr = StartPointer 'start at beginning of list

            While ThisNodePtr <> nullPointer And List(ThisNodePtr).Data < newItem
                'while not end ofl list
                previousNodePtr = ThisNodePtr 'remember this node follow the pointer to the next node
                ThisNodePtr = List(ThisNodePtr).Pointer
            End While


            If previousNodePtr = StartPointer Then 'insert new node at start of list
                List(NewNodePtr).Pointer = StartPointer
                StartPointer = NewNodePtr
            Else 'insert new node between previous node and this node
                List(NewNodePtr).Pointer = List(previousNodePtr).Pointer
                List(previousNodePtr).Pointer = NewNodePtr
            End If

        End If
    End Sub

在我执行代码后,看看我的代码如何 运行 借助 while 语句上的断点。循环 运行 在函数第三次执行时无限。

 Dim currentnodeptr As Integer
Sub OutputAllNodes()
        StartPointer = 0
        currentnodeptr = StartPointer
        While currentnodeptr <> nullPointer
            console.writeline(list(currentnodeptr).data)
            currentnodeptr = List(currentnodeptr).Pointer
            currentnodeptr = currentnodeptr + 1
        End While

    End Sub

startpointer 必须为 0 否则会出现问题,因为 nullpointer 为 -1 并且会触发 indexoutofbound 错误

Sub Main()
        InitialiseList()
        InsertNode(6)
        InsertNode(4)
        InsertNode(3)
        OutputAllNodes()
    End Sub

无限时间我得到的输出是 0 和 6

我想如果你修复你的无限循环,一切都会好起来的。

Sub OutputAllNodes()
    StartPointer = 0
    currentnodeptr = StartPointer
    'While currentnodeptr <> nullPointer
    '    Console.WriteLine(List(currentnodeptr).Data)
    '    currentnodeptr = List(currentnodeptr).Pointer
    '    currentnodeptr = currentnodeptr + 1
    'End While
    For Each item In List
        Console.WriteLine($"Data {item.Data}, Pointer {item.Pointer}")
    Next
End Sub

在 InsertNode Sub rem 出以下内容

        'If previousNodePtr = StartPointer Then 'insert new node at start of list
        '    'List(NewNodePtr).Pointer = StartPointer 'Don't reset the pointer
        '    StartPointer = NewNodePtr
        'Else 'insert new node between previous node and this node
        '    List(NewNodePtr).Pointer = List(previousNodePtr).Pointer
        '    List(previousNodePtr).Pointer = NewNodePtr
        'End If

我的输出

Data 0, Pointer 1
Data 6, Pointer 2
Data 4, Pointer 3
Data 3, Pointer 4
Data 0, Pointer 5
Data 0, Pointer 6
Data 0, Pointer -1

编辑

Public Structure ListNode
    Public Data As Integer
    Public Pointer As Integer
End Structure

Private CurrentIndex As Integer
Private NodeList(6) As ListNode 'Seven elements in the array

Sub Main()
    InitialiseList()
    InsertNode(6)
    InsertNode(4)
    InsertNode(3)
    OutputAllNodes()
End Sub

Private Sub InsertNode(input As Integer)
    NodeList(CurrentIndex).Data = input
    CurrentIndex += 1
End Sub

Sub InitialiseList()
    For index As Integer = 0 To 5 'link all nodes to make free list
        NodeList(index).Pointer = index + 1
    Next
    NodeList(6).Pointer = -1 'last node of free list  
End Sub

Sub OutputAllNodes()
    For Each item In NodeList
        Console.WriteLine($"Data {item.Data}, Pointer {item.Pointer}")
    Next
End Sub

输出

Data 6, Pointer 1
Data 4, Pointer 2
Data 3, Pointer 3
Data 0, Pointer 4
Data 0, Pointer 5
Data 0, Pointer 6
Data 0, Pointer -1