[never mind, Stoffel gave the answer.]
aig
Edited by - An Irritable Gent on 4/19/00 4:42:35 PM
PLEASE HELP
April 19, 2000 04:48 PM
Thanx ! It helps !
i did it as you said, Osmo Suvisaari, thanx alot !!!!!
But i have another question !
How can i add a elemnt to a array without deleting the array. For Example i have int a[3] how can i change this at runtime in int a[4] ?
i know its something with dynamic memory things. iam new to this.
i did it as you said, Osmo Suvisaari, thanx alot !!!!!
But i have another question !
How can i add a elemnt to a array without deleting the array. For Example i have int a[3] how can i change this at runtime in int a[4] ?
i know its something with dynamic memory things. iam new to this.
You can''t. Not the way you want to at least. When you declare an array, the memory is set aside at compile time, unless you use dynamic memory allocation like new or malloc. So, you either need to delete the old memory and make a new block for the array (''Cause there''s no gaurantee that the next address is free, this is neccessary, otherwise you''ll overwrite your memory bounds) or you can make an array large enough to hold all of the values you think you''ll ever need.
You could look into the STL classes though. One of those would probably work. It has classes that simulate what you want to do.
If not you, could build you own linked list, it''s not to hard once you get your mind around it and there are tons of free samples on the net.
You could look into the STL classes though. One of those would probably work. It has classes that simulate what you want to do.
If not you, could build you own linked list, it''s not to hard once you get your mind around it and there are tons of free samples on the net.
are there any tutorials out there on Linked Lists ?
i am the 2 between 0 and 1
I made a liked list. What you do is make a struct that has a pointer to the next item in the list. You can also add a pointer to the previous item.
Where the pointer to data can be anything you really want. (I used my list to keep track of images that are in memeory, they need to be all deallocated upon exit)
Now you need a function that will add a node to the list, what you do is have a loop that searches till it finds the last node (or you can have a variable to store the pointer to the last node) and attaches a new node to the list.
Now you will probably want a function to remove an item from the list, this is a similar fucntion.
Note that you will have to only keep track of a pointer to the first node externally. The benefits of this is that you can make the list as long or short as you want by adding in nodes or removing them. However this is lower than having a prediefined array as if you want to get the 5th item you have to do a for loop. In the for loop you have to have an if statement to make shure you wont be using a NULL to point to the next item. (ie you have 4 nodes and you want to access the 5th)
You could do an array of pointers to arrays of pointers to the data. That would put a limit but it would be high, and you could always keep some pointers NULL therfore not waste memory. Sound confusing? Should be, I can bearly imagne it.
OneEyeLessThanNone
- Gonna pluck whichever Eye I have left. Keeps itching
oh btw, could somone tell me if my code would mess up upon removing nodes? I wrote this off the top of my head and the linked list i used took asome debugging to fix. (forgot to change the list pointer when removing first item LOL)
struct NODE{ NODE next; void *data;};
Where the pointer to data can be anything you really want. (I used my list to keep track of images that are in memeory, they need to be all deallocated upon exit)
Now you need a function that will add a node to the list, what you do is have a loop that searches till it finds the last node (or you can have a variable to store the pointer to the last node) and attaches a new node to the list.
void addNode( NODE *list, NODE *new ){ // I use NULL to signify end of list while( list != NULL ) // loop while not the last element list = list->next; // set to next element list->next = new; // set next element to new new->next = NULL; // make new last element}
Now you will probably want a function to remove an item from the list, this is a similar fucntion.
void removeNode( NODE *list, NODE *target ){ NODE *next; while( list != NULL ) if( list->next == target ) // if we found target break; // end search else list = list->next; // else contine with search if( list == NULL ) // last node, no match return; next = target->next; // save the pos of the next node list->next = next; // link the list delete target; // destroy target}
Note that you will have to only keep track of a pointer to the first node externally. The benefits of this is that you can make the list as long or short as you want by adding in nodes or removing them. However this is lower than having a prediefined array as if you want to get the 5th item you have to do a for loop. In the for loop you have to have an if statement to make shure you wont be using a NULL to point to the next item. (ie you have 4 nodes and you want to access the 5th)
You could do an array of pointers to arrays of pointers to the data. That would put a limit but it would be high, and you could always keep some pointers NULL therfore not waste memory. Sound confusing? Should be, I can bearly imagne it.
OneEyeLessThanNone
- Gonna pluck whichever Eye I have left. Keeps itching
oh btw, could somone tell me if my code would mess up upon removing nodes? I wrote this off the top of my head and the linked list i used took asome debugging to fix. (forgot to change the list pointer when removing first item LOL)
quote: Original post by Anonymous Poster
How can i add a elemnt to a array without deleting the array. For Example i have int a[3] how can i change this at runtime in int a[4] ?
i know its something with dynamic memory things. iam new to this.
There''s a thread on this elsewhere on this very forum I believe. Have a look around for it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement