VB.NET tutorial-delete elements


VB.NET OOP example

Delete element

Step 4: Delete an element of the circularly linkedlist

To delete an element of the circularly linkedlist, you need to consider the followings:

1. If the element to be deleted is the first element of the list and the list contains only one element, you only need to assign null to the pfirst and plast links. If the element to be deleted is the first element of the list and the list contain more than one element, you need a temporary link to point to the pfirst then move the pfirst to point to its next element and set the temporary link to null.

VB.NET Delete the first item of the list  

2. If the element to be deleted is in the middle of the list, you need a traversing link(temp) to point to the element before the element to be deleted and a temporary link(del) to point to the element to be deleted. Then let the next link of the traversing link to point to the next link of the temporary link. To handle situation where the element to be deleted is the last element of the list, you need to test whether the target item is equal to the plast. If it is really equal, you need to update the plast to point to the traversing link. Finally set the temporary link to null.

VB.NET Delete middle item of the list  

'delete an item from the Circularly Linked List
Public Overrides Sub delete(ByVal pos As Integer)
  Dim deleted As Integer = 1
  If (countitem() > 0) Then
  'make sure the list is not empty.
  Dim temp, del As ListNode

  If (pos = 1) Then
    'delete the first item
      If (countitem() = 1) Then 'The list contains only one item
         pfirst = Nothing
         plast = Nothing

     Else 'The list contains more than one item
        temp = pfirst
        pfirst = pfirst.nextlk
       temp = Nothing

       Console.WriteLine("Deleted")

     End If

  ElseIf (pos > 1 And pos <= countitem()) Then
     'delete middle item
     temp = pfirst
     Dim i As Integer
     For i = 1 To pos - 1
        temp = temp.nextlk 'move to the item staying before the target item to be deleted

     Next
     del = temp.nextlk 'target item to be deleted
     temp.nextlk = del.nextlk
     If (Object.Equals(del,plast)) Then
         plast = temp 'delete last item

     End if
     del = Nothing
     Console.WriteLine("Deleted")
     End If
Else
deleted = 0
Console.WriteLine("Invalid position!")
End If

If (deleted <> 0 And Not IsNothing(plast)) Then plast.nextlk = pfirst ' Keep the list circularly linked

Else : Console.WriteLine("No item found")
End If

End Sub




Comments

CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.