By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,398 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,398 IT Pros & Developers. It's quick & easy.

What data type would you prefer and why?

P: n/a
I wanted to practice some Linked List stuff, so I set out to create a
linked list. The plan was to create the following:

(1) A linked list class in Visual Basic
(2) A non-class based linked list using functions in C
(3) A linked list class in C++

I started with Visual Basic and I wrote an IList interface that I
wanted my list to implement. When I had started, somehow I thought
this time, I'd first use a collection as the ingredient, and so it
would not really be a linked list. It would be an extended collection
behaving like a (not linked, but just a) list, as in list of things.
And my new agenda would then be,

(1) A list (not a linked list) class in Visual Basic by extending the
Collection object.
(2) A linked list class in Visual Basic
(3) A linked list in Visual Basic that is not class-based but has a
struct (Type) and global functions in a standard module (.bas).
(4) A non-class based linked list using functions in C
(5) A linked list class in C++

I don't like to create lists if they have no meaning, so I thought it
would be a good idea if the list was a list of something and not just
"ints". So, I made a list of my friends on the Joel On Software forum.
I called the list MyFriends.

I have just finished implementing a list by extending the Visual
Basic's Collection object. My list has the following interface:

Expand|Select|Wrap|Line Numbers
  1. Public Function AddAtPos(ByRef Object As Object, _
  2. ByVal Index As Long) As Boolean
  3. End Function
  4.  
  5. Public Function PeekAtPos(ByVal Index As Long) As Object
  6. End Function
  7.  
  8. Public Function RemoveAtPos(ByVal Index As Long) As Boolean
  9. End Function
  10.  
  11. Public Property Get Count() As Long
  12. End Property
  13.  
  14. Public Function Contains(Object As Object) As Boolean
  15. End Function
  16.  
  17. Public Function IndexOf(Object As Object) As Long
  18. End Function
  19.  
  20. Public Sub Serialize()
  21. End Sub
  22.  
  23. Public Sub Deserialize()
  24. End Sub
  25.  
  26. Public Sub Clear()
  27. End Sub
  28.  
  29. Public Sub Sort(ByVal SortOrder As SortOrders)
  30. End Sub
  31.  
  32. Public Sub Reverse()
  33. End Sub
  34.  
If you're interested, you can find the source code and the executable
for my first experiment here.

http://www.vbforums.com/showthread.p...hreadid=303400

Now, I am set to implement item number (2) in my agenda - the linked
list class in Visual Basic. Once again, the semantics remain the same.
I intend to keep the list as a list of friends. So each node in the
list is a friend. While designing the "node" class or the "MyFriend"
class, I stumbled accross this problem. I am recording two pieces of
information about each friend:

(1) Name
(2) Phone Number

Both are of String type. The problem was not with these, but with the
"node pointer" item. I have three data types in mind that I can use to
point to the next node. I am confused as to which one would be a good
choice, and I want your opinion on the same.

Here's what I have thought the node/MyFriend class as:

Expand|Select|Wrap|Line Numbers
  1. Implements IList
  2.  
  3. Private mName As String
  4. Private mPhoneNumber As String
  5.  
  6. '===Which one do I choose?=======
  7. 'Private mNextFriend As MyFriend
  8. '         OR
  9. 'Private mNextFriend As Long
  10. '         OR
  11. 'Private mNextFriend() As Byte
  12. '==================================
  13.  
Let me argue each case, as I thought about them. Starting with the
last, if I take the mNextFriend field as a Byte array, it would help
me....do nothing, basically. So, ruled out.

Next, if took the mNextFriend field as a Long, I think it would be the
ideal thing to do, because VB6 Long's are indeed 32-bit values, and
then I would use the mNextFriend Long field to point to a new instance
of MyFriend type, using the ObjPtr function. I would dereference the
mNextFriend Long type by using RtlMoveMemory. That sounds like a nice
plan. However, the problem with this is that it does not strictly
confirm to a Linked List set up, because of the generic nature of this
Long pointer. This pointer could be used to point not only to MyFriend
types but to anything. Since I am going to be the developer audience
for this class, and hence this is only dogfood, it is no problem, but
in general, I don't like it as a habit and I always want my code to
represent what it ought to represent. So, this would be like
compromising on a trivial issue.

If I use mNextFriend as a MyFriend type, I solve the problem of the
generic pointer by restricting it to the MyFriend type. So, it's more
disciplined this way, but now, the field is not really a pointer. It
is an object. Of course, it is still a 32-bit reference to the actual
object, but it is still not a real pointer.

So, I am confused. If you were to be doing this, what option would you
choose and why? To me, the Long seems like ok, although it is a little
bit of a compromise on the type-checking.
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Sathyaish" <Vi****************@yahoo.com> wrote

If I use mNextFriend as a MyFriend type, I solve the problem of the
generic pointer by restricting it to the MyFriend type. So, it's more
disciplined this way, but now, the field is not really a pointer. It
is an object. Of course, it is still a 32-bit reference to the actual
object, but it is still not a real pointer.


IMHO, the field is a type safe pointer, and I would have thought

Private mNextFriend As IList

would have been the obvious choice for the task. That would
have restricted those fields to objects that implement the IList
interface, no matter what type of object they actually were....

LFS
Nov 14 '05 #2

P: n/a
Sathyaish wrote:
I wanted to practice some Linked List stuff, so I set out to create a
linked list. The plan was to create the following:


No real advice here except.... check out these samples.... they may give
you some ideas (plus they're great samples)

DataBagLib and LinkedListLib
http://killervb.com/Libraries.aspx

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Nov 14 '05 #3

P: n/a
Sathyaish wrote:

I wanted to practice some Linked List stuff, so I set out to create a
linked list. The plan was to create the following:

(1) A linked list class in Visual Basic
(2) A non-class based linked list using functions in C
(3) A linked list class in C++


This VB and C++ junk is foully off-topic here in c.l.c. F'ups
set. See the links in my sig below for help. Singly linked lists
are extremely simple. For an example in C see the freverse.c
example contained in:

<http://cbfalconer.home.att.net/download/ggets.zip>

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> C-library
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.