双向循环链表的实现(粗糙版)

双向链表元素类定义:

Public Class UBaseBidirectianalLinkedItem

    Implements IDisposable





    Private disposedValue As Boolean = False        ' 检测冗余的调用



    ' IDisposable

    <System.Diagnostics.DebuggerNonUserCode()> _

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)

        If Not Me.disposedValue Then

            If disposing Then

                ' TODO: 显式调用时释放非托管资源

            End If



            ' TODO: 释放共享的非托管资源

        End If

        Me.disposedValue = True

    End Sub



#Region " IDisposable Support "

    ' Visual Basic 添加此代码是为了正确实现可处置模式。

    Public Sub Dispose() Implements IDisposable.Dispose

        ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。

        Dispose(True)

        GC.SuppressFinalize(Me)

    End Sub

#End Region



    '链表KEY

    Private _key As String = String.Empty

    Public Property Key() As String

        Get

            Return _key

        End Get

        Set(ByVal value As String)

            _key = value

        End Set

    End Property

    '链表表示VALUE

    Private _value As String = String.Empty

    Public Property Value() As String

        Get

            Return _value

        End Get

        Set(ByVal value As String)

            _value = value

        End Set

    End Property

    '构造函数1(无参数)

    Sub New()

    End Sub

    '构造函数2(Key,Value)

    Sub New(ByVal key As String, ByVal value As String)

        Me._key = key

        Me._value = value

    End Sub

    '前驱结点

    Private _preKey As UBaseBidirectianalLinkedItem = Nothing

    Public Property PreKey() As UBaseBidirectianalLinkedItem

        Get

            Return _preKey

        End Get

        Set(ByVal value As UBaseBidirectianalLinkedItem)

            _preKey = value

        End Set

    End Property

    '后继结点

    Private _nextKey As UBaseBidirectianalLinkedItem = Nothing

    Public Property NextKey() As UBaseBidirectianalLinkedItem

        Get

            Return _nextKey

        End Get

        Set(ByVal value As UBaseBidirectianalLinkedItem)

            _nextKey = Value

        End Set

    End Property

    '等于操作符

    Public Shared Operator =(ByVal item1 As UBaseBidirectianalLinkedItem, _

                             ByVal item2 As UBaseBidirectianalLinkedItem) As Boolean



        If item1 Is Nothing And item2 Is Nothing Then

            Return True

        Else

            Return False

        End If

        If item1.Key.Equals(item2.Key) Then

            Return True

        Else

            Return False

        End If

    End Operator

    '不等于操作符

    Public Shared Operator <>(ByVal item1 As UBaseBidirectianalLinkedItem, _

                             ByVal item2 As UBaseBidirectianalLinkedItem) As Boolean

        If item1 Is Nothing And item2 Is Nothing Then

            Return False

        Else

            Return True

        End If

        If item1.Key.Equals(item2.Key) = False Then

            Return True

        Else

            Return False

        End If

    End Operator







End Class
链表类定义:
Public Class UBaseBidirectianalLinkedList

    Implements IDisposable





    Private disposedValue As Boolean = False        ' 检测冗余的调用



    ' IDisposable

    <System.Diagnostics.DebuggerNonUserCode()> _

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)

        If Not Me.disposedValue Then

            If disposing Then

                ' TODO: 显式调用时释放非托管资源

            End If



            ' TODO: 释放共享的非托管资源

        End If

        Me.disposedValue = True

    End Sub



#Region " IDisposable Support "

    ' Visual Basic 添加此代码是为了正确实现可处置模式。

    Public Sub Dispose() Implements IDisposable.Dispose

        ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。

        Dispose(True)

        GC.SuppressFinalize(Me)

    End Sub

