Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linked list using structures

Status
Not open for further replies.

lewisp

Programmer
Aug 5, 2001
1,238
GB
Hi All,

This is my first post for quite some time, having been active on some of the Oracle forums a while ago. I hope someone out there can point me in the right direction.

I'm picking up VB.Net using Visual Basic 2010. I've used linked lists in C very effectively in the past, and I'd like to acheive the same goal in VB. However, I'm trying to create a linked list of a structure type, and I have a problem trying to 'search' for an element in the list. I've searched around the web but not found anything that conslusively answers my question. Have a look at this code:

Code:
    Structure structType
        Public theDate As Date
        Public theName As String
    End Structure

    Private Sub test()

        Dim structLinkList As New LinkedList(Of structType)
        Dim element As structType

        ' Add the first element
        element.theDate = Now()
        element.theName = "Name 1"
        structLinkList.AddLast(element)

        ' Add another
        element.theDate = Now()
        element.theName = "Name 2"
        structLinkList.AddLast(element)

        ' Add another
        element.theDate = Now()
        element.theName = "Name 3"
        structLinkList.AddLast(element)

        For Each element In structLinkList
            MsgBox(element.theName)
        Next

    End Sub

So now I have 3 elements in my list.

1) Is there anything wrong in the way I have coded this?
2) How do I search for and remove the element where theName = "Name 2"?
 
Thanks for the swift reply. However, I did look at that page and it doesn't specifically answer my question about the use of a structure type. Perhaps I should have been a little clearer in my OP. I want to look for the element of a structure in the linked list, where I know the value of "theName" but I do not know the value of "theDate".
 
I worked out the solution, and it's easier than I thought. I'll share my solution in case anyone else wants to know. Here's my code applied to a bubble sort:

Code:
        Structure holidayStructType
            Public holidayDate As Date
            Public holidayName As String
        End Structure

        Dim element As holidayStructType
        Dim compare As holidayStructType
        Dim swap As Boolean = True

        Do While swap
            swap = False

            For i As Integer = 0 To (holidayLinkList.Count - 2)
                element = holidayLinkList.ElementAt(i)
                compare = holidayLinkList.ElementAt(i + 1)

                If element.holidayDate > compare.holidayDate Then
                    swap = True
                    holidayLinkList.Find(element).Value = compare
                    holidayLinkList.FindLast(compare).Value = element
                End If
            Next
        Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top