VB.Net/Data Structure/List Yours

Материал из VB Эксперт
Версия от 12:45, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add Item to Most Recent Used Item List

Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Public Class MainClass
   Shared Sub Main()
       Dim the_items As New MostRecentList(Of String)(4)
       the_items.Add("A")
       the_items.Add("B")
       the_items.Add("C")
       the_items.Add("D")
       the_items.Add("D")
       the_items.Add("B")
       the_items.Add("F")
       For i As Integer = 0 To the_items.Count - 1
            Console.WriteLine(the_items.Item(i))
       Next i
   End Sub 
End Class

Public Class MostRecentList(Of ItemType)
    Public Sub New(ByVal max_items As Integer)
        MaxItems = max_items
    End Sub
    Private m_Items As New List(Of ItemType)
    <Description("The items."), _
     Category("Data")> _
    Public Property Item(ByVal index As Integer) As ItemType
        Get
            Return m_Items(index)
        End Get
        Set(ByVal value As ItemType)
            m_Items(index) = value
        End Set
    End Property
    Private m_MaxItems As Integer = 4
    <Description("The maximum number of items in the list."), _
     Category("Data")> _
    Public Property MaxItems() As Integer
        Get
            Return m_MaxItems
        End Get
        Set(ByVal value As Integer)
            m_MaxItems = value
            Do While m_Items.Count > m_MaxItems
                m_Items.RemoveAt(m_Items.Count - 1)
            Loop
        End Set
    End Property
    <Description("The current number of items."), _
     Category("Data")> _
    Public ReadOnly Property Count() As Integer
        Get
            Return m_Items.Count
        End Get
    End Property
    Public Sub Add(ByVal value As ItemType)
        If m_Items.Contains(value) Then m_Items.Remove(value)
        m_Items.Insert(0, value)
        If m_Items.Count > m_MaxItems Then m_Items.RemoveAt(m_Items.Count - 1)
    End Sub
    Public Sub Remove(ByVal value As ItemType)
        m_Items.Remove(value)
    End Sub
    Public Sub RemoveAt(ByVal index As Integer)
        m_Items.RemoveAt(index)
    End Sub
End Class


Change the Max Item Count Allowed Property in Most Recent Used Item List

Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Public Class MainClass
   Shared Sub Main()
       Dim the_items As New MostRecentList(Of String)(4)
       the_items.Add("A")
       the_items.Add("B")
       the_items.Add("C")
       the_items.Add("D")
       the_items.Add("D")
       the_items.Add("B")
       the_items.Add("F")
       For i As Integer = 0 To the_items.Count - 1
            Console.WriteLine(the_items.Item(i))
       Next i
       the_items.MaxItems = 2
       Console.WriteLine()
       For i As Integer = 0 To the_items.Count - 1
            Console.WriteLine(the_items.Item(i))
       Next i

   End Sub 
End Class

Public Class MostRecentList(Of ItemType)
    Public Sub New(ByVal max_items As Integer)
        MaxItems = max_items
    End Sub
    Private m_Items As New List(Of ItemType)
    <Description("The items."), _
     Category("Data")> _
    Public Property Item(ByVal index As Integer) As ItemType
        Get
            Return m_Items(index)
        End Get
        Set(ByVal value As ItemType)
            m_Items(index) = value
        End Set
    End Property
    Private m_MaxItems As Integer = 4
    <Description("The maximum number of items in the list."), _
     Category("Data")> _
    Public Property MaxItems() As Integer
        Get
            Return m_MaxItems
        End Get
        Set(ByVal value As Integer)
            m_MaxItems = value
            Do While m_Items.Count > m_MaxItems
                m_Items.RemoveAt(m_Items.Count - 1)
            Loop
        End Set
    End Property
    <Description("The current number of items."), _
     Category("Data")> _
    Public ReadOnly Property Count() As Integer
        Get
            Return m_Items.Count
        End Get
    End Property
    Public Sub Add(ByVal value As ItemType)
        If m_Items.Contains(value) Then m_Items.Remove(value)
        m_Items.Insert(0, value)
        If m_Items.Count > m_MaxItems Then m_Items.RemoveAt(m_Items.Count - 1)
    End Sub
    Public Sub Remove(ByVal value As ItemType)
        m_Items.Remove(value)
    End Sub
    Public Sub RemoveAt(ByVal index As Integer)
        m_Items.RemoveAt(index)
    End Sub
End Class


Remove Item from Most Recent Used Item List

Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Public Class MainClass
   Shared Sub Main()
       Dim the_items As New MostRecentList(Of String)(4)
       the_items.Add("A")
       the_items.Add("B")
       the_items.Add("C")
       the_items.Add("D")
       the_items.Add("D")
       the_items.Add("B")
       the_items.Add("F")
       For i As Integer = 0 To the_items.Count - 1
            Console.WriteLine(the_items.Item(i))
       Next i
       the_items.Remove("F")
       Console.WriteLine()
       For i As Integer = 0 To the_items.Count - 1
            Console.WriteLine(the_items.Item(i))
       Next i

   End Sub 
End Class

Public Class MostRecentList(Of ItemType)
    Public Sub New(ByVal max_items As Integer)
        MaxItems = max_items
    End Sub
    Private m_Items As New List(Of ItemType)
    <Description("The items."), _
     Category("Data")> _
    Public Property Item(ByVal index As Integer) As ItemType
        Get
            Return m_Items(index)
        End Get
        Set(ByVal value As ItemType)
            m_Items(index) = value
        End Set
    End Property
    Private m_MaxItems As Integer = 4
    <Description("The maximum number of items in the list."), _
     Category("Data")> _
    Public Property MaxItems() As Integer
        Get
            Return m_MaxItems
        End Get
        Set(ByVal value As Integer)
            m_MaxItems = value
            Do While m_Items.Count > m_MaxItems
                m_Items.RemoveAt(m_Items.Count - 1)
            Loop
        End Set
    End Property
    <Description("The current number of items."), _
     Category("Data")> _
    Public ReadOnly Property Count() As Integer
        Get
            Return m_Items.Count
        End Get
    End Property
    Public Sub Add(ByVal value As ItemType)
        If m_Items.Contains(value) Then m_Items.Remove(value)
        m_Items.Insert(0, value)
        If m_Items.Count > m_MaxItems Then m_Items.RemoveAt(m_Items.Count - 1)
    End Sub
    Public Sub Remove(ByVal value As ItemType)
        m_Items.Remove(value)
    End Sub
    Public Sub RemoveAt(ByVal index As Integer)
        m_Items.RemoveAt(index)
    End Sub
End Class


Store Object in Most Recent Used Item List

Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Public Class MainClass
   Shared Sub Main()
       Dim the_items As New MostRecentList(Of DataItem)(4)
       Dim new_data As New DataItem
       new_data.Text = "A"
       the_items.Add(new_data)       
       new_data = new DataItem
       new_data.Text = "B"
       the_items.Add(new_data)       
       
       For i As Integer = 0 To the_items.Count - 1
            Console.WriteLine(the_items.Item(i).Text)
       Next i

   End Sub 
End Class

Public Class MostRecentList(Of ItemType As IComparable)
    " Initialize MaxItems for the new list.
    Public Sub New(ByVal max_items As Integer)
        MaxItems = max_items
    End Sub
    " The Item property.
    Private m_Items As New List(Of ItemType)
    <Description("The items."), _
     Category("Data")> _
    Public Property Item(ByVal index As Integer) As ItemType
        Get
            Return m_Items(index)
        End Get
        Set(ByVal value As ItemType)
            m_Items(index) = value
        End Set
    End Property
    " The MaxItems property.
    Private m_MaxItems As Integer = 4
    <Description("The maximum number of items in the list."), _
     Category("Data")> _
    Public Property MaxItems() As Integer
        Get
            Return m_MaxItems
        End Get
        Set(ByVal value As Integer)
            m_MaxItems = value
            " Resize appropriately.
            Do While m_Items.Count > m_MaxItems
                m_Items.RemoveAt(m_Items.Count - 1)
            Loop
        End Set
    End Property
    " The current number of items.
    <Description("The current number of items."), _
     Category("Data")> _
    Public ReadOnly Property Count() As Integer
        Get
            Return m_Items.Count
        End Get
    End Property
    " Add an item to the top of the list.
    Public Sub Add(ByVal value As ItemType)
        " Remove the item if it is present.
        Remove(value)
        " Add the item to the top of the list.
        m_Items.Insert(0, value)
        " Make sure there are at most MaxItems items.
        If m_Items.Count > m_MaxItems Then m_Items.RemoveAt(m_Items.Count - 1)
    End Sub
    " Remove an item.
    Public Sub Remove(ByVal value As ItemType)
        " Find the item.
        For i As Integer = m_Items.Count - 1 To 0 Step -1
            If value.rupareTo(m_Items(i)) = 0 Then
                m_Items.RemoveAt(i)
            End If
        Next i
    End Sub
    " Remove an item at a specific position.
    Public Sub RemoveAt(ByVal index As Integer)
        m_Items.RemoveAt(index)
    End Sub
End Class
Public Class DataItem
    Implements IComparable
    Public Text As String
    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.rupareTo
        Dim data_item As DataItem = DirectCast(obj, DataItem)
        Return String.rupare(Me.Text, data_item.Text)
    End Function
End Class


Your own List Data structure

Imports System
Public Class MainClass
  Public Shared Sub Main()
      Dim list As List = New List() 
      Dim aBoolean As Boolean = True
      Dim aCharacter As Char = "$"c
      Dim anInteger As Integer = 34
      Dim aString As String = "www.vbex.ru"
      list.InsertAtFront(aBoolean) 
      Console.WriteLine(list)
      list.InsertAtFront(aCharacter)
      Console.WriteLine(list)
      list.InsertAtBack(anInteger) 
      Console.WriteLine(list)
      list.InsertAtBack(aString) 
      Console.WriteLine(list)
      Dim removedObject As Object
      Try
         removedObject = list.RemoveFromFront()
         Console.WriteLine(Convert.ToString(removedObject) & " removed")
         Console.WriteLine(list)
         removedObject = list.RemoveFromFront()
         Console.WriteLine(Convert.ToString(removedObject) & _
            " removed")
         Console.WriteLine(list)
         removedObject = list.RemoveFromBack()
         Console.WriteLine(Convert.ToString(removedObject) & _
            " removed")
         Console.WriteLine(list)
         removedObject = list.RemoveFromBack()
         Console.WriteLine(Convert.ToString(removedObject) & _
            " removed")
         Console.WriteLine(list)
      Catch emptyListException As EmptyListException
         Console.Error.WriteLine(vbCrLf & _
            Convert.ToString(emptyListException))
      End Try
  End Sub
End Class

Public Class List
   Private firstNode As ListNode
   Private lastNode As ListNode
   Private name As String
   Public Sub New(ByVal listName As String)
      name = listName
      firstNode = Nothing
      lastNode = Nothing
   End Sub
   Public Sub New()
      MyClass.New("list")
   End Sub 
   Public Sub InsertAtFront(ByVal insertItem As Object)
      SyncLock (Me)
         If IsEmpty() Then
            lastNode = New ListNode(insertItem)
            firstNode = lastNode
         Else 
            firstNode = New ListNode(insertItem, firstNode)
         End If
      End SyncLock
   End Sub
   Public Sub InsertAtBack(ByVal insertItem As Object)
      SyncLock (Me)
         If IsEmpty() Then
            lastNode = New ListNode(insertItem)
            firstNode = lastNode
         Else 
            lastNode.NextNode = New ListNode(insertItem)
            lastNode = lastNode.NextNode
         End If
      End SyncLock
   End Sub
   Public Function RemoveFromFront() As Object
      SyncLock (Me)
         Dim removeItem As Object = Nothing
         If IsEmpty() Then
            Throw New EmptyListException(name)
         End If
         removeItem = firstNode.Data 
         If firstNode Is lastNode Then
            firstNode = Nothing
            lastNode = Nothing
         Else
            firstNode = firstNode.NextNode
         End If
         
         Return removeItem
      End SyncLock
   End Function
   Public Function RemoveFromBack() As Object
      SyncLock (Me)
         Dim removeItem As Object = Nothing
         If IsEmpty() Then
            Throw New EmptyListException(name)
         End If
         removeItem = lastNode.Data
         If firstNode Is lastNode Then
            lastNode = Nothing
            firstNode = lastNode
         Else
            Dim current As ListNode = firstNode
            While (Not (current.NextNode Is lastNode))
               current = current.NextNode
            End While
            lastNode = current
            current.NextNode = Nothing
         End If
         Return removeItem
      End SyncLock
   End Function
   Public Function IsEmpty() As Boolean
      SyncLock (Me)
         If firstNode Is Nothing Then
            Return True
         Else
            Return False
         End If
      End SyncLock
   End Function
   Public Overrides Function ToString() As String
      Dim str As String = New String("")
      SyncLock (Me)
         If IsEmpty() Then
            Return "Empty " & name
         End If
         str = "The " & name & " is: "
         Dim current As ListNode = firstNode
         While Not current Is Nothing
            str += current.Data & " "
            current = current.NextNode
         End While
         
         Return str
      
      End SyncLock
   End Function
End Class 

Public Class ListNode
   Private mData As Object
   Private mNextNode As ListNode
   Public Sub New(ByVal dataValue As Object)
      MyClass.New(dataValue, Nothing)
   End Sub 
   Public Sub New(ByVal dataValue As Object, _
      ByVal nextNodeValue As Object)
      mData = dataValue
      mNextNode = nextNodeValue
   End Sub 
   Public ReadOnly Property Data() As Object
      Get
         Return mData
      End Get
   End Property
   Public Property NextNode() As ListNode
      Get
         Return mNextNode
      End Get
      Set(ByVal value As ListNode)
         mNextNode = value
      End Set
   End Property
End Class

Public Class EmptyListException
   Inherits ApplicationException
   Public Sub New(ByVal name As String)
      MyBase.New("The " & name & " is empty")
   End Sub 
End Class