• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

LinkedListNote and 70-536 Exam

Can anyone help to explain to me how the heck this little program returns "E, B, D, A, C"?

My real problem is that I'm not getting how the LinkedListNode thing works. I understand LinkedList.AddFirst and LinkedList.AddLast...etc. But when it comes to the LinkedListNode = LinkList.AddBefore(LinkedListNode, item) I get really lost. How does this work?

The MSDN explanation sort of confuses me more.
http://msdn.microsoft.com/en-us/library/ahf4c754.aspx


Module Module1
    Sub Main()
 
        Dim v1 As String
        Dim v2 As String
        Dim v3 As String
        Dim v4 As String
        Dim v5 As String
 
        v1 = "A"
        v2 = "B"
        v3 = "C"
        v4 = "D"
        v5 = "E"
 
        Dim vList As New LinkedList(Of String)
        Dim vNode As LinkedListNode(Of String)
        vNode = vList.AddFirst(v2)          ' b - makes sense
        vList.AddFirst(v5)                  ' e, b - makes sense
        vList.AddLast(v3)                   ' e, b, c - makes sense
        vNode = vList.AddAfter(vNode, v1)   ' e, b, a, c - makes sense
        vNode = vList.AddBefore(vNode, v4)  ' ??????????? - not a clue
 
        For Each v As String In vList
            Console.WriteLine("{0}", v)
        Next
 
    End Sub
End Module

Open in new window

0
iammrkcohen
Asked:
iammrkcohen
  • 2
  • 2
3 Solutions
 
ppittleCommented:
In the example above vList.AddAfter returns the Node that was added (ie the 2nd parameter). So after line 21 executes, vNode equals v1.  It could be rewritten as follows:

vList.AddFirst(v2)  'b
vList.AddFirst(v5)  'e, b
vList.AddLast(v3)  'e,b,c
vList.AddAfter(v2, v1) 'e, b, a, c
vList.AddBefore(v1, v4) 'e, b,  d, a c

Let me know if that's still confusing

PJ
0
 
Éric MoreauSenior .Net ConsultantCommented:
after this line:
vNode = vList.AddAfter(vNode, v1)   ' e, b, a, c - makes sense

vNode contains a reference to "a" which is the element that was just inserted

The following line will insert "d" BEFORE vNode wchich is in fact a reference to "a"

vNode = vList.AddBefore(vNode, v4)  ' ??????????? - not a clue
0
 
iammrkcohenAuthor Commented:
So is it fair to say that every time I make an assignment to vNode, it retains the value of the last individual item appended to it?

so:

vNode = vList.AddAfter(vNode, v1) ---> vNode would become v1 here?
vNode = vList.AddBefore(vNode, v3) ---> vNode would become v3 here?
0
 
Éric MoreauSenior .Net ConsultantCommented:
exactly. in fact, it retains a reference to the last node you have inserted.
0
 
iammrkcohenAuthor Commented:
Thanks so much. I've solved several of these correctly now. Now on to the other 10,000 topics for this exam.

Thanks,
Mike
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now