VB.NET tutorial: add element to circularly linked list


VB.NET OOP example

Add element to circularly linked list

Step 2: Add an element to the circularly linked list

To add an element to the circularly linked list, you need to consider 4 things:

1. When the list is empty, to add a new element to the list, you only let the pfirst and plast point to the new item.

VB.NET Add an item to the empty list  



2.If the new element is to be added to the beginning of  the list, you will need to let the next link of the new item points to the pfirst and then update the pfirst to point to the new item.

VB.NET Add a new element at the beginning of the list  

 3. If the new element is to be added to the middle position of the list, you need to let a pointer point to the position immediately before the position that the new element will be placed in.

VB.NET Add a new item in the middle of the list  

4. If the new element is to be added to the last of the list, you need to let the link of the plast point to the new element then update the plast to point the new element.

VB.NET Add the new item to the last of the list  

Note: After the new item was inserted to the list, the next link of the plast is pointed to the pfirst. By doing this, all items of the list are linked together circularly.

'Add a new item to the Circularly LinkedList
Public Overrides Sub insert(ByVal val As Integer, ByVal pos As Integer)

  Dim newnode As ListNode = New ListNode(val)

  'empty list
  Dim inserted As Integer = 1
  If IsNothing(pfirst) And IsNothing(plast) Then

      pfirst = newnode
      plast = newnode
      Console.WriteLine("Inserted:{0}", newnode.val)

  'Insert at the beginning of the CirLinkedList
  ElseIf (pos = 1) Then

      newnode.nextlk = pfirst
      pfirst = newnode
      Console.WriteLine("Inserted:{0}", newnode.val)

  'Insert in the middle of the list
   ElseIf (pos > 1 And pos <= countitem()) Then

      Dim ta As ListNode
      Dim t As Integer
      ta = pfirst
      For t = 1 To pos - 2
          ta = ta.nextlk
      Next
      newnode.nextlk = ta.nextlk
      ta.nextlk = newnode
      Console.WriteLine("Inserted:{0}", newnode.val)

  ElseIf (pos = countitem() + 1) Then

     plast.nextlk = newnode
     plast = newnode
     Console.WriteLine("Inserted:{0}", newnode.val)


  Else
      inserted = 0
     Console.WriteLine("Invalid position!")
   End If

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


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.