Your own List Data structure : List Yours « Data Structure « VB.Net

VB.Net
1. 2D
2. Application
3. Class
4. Data Structure
5. Database ADO.net
6. Development
7. Event
8. File Directory
9. Generics
10. GUI
11. Language Basics
12. Network Remote
13. Thread
14. Windows System
15. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
VB.Net » Data Structure » List YoursScreenshots 
Your own List Data structure
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.java2s.com"

      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 

           
       
Related examples in the same category
1. Add Item to Most Recent Used Item ListAdd Item to Most Recent Used Item List
2. Remove Item from Most Recent Used Item ListRemove Item from Most Recent Used Item List
3. Change the Max Item Count Allowed Property in Most Recent Used Item ListChange the Max Item Count Allowed Property in Most Recent Used Item List
4. Store Object in Most Recent Used Item ListStore Object in Most Recent Used Item List
w_w___w.j___a_va___2_s._com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.