Advertisement

Array class

Started by August 29, 2001 01:01 PM
5 comments, last by BradDaBug 23 years, 5 months ago
I''m trying to create an array class (i think that''s what its called)... basically a class that encapsulates a linked list, and the rest of the app can use it somewhat like an array. anyway, I''m having some trouble with it, and i''m hoping I can get some help. Do you guys know of any online tutorials or help or could you provide some yourself? Thanks!
I like the DARK layout!
arrays have constant time random access. linked lists do not have random access, but they can fake it at the const of linear time accessing. Arrays are to lists as apples are to oranges. They are fundamentally different. You might want to re-think your problem or re-state what you''re trying to do.
Advertisement
ok, ok, so its not really like an array.

Lets say you have a class called List. You''d add stuff to it like List.AddData(data) and then it''d go and add it to the linked list. If you wanted to delete something from it, it''d be like List.DeleteData(data).

I guess it''d be most useful for storing pointers.

What I''m mostly gonna use if for is whatever this thing is that this tutorial uses... http://www.gamedev.net/reference/programming/features/gui/

It stores the windows in some kind of linked list thats been encapsulated in a class. THATS what I''m trying to do.
I like the DARK layout!

Why reinvent the wheel? I''m assuming you are using C++? Look into using STL container classes.


To access node x in a linked list, you have to start at node 0 and traverse all the nodes before x to get to it. So let''s say you want to go through your "array class"''s sata in a linear fashion. You could just use a while loop checking to see if current_pointer != NULL and at the bottom of each loop set current_pointer = current_pointer->next. Or, you could use a function which just takes X and a pointer to the beginning of the list as an argument and returns the value of node X. That''s essentially what your class will do. The problem with that is that you will end up having to traverse the whole list before x each iteration of the loop. Here''s an example:

  NORMAL WAY:Traversal: Node 0                0, 1                1, 2                2, 3YOUR WAY:Traversal: Node 0           Node 0, 1           Node 0, 1, 2           Node 0, 1, 2, 3           Node 0, 1, 2, 3, 4           Node 0, 1, 2, 3, 4, 5  



Get what I''m saying? What you want to do is inefficient - very inefficient.
STL? I''ve heard of those! but how do I get to them and use them?
I like the DARK layout!
Advertisement
stl libraries come with standard compiler, here is a good reference http://www.sgi.com/tech/stl/

This topic is closed to new replies.

Advertisement