#End Region



    '双向链表

    Private _listHead As UBaseBidirectianalLinkedItem = Nothing

    Public Property ListHead() As UBaseBidirectianalLinkedItem

        Get

            Return _listHead

        End Get

        Set(ByVal value As UBaseBidirectianalLinkedItem)

            _listHead = value

            _listHead.PreKey = _listHead

            _listHead.NextKey = _listHead

        End Set

    End Property



    '获得指定Key的节点对象

    Public ReadOnly Property Items(ByVal key As String) As UBaseBidirectianalLinkedItem

        Get

            Return GetItem(Me.ListHead, key)

        End Get

    End Property

    Private Function GetItem(ByVal item As UBaseBidirectianalLinkedItem, _

                             ByVal key As String) _

                                As UBaseBidirectianalLinkedItem

        Try

            Static cnt As Integer

            If cnt = _count + 1 Then

                Return Nothing

            End If

            If item.Key.Equals(key) Then

                Return item

            End If

            cnt += 1

            Return GetItem(item.NextKey, key)

        Catch ex As Exception

            Throw ex

        End Try

    End Function



    '结点个数

    Private _count As Integer = 0

    Public ReadOnly Property Count() As String

        Get

            Return _count

        End Get

    End Property



    '构造函数

    Sub New()

        '设置

        _listHead = New UBaseBidirectianalLinkedItem()

        _listHead.PreKey = _listHead

        _listHead.NextKey = _listHead

        _count += 1

    End Sub



    '构造函数

    Sub New(ByVal key As String, _

            ByVal value As String)

        '设置

        _listHead = New UBaseBidirectianalLinkedItem(key, value)

        _listHead.PreKey = _listHead

        _listHead.NextKey = _listHead

        _count += 1

    End Sub

    '添加结点

    Public Sub Add(ByVal listHead As UBaseBidirectianalLinkedItem, _

                   ByVal nextItem As UBaseBidirectianalLinkedItem)

        Dim thisItem As UBaseBidirectianalLinkedItem = listHead

        nextItem.NextKey = thisItem.NextKey

        nextItem.PreKey = thisItem

        thisItem.NextKey.PreKey = nextItem

        thisItem.NextKey = nextItem

        _count += 1

    End Sub

    '删除结点

    Public Sub Delete(ByVal listHead As UBaseBidirectianalLinkedItem, _

                      ByVal key As String)

        If listHead = listHead.NextKey Then

            '抛出异常

            Throw New Exception("UBaseBidirectianalLinkedItem:Dou't has any next item.")

        Else

            Dim thisItem As UBaseBidirectianalLinkedItem = listHead.NextKey

            Do While thisItem = listHead

                If key.Equals(thisItem.Key) Then

                    thisItem.NextKey.PreKey = thisItem.PreKey

                    thisItem.PreKey.NextKey = thisItem.NextKey

                    thisItem.PreKey = Nothing

                    thisItem.NextKey = Nothing

                    thisItem = Nothing

                    _count -= 1

                    Exit Do

                End If

                thisItem = thisItem.NextKey

            Loop

            '抛出异常

            Throw New Exception("UBaseBidirectianalLinkedItem:Can found the item mapping with the key.")

        End If

    End Sub

    '删除所有结点

    Public Sub Clear()

        If _listHead Is Nothing Then

            _count = 0

            Return

        End If

        Clear(_listHead.NextKey)

        If _listHead.NextKey = Nothing And _listHead.PreKey = Nothing Then

            _listHead = Nothing

            _count = 0

            Return

        End If

        _count = 0

    End Sub

    '删除单个结点

    Public Sub Clear(ByVal linkitem As UBaseBidirectianalLinkedItem)

        If linkitem.NextKey Is Nothing Then

            Return

        End If

        linkitem.PreKey.NextKey = linkitem.NextKey

        linkitem.NextKey.PreKey = linkitem.PreKey

        Dim thisitem As UBaseBidirectianalLinkedItem = linkitem.NextKey

        linkitem.NextKey = Nothing

        linkitem.PreKey = Nothing

        Clear(thisitem)

    End Sub



End Class

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值