Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I need help with my linked list program.
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RGB255




PostPosted: Thu Nov 16, 2006 8:15 pm   Post subject: I need help with my linked list program.

We started linked lists today at school and I sort of get how the adding code works to add to the list but I don't know how to make code that will "remove" items from the list. Any help on this would be awsome. Thanks.

code:
Private Type NameList
    Data As String
    Link As Integer
End Type
 
Dim linkvar(100) As NameList
Dim First_loca, Spot_loca, Vacant_loca, Vacant_loca_temp As Integer
 
Private Sub Command3_Click()
Vacant_loca_temp = Vacant_loca
Vacant_loca = 2
Call Command1_Click
Vacant_loca = Vacant_loca_temp
End Sub
 
Private Sub Command4_Click()
Form1.Caption = linkvar(1).Data
End Sub
 
Private Sub Form_Load()
'Initialize Linked List
 
For x = 1 To 99
    linkvar(x).Link = x + 1
Next x
 
'Set last item to null
linkvar(100).Link = 0
 
'Set First_loca and Vacant_loca
First_loca = 0   'list empty
Vacant_loca = 1  'initial open position
End Sub
 
Private Sub Command1_Click()        'Add to list
'If Vacant_loca_temp > 0 Then
'    Vacant_loca = Vacant_loca_temp
'    Vacant_loca_temp = 0
'End If



'Update Vacant_loca Postion
Spot_loca = Vacant_loca
Vacant_loca = linkvar(Spot_loca).Link
 
'Place input into array
linkvar(Spot_loca).Data = Text1
Text1 = ""
Text1.SetFocus
 
'Establish Position in the List
Form1.Caption = Spot_loca
'this also sets the links I think 'Check to see if item is First_loca
If First_loca = 0 Or linkvar(Spot_loca).Data < linkvar(First_loca).Data Then
    linkvar(Spot_loca).Link = First_loca    'Points to what previous First_loca pointed to
    First_loca = Spot_loca                'Makes new item First_loca in list
Else        'New Item is not the First_loca in the list
    Pointer1 = First_loca
    pointer2 = linkvar(First_loca).Link
    'The two pointers around the position where the new
    'item is inserted need to be corrected
   
    'Find the Correct Position in the list
    Do Until pointer2 = 0 Or linkvar(Spot_loca).Data < linkvar(pointer2).Data
        'loops until the end of the list, or the correct position is found
       
        Pointer1 = pointer2
        pointer2 = linkvar(pointer2).Link
        'moves through the list, when the loop stops,
        'these two pointers must be adjusted
    Loop
 
   
    linkvar(Pointer1).Link = Spot_loca 'fixing links
    linkvar(Spot_loca).Link = pointer2
End If
Call Command2_Click

End Sub
 
Private Sub Command2_Click()
'Clear Listbox
List1.Clear
 
'Output the linked list in order
'Current is a local pointer used to move through the list
Dim Current As Integer
 
'Current begins at First_loca
Current = First_loca
 
Do Until Current = 0
    List1.AddItem linkvar(Current).Data   'output data
    Current = linkvar(Current).Link       'move to next item
Loop
End Sub



Form1.frm
 Description:
my very unorganized form.

Download
 Filename:  Form1.frm
 Filesize:  4.08 KB
 Downloaded:  100 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Nov 17, 2006 12:11 am   Post subject: (No subject)

With all due respect, if you're using an array, then you don't have a linked list.
RGB255




PostPosted: Fri Nov 17, 2006 8:11 am   Post subject: (No subject)

So how would I go about making a proper linked list?
rdrake




PostPosted: Fri Nov 17, 2006 9:17 am   Post subject: (No subject)

If you look at the [WIP] C Whirlwind then you will see an example of a linked list in C. Not sure if that helps, though.
wtd




PostPosted: Fri Nov 17, 2006 2:19 pm   Post subject: (No subject)

A linked list is formed from nodes. Each node contains a value of some type, and a link to another node.

In a language like C or C++, this will often take the form of a pointer to another node. If the pointer is NULL, then the current node is assumed to be the end of the list.

Other languages may have different ways to represent this boolean logic.
RGB255




PostPosted: Wed Nov 22, 2006 8:55 pm   Post subject: (No subject)

Okay the code may not be a true linked list but can anyone help me with the code that will allow me to replace an item in the list and then allow me to keep adding to the end of the list?
wtd




PostPosted: Thu Nov 23, 2006 12:48 am   Post subject: (No subject)

I hate to harp on this incessantly, but style matters Indenting the contents of your subroutines would make it much easier to trace what is going on. Smile

Of course, more of an MVC approach would help too, with the code to manage a "linked list" kept separate from the code for the UI.

The problem with adding to the end of the list is that your solution is based on an array. The array is of fixed size, and thus cannot grow in the same way a true linked list can.
RGB255




PostPosted: Thu Nov 23, 2006 1:24 pm   Post subject: (No subject)

Well this is just supposed to be an example to show us the concept so I didn't bother to make it in a dynamic array. The problem is the teacher left us to figure out the code that will allow us to replace an item in the list. The problem I get is that when I try to replace an item everything gets out of order and doesn't work.
Sponsor
Sponsor
Sponsor
sponsor
Silent Avenger




PostPosted: Thu Nov 30, 2006 6:34 pm   Post subject: (No subject)

Well linked lists are complicated things so I won't just give you the answer to your problem but I'll give you hints and in this way you'll learn how the linked list works. First of all if if you look how your initial links are set up you'll see a pattern which is that each empty spot points to the next empty spot. So you learn from this that you'll also need to change the link of the spot you're trying to remove. Also if you try running your program a few times you'll notice that if you remove a spot it doesn't really change the order of the list so you'll also have to change the link of the item that pointed to your removed item. With this you should be able to figure out the code that you need to remove an Item.
RGB255




PostPosted: Thu Nov 30, 2006 6:55 pm   Post subject: (No subject)

Okay thanks for the tips. I'll try them out.
Silent Avenger




PostPosted: Thu Nov 30, 2006 7:15 pm   Post subject: (No subject)

One other thing to tell you the code that you're going to come up with isn't going to remove the first item so you'll have to do some thinking.
RGB255




PostPosted: Thu Nov 30, 2006 7:30 pm   Post subject: (No subject)

Thanks it worked!
Silent Avenger




PostPosted: Thu Nov 30, 2006 7:39 pm   Post subject: (No subject)

You're welcome. See now that you know how it works it makes it a lot easier to figure out the code to remove an item.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: