Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help in Linked list in VB

Status
Not open for further replies.

neoaspilet11

Full Member level 5
Joined
Sep 29, 2005
Messages
277
Helped
29
Reputation
56
Reaction score
8
Trophy points
1,298
Location
Cebu, Philippines
Activity points
4,048
Hello,

How to remove and add an item in a linked list anywhere in the listl?

I just improve the deitel n deitel code for linked list and it works fine in manipulating at both ends (front and back ends) of teh linked list but no code is given if manipulating (inserting and deleting inside teh list).


Please help

Here's my improved deitel's and deitels code for VB linked list

'Class cListNode
' February 10, 2008
Option Explicit

Private mNodeData As Variant
Private mNextNode As cListNode


Public Property Get Data() As Variant
Set Data = mNodeData
End Property

Public Property Let Data(Data As Variant)
Set mNodeData = Data
End Property


Public Property Get NextNode() As cListNode
Set NextNode = mNextNode
End Property


Public Property Let NextNode(Data As Variant)
Set mNextNode = Data
End Property




' Class Clist
' February 12, 2008
Option Explicit

Private mFirstNode As cListNode
Private mLastNode As cListNode

Public Function IsEmpty() As Boolean
IsEmpty = IIf(mFirstNode Is Nothing, True, False)
End Function


Public Sub InsertAtFront(InsertItem As Variant)
Dim TempNode As cListNode

If IsEmpty() Then
Set mFirstNode = New cListNode
Set mLastNode = mFirstNode
Else
Set TempNode = mFirstNode
Set mFirstNode = New cListNode
mFirstNode.NextNode = TempNode
End If

mFirstNode.Data = InsertItem

End Sub


Public Sub InsertAtBack(InsertItem As Variant)
Dim TempNode As cListNode

If IsEmpty() Then
Set mLastNode = New cListNode
Set mFirstNode = mLastNode
Else
Set TempNode = mLastNode
Set mLastNode = New cListNode
TempNode.NextNode = mLastNode
End If

mLastNode.Data = InsertItem

End Sub


Public Function RemoveFromFront()
Dim RemoveItem As Variant
If IsEmpty() Then
RemoveFromFront = Null
Exit Function
End If

RemoveItem = mFirstNode.Data

If mFirstNode Is mLastNode Then
Set mFirstNode = Nothing
Set mLastNode = Nothing
Else
Set mFirstNode = mFirstNode.NextNode
End If

RemoveFromFront = RemoveItem

End Function


Public Function RemoveFromBack()
Dim RemoveItem As Variant
Dim Current As cListNode

If IsEmpty() Then
RemoveFromBack = Null
Exit Function
End If

RemoveItem = mLastNode.Data

If mFirstNode Is mLastNode Then
Set mFirstNode = Nothing
Set mLastNode = Nothing
Else
Set Current = mFirstNode

While Not Current.NextNode Is mLastNode
Set Current = Current.NextNode
Wend

Set mLastNode = Current
Set Current.NextNode = Nothing
End If

RemoveFromBack = RemoveItem

End Function



'This is my trouble and also inserting an item inside the list
Public Function RemoveFromList(ByVal ItemIndex As Integer)
Dim Elements As New cListIterator
Dim count As Integer
Dim Item As Variant
Dim RemoveItem As Variant
Dim CurrentNode As cListNode
' Process in removing an item from the list
'
' 1.) Iterate through the list, and count items until equal to ItemIndex


If IsEmpty() Then
Set RemoveFromList = Null
Exit Function
End If

Set Elements.StartNode = mFirstNode

count = 0
While Elements.HasMoreItems
Set CurrentNode = Elements.CurrentNode
Call Elements.ToNextNode
count = count + 1

If count = ItemIndex Then
Set RemoveItem = CurrentNode.Data
GoTo SHIFT_LIST
End If
Wend

If count < ItemIndex Then
Set RemoveFromList = Null
Exit Function
End If


SHIFT_LIST:
While Elements.HasMoreItems
CurrentNode.NextNode = Elements.CurrentNode
Call Elements.ToNextNode
Wend

Set RemoveFromList = RemoveItem
End Function


Public Property Get Iterator() As Variant
Dim Iter As cListIterator

Set Iter = New cListIterator
Iter.StartNode = mFirstNode
Set Iterator = Iter
End Property


Public Property Get ItemsCount() As Integer
Dim count As Integer
Dim Elements As New cListIterator

Elements.StartNode = mFirstNode

count = 0
While Elements.HasMoreItems
Call Elements.ToNextNode
count = count + 1
Wend

ItemsCount = count

End Property


'Class cListIterator
' February 12, 2008
Option Explicit

Private mBookMark As cListNode '
Private mFirstNode As cListNode ' Reference node


Public Property Let StartNode(Data As Variant)
Set mFirstNode = Data
Set mBookMark = mFirstNode
End Property

Public Sub ToNextNode()

If mBookMark Is Nothing Then
Exit Sub ' end of the list
Else
Set mBookMark = mBookMark.NextNode
End If
End Sub

Public Property Get CurrentNode() As cListNode
Set CurrentNode = mBookMark
End Property

Public Function HasMoreItems() As Boolean
HasMoreItems = IIf(Not mBookMark Is Nothing, True, False)
End Function

Public Sub ResetBookMark()
mBookMark = mFirstNode
End Sub
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top