Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Creating a linked list using Type keyword

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2010, 08:02 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Creating a linked list using Type keyword

The Type keyword is used to create your own types or structures. You can use such types to create simple structures that group some values.
However you can also use it to create more complex collections.
In this tutorial we will create a linked list.
A simpler usage of Type is demonstrated in the currency converter tutorial.

A linked list is a collection of elements. Each element has a value field and a 'next element' object. The 'next element' is actually a reference to the next element in the list.

The type is declared like this:
Code:
Sub Process_Globals
    
Type Element (NextElement As Element, Val As Int)
    
Dim Head As Element 'declare a variable of that type.
    Dim Last As Element
End Sub
The interesting thing about this declaration is that we are using the current type as a field. The ability to declare such recursive types is very powerful.
Before we can access any of the type fields, it should be initialized by calling its Initialize method. Note that if the fields were initialized automatically then it would not have been possible to create such recursive types.
Note that if your type only includes numeric fields and strings then there is no need to call Initialize (though nothing bad will happen if you do call it).

Once we declared a type we can use it like any other "regular" types. It can be passed to subs, you can create an array of it and so on.

Lets build our list:
Code:
Sub InitializeList (Value As Int)
    Head.Initialize
    Head.Val = Value
    Last = Head 
'The last item is currently the head.
End Sub
The InitializeList sub initializes the head element, sets its value and sets the Last variable to reference the head element (as this is the only item in the list).

Adding an item:
Code:
Sub AddElement(Value As Int)
    
'create a new element
    Dim e As Element
    e.Initialize
    e.Val = Value
    Last.NextElement = e 
'set the NextElement of the current last element to the new one.
    Last = e 'set the last variable to point to the new element.
End Sub
We are creating a new element. The current last element NextElement field is set to the new element and eventually the last variable is updated to the new element.

Going over all the items in the list is done by starting with the head element and then going forward by calling NextElement. When we reach an uninitialized element we stop.
Code:
Sub ListToString As String
    
Dim e As Element
    
Dim sb As StringBuilder
    sb.Initialize
    e = Head
    
Do While e.IsInitialized = True
        sb.Append(e.Val).Append(
CRLF)
        e = e.NextElement
    
Loop
    
Return sb.ToString
End Sub
The complete code:
Code:
Sub Process_Globals
    
Type Element (NextElement As Element, Val As Int)
    
Dim Head As Element
    
Dim Last As Element
End Sub

Sub Globals

End Sub
Sub InitializeList (Value As Int)
    Head.Initialize
    Head.Val = Value
    Last = Head 
'The last item is currently the head.
End Sub
Sub AddElement(Value As Int)
    
'create a new element
    Dim e As Element
    e.Initialize
    e.Val = Value
    Last.NextElement = e 
'set the NextElement of the current last element to the new one.
    Last = e 'set the last variable to point to the new element.
End Sub

Sub ListToString As String
    
Dim e As Element
    
Dim sb As StringBuilder
    sb.Initialize
    e = Head
    
Do While e.IsInitialized = True
        sb.Append(e.Val).Append(
CRLF)
        e = e.NextElement
    
Loop
    
Return sb.ToString
End Sub

 
Sub Activity_Create(FirstTime As Boolean)
    InitializeList(
10)
    AddElement(
9)
    AddElement(
8)
    AddElement(
7)
    AddElement(
6)
    AddElement(
5)
    
Msgbox(ListToString, "List values")
End Sub
Attached Files
File Type: zip LinkedList.zip (4.9 KB, 224 views)
Reply With Quote
  #2 (permalink)  
Old 08-02-2011, 04:53 PM
Widget's Avatar
Senior Member
 
Join Date: Jul 2011
Posts: 105
Default

I went through this tutorial and a few questions popped up.

1) How do I clear the list if I want to add new elements to the list?
2) If I want to delete an element from the list I assume I manage the NextElement myself, but how do I free the element? Use element.initialize?
3) Finally is there any advantage in using this element structure instead of LinkedList? Do most people use LinkedList?

TIA
Widget
Reply With Quote
  #3 (permalink)  
Old 08-02-2011, 05:34 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The purpose of this code is demonstrate the power and usage of Type structures. I recommend you to use the regular List in your project.
Reply With Quote
  #4 (permalink)  
Old 11-10-2011, 06:47 PM
Newbie
 
Join Date: Jun 2011
Posts: 5
Default

This is just what I need.

Thanks Erel!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
VAL keyword SarahWard Basic4ppc Wishlist 24 09-16-2010 11:47 AM
OR keyword Monsterman22 Questions (Windows Mobile) 4 07-27-2010 07:44 PM
find keyword lijingtong Questions (Windows Mobile) 2 06-20-2008 09:05 AM
Auto Keyword im4retro Questions (Windows Mobile) 2 06-06-2008 03:45 PM
'live type' filtering of list box. Hennell Questions (Windows Mobile) 3 04-24-2008 08:49 AM


All times are GMT. The time now is 10:22 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0