Henry Instructions - NET Data Structure Single List (2)

zhaozj2021-02-16  52

Henry Instructions - NET Data Structure Single List (2)

Han Rui (06/15/2003)

3.4 Find in the linked list according to the index position or data element value

Location in the list is the basis for operation, we define two protected findings in the category:

Protected Overridable Function Findby Index (Byval Index As Integer) AS ListNode

'Look for nodes in the list through index

DIM Tempindex as integer = 0

DIM CURRENT As ListNode = Head.NextNode 'starts from the first node after the head knot

DIM RETURNVALUE As ListNode = Nothing 'Initialization Return Node

DO 'loop lookup

IF index = Tempindex Then

ReturnValue = CURRENT

Else

Current = current.nextNode

Tempindex = 1

END IF

LOOP Until Current Is Nothing or Not ReturnValue Is Nothing

Return ReturnValue

END FUNCTION

Protected Overridable Function FindByValue (Byval Value As Object) AS Integer

'Take the data value to find the index of the node in the list

DIM Tempindex as integer = 0

DIM CURRENT As ListNode = Head.NextNode 'starts from the head knot

DIM RETURNVALUE AS INTEGER = -1 'Initialization Return Value

DO 'loop lookup

IF value.equals (current.data) THEN

ReturnValue = Tempindex

Else

Current = current.nextNode

Tempindex = 1

END IF

Loop unsil current is nothing or returnValue> -1

Return ReturnValue

END FUNCTION

With this basis, we can implement the ILIST interface indexof index method:

Public overridable function indexof (Byval value as object) _

As integer imports ilist.indexof

'Return node index by node data value

Validate (Value) 'First verification value

Return FindByValue (Value) 'Call Protected Finding Method

END FUNCTION

In this way, when we use the list to find an index of a value, we will throw an exception if Value is empty; if not found, it will return -1; find the index position where the data element is returned. Is it similar to ArrayList?

In addition, we also need to implement the Contains function, used to determine if there is a value in the list:

Public Overridable Function Contains (Byval Value As Object)

As Boolean Implements IList.Contains

'Find value values ​​in the list

Validate (Value)

If FindByValue (Value) = -1 ThenreTurn False 'can't find

Else

Return True 'found

END IF

END FUNCTION

3.5 Add Node

In the first quarter above, it is mentioned that there are two cases, add the end of the linked list and insert the index value:

Public Overridable Function Add (Byval Value As Object)

As integer imports ilist.add

Add a node to the linked list

Validate (Value) 'First verification value

TAIL.NEXTNODE = New ListNode (Value) 'points the next node of the existing tailpoint to the new node

Tail = tail.nextnode 'Sets the newly added node to the tailpoint

Version = 1 'Change version number

NodeCount = 1 'Add a chain table count

Return NodeCount - 1 'Returns the tail node index

END FUNCTION

Public overridable subinsert (byval index as integer, _

IMPLEMENTS ILIST.Insert

'Add a node to the specified index

Validate (Index, Value) 'Verify Index and Data Value

DIM TEMPNODE As ListNode = FindbyIndex (Index) 'found an existing node at the index

'Define new nodes, the next node of the new node references the index of index to index

DIM NewNode As ListNode = New ListNode (Value, TempNode)

'Pointing the next node of Index-1 points to the new node

FindbyIndex (Index - 1) .nextNode = NewNode

Version = 1 'Change version number

NodeCount = 1 'Add a chain table count

End Sub

3.6 Delete Node

Protected Overridable Sub Removenode (Byval Node As ListNode, Byval Index As INTEGER)

'Delete node used inside the class

'Delete node method is to reference the next node of its previous junction to the next node

DIM TEMPNODE AS LISTNODE = FindByindex (INDEX - 1) 'found the previous node to delete nodes

TempNode.nextNode = node.nextNode

IF node is tail kil

Tail = TempNode

END IF

Version = 1 'Change version number

NodeCount - = 1 'Reduced Lin Try

End Sub

Public Overridable Sub Remove (Byval Value As Object)

Implements IList.remove

Delete method of "class implementation interface

Validate (Value)

Removeat (FindByValue (Value)

End Sub

Public Overridable Sub Removeat (BYVAL INDEX AS INTEGER) _

Implements ilist.removeat

The method of deleting the index in the interface

Validate (INDEX)

Dim node as listnode = findbyindex (index)

Removenode (Node, INDEX)

End Sub

Public Overridable Sub Clear () IMPLEments IList.Clear

'Empty linked list

Head.nextNode = Nothing

Tail = HEAD

NodeCount = 0

Version = 0

End Sub

From the above three Remove methods, it is actually deleted through the RemoveNode method inside the class, but only provides two interfaces to the user: one is deleted according to the index value, one is by comparing the data element value. delete. Here, you will explain it here, the single-link sheet is not to look forward to the previous junction, so the efficiency of deletion is lower than the bidirectional linked list, which will later mention when the double-linked list will be mentioned.

3.7 replication

Press the data element in the list to start with an index, copy the element to the Array. This method is useful in actual operations:

Public Overridable Sub Copyto (Byval Array As System.Array, _

IMPLEMENTS ILIST.COPYTO

'Start copying the elements to the list from the index of the list

IF array is nothing then

Throw new argumentnullexception ()

Elseif Index <0 THEN

Throw new ArgumentOutofrangeException ("index matte")

Elseif Index> = array.length _

Or (Array.Length - INDEX - 1)> NodeCount_

Or array.rank <> 1 THEN

Throw new argumentexception ()

END IF

DIM CURRENT As ListNode = head.nextNode

DIM position as integer = index

'Cycle replication

While Not Current Is Nothing

Array (position) = current.data

Current = current.nextNode

Position = 1

End while

End Sub

----

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

转载请注明原文地址:https://www.9cbs.com/read-25539.html

New Post(0)