Advertisement

How to implement dynamic "custom" arrays..?

Started by April 20, 2002 01:48 PM
11 comments, last by Xces 22 years, 10 months ago
Ok, i want to read a filename (or directory) which contains pictures. Let''s assume i want to read a text file. I want to display these pictures in my OpenGL app. Therefore i want to load the entire text file into a dynamic array. Why dynamic array? First of all i don''t know how many items will be in the text file, and second of all i want to be able to add images while the app is running. a) But i need an array which not only contains the filename, but also contains the extension. So my guess is i must use a struct for this. b) Anybody has some examples of how to initialize such an array and how to add an item, delete an item etc etc? maybe another way of doing this? Greetz Xces (2 weeks c++ experience and growing )
A linked list is a better choice for that type of thing. You can find plenty of information on making your own or using the STL''s online.

Advertisement
Hey there, with your suggestion about linked list, i went on searching for it on Google, read some documents and came to this site:

http://www.cprogramming.com/tutorial/lesson15.html

Very interesting with good explanation of what''s what. But now my next question: As they say on this page, a (singular) linked list can only be pushed and popped;

If my linked list is 15 items big, and i want to delete item 7, so 8 becomes 7, 9 becomes 8 and so on, this cannot be done?

And also.. I read that for me to read item 15, i have to traverse throught items 1..14 is that right? Any work-arrounds, as in, standard linked list classed which support such navigation?
quote:
Original post by Xces
If my linked list is 15 items big, and i want to delete item 7, so 8 becomes 7, 9 becomes 8 and so on, this cannot be done?

It can be done, but it requires navigation through the list to get item 6 and 7 (6 since we can''t go backwards, and must link 6 to 8). A doubly linked list helps with ''I have a pointer to node x and want to delete it'' situations (since we don''t have to get x-1, because x has a parent node).
quote:
Original post by Xces
And also.. I read that for me to read item 15, i have to traverse throught items 1..14 is that right? Any work-arrounds, as in, standard linked list classed which support such navigation?

There''s no single workaround, but most of the time it isn''t a problem. The most common use of a linked list is to loop through from beginning to end, in which case you don''t run into the ''I must get x, so I have to loop through x-1 nodes'' issue. If random access is crutial, but appending and deleting items isn''t, then you may actually want to use a dynamic array. The STL''s vector class is a dynamic array wrapper than can hide all of the resizing from you.

You can always use a circular linked list, then use a back pointer that points to the last item in the list.

------

Check a Reseller''s Rating Before You Purchase!
------Shop for the Lowest Price!Then, Check a Reseller's Rating Before You Purchase!
The url i posted above actually is a circular (double) linked list.
Advertisement
Whoops!, Sorry bout that.

------

Check a Reseller''s Rating Before You Purchase!
------Shop for the Lowest Price!Then, Check a Reseller's Rating Before You Purchase!
It''s OK :D
----------------------------------------------------------------
But anywayz...
----------------------------------------------------------------
Is this a good starting point for me?

http://www.cs.smith.edu/~nhowe/teaching/csc112/Tutorials/linklist.html

Basicly i only need to add (e.g. initialize) the linked list while i read in the file, and move through it untill i reach the end and then restart from head()->tail(). When the file changes, i have to "empty" the linked list, so i can refill it again.
So searching in, and deleting @ positions is not needed by me.

Pixellate (web-zine for games programmers, find via google) did a great article on the STL which should answer all your questions on list, vectors etc. I recommend giving it a read.

Dan

[size="1"]
btw, you can add to your text file the number of files that are there, so when reading in the file you know how many char* you need for the file name ( a simple malloc(num*szieof(char*)) allocation). then when reading the filenames place an upper limit like MAX_PATH (which is the windows upper limit and defined in the windows.h header). this is useful becuase you can have a tmp array which you read the file name into then allocate the space in your filelist array.


  // PSUDEO codechar **filelist = (char**)malloc(numFiles*sizeof(char*));for(i=0; i<numFiles; i++){   char tmp[MAX_PATH]; // like 512 or 1024   getline(tmp);   filelist[i] = (char*)malloc(strlen(tmp));   strcpy(filelist[i], tmp);}// deinitfor(i=0; i<numFiles; i++){   free(filelist[i]);}free(filelist);// to get the extensionchra *ext = strrchr(filelist[i], ''.'')+1;  


hope that helps

This topic is closed to new replies.

Advertisement