VB.Net数据结构系列 第2章 线性表 2.3线性表的链式存储结构

1、使用关键字作为变量,请在申明的时候将关键字加上中括号:

Public [next] As Node

如果是类的成员,那么在类外调用该成员的时候,将不用再加上中括号。

单向链表中通常将next(可以是属性或者成员)设置为下一个结点。

2、嵌套类(Nested Class)是在类中定义的类。

嵌套类可以访问外部类的方法、属性、字段而不管访问修饰符的限制。但是外部类只能够访问修饰符为public、internal。

3、单向链表中移除结点,需要获得它的前结点,将前结点的下一个结点设置为前结点的下一个结点的下一个结点。

prevNode.next = prevNode.next.next

4、单向链表中插入结点,需要获得它的前结点,将前结点的下一个结点设置为插入结点,插入结点的下一个结点设置为前结点的原下一个结点。

Code2-3

linkedList.vb

Public Class linkedList
    Private size As Integer
    Private head As Node

    Public ReadOnly Property Count As Integer
        Get
            Return size
        End Get
    End Property

    Default Public Property items(ByVal index As Integer) As Object
        Get
            Return getNodeByIndex(index).item
        End Get
        Set(value As Object)
            getNodeByIndex(index).item = value
        End Set
    End Property

    '增加元素
    Public Sub Add(ByVal value As Object)
        Dim newNode As New Node(value)
        '先检查是否有头部节点
        If IsNothing(head) Then
            head = newNode
        Else
            '最后一个节点的基础上再增加节点
            getNodeByIndex(size - 1).next = newNode
        End If
        size += 1
    End Sub

    '在指定索引位置插入节点
    Public Sub Insert(ByVal index As Integer, ByVal value As Object)
        Dim tempNode As Node
        If index = 0 Then
            '检查头部节点是否已经存在
            If IsNothing(head) Then
                head = New Node(value)
            Else
                tempNode = New Node(value)
                tempNode.next = head
                head = tempNode
            End If
        Else
            '获得插入点处前节点
            Dim prevNode As Node = getNodeByIndex(index - 1)
            '获得插入点处后节点
            Dim nextNode As Node = prevNode.next
            tempNode = New Node(value)
            prevNode.next = tempNode
            tempNode.next = nextNode
        End If
        size += 1
    End Sub

    '移除指定索引处节点
    Public Sub RemoveAt(ByVal index As Integer)
        If index = 0 Then
            If IsNothing(head) Then
                Throw New ArgumentOutOfRangeException("index", "索引超出范围")
            Else
                head = head.next
            End If
        Else
            '获得索引处前一个节点
            Dim prevNode As Node = getNodeByIndex(index - 1)
            If IsNothing(prevNode) Then
                Throw New ArgumentOutOfRangeException("index", "索引超出范围")
            Else
                prevNode.next = prevNode.next.next
            End If
        End If
        size -= 1
    End Sub

    Public Overridable Function toString() As String
        Dim s As String = ""
        Dim tempNode As Node = head
        Do While IsNothing(tempNode) = False
            s &= tempNode.ToString & " "
            tempNode = tempNode.next
        Loop
        Return s
    End Function


    '根据索引获得节点
    Public Overridable Function getNodeByIndex(ByVal index As Integer) As Node
        If (index < 0 Or index >= size) Then
            Throw New ArgumentOutOfRangeException("index", "索引超出范围")
        End If
        Dim tempNode As Node = head
        '循环到指定节点
        For i As Integer = 0 To index - 1
            tempNode = tempNode.next
        Next
        Return tempNode
    End Function

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

        '构造函数
        Public Sub New(ByVal value As Object)
            item = value
        End Sub

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

End Class

Module1.vb:

Module Module1
    Sub Main()
        Dim lst As New linkedList
        For i As Integer = 0 To 3
            lst.Add(i)
        Next
        Console.WriteLine(lst.toString)
        lst.Insert(2, 50)
        Console.WriteLine(lst.toString)
        lst.RemoveAt(1)
        lst(2) = 9
        Console.WriteLine(lst.toString)
        Console.ReadKey()
    End Sub
End Module

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值