hi, I am working on some legacy code that defines a linked list (not using STL container)
I want to convert this code so as to use STL list. As you can see in the following example, the linked list is assigned an initial value for all Ns. Then certain elements in the list are assigned some value. Then the "empty elements" in the list are "cleaned up".
I am looking for a better way to do this using STL. especially, can this deleting empty elements code be avoided? I checked STL documentation.. it defines a remove method but thats not exactly what I need here. Is there a way to dynamically allocate linked list size? I would appreciate your suggestions!
Update I have modified the code. this resembles the main code I have. But to avoid any confusion, I am writing a pseudo code below to explain how it works.
Steps
- Allocate a size
elementIdsto the linked list (struct my_list) - There is another linked list
meshElemand I am interested in some values frommeshElem->elemstruct.- For Example : I need
elemId = meshElem->elem->id;ThiselemIdis in range0 to elementIds. - The
elemIdwill be used as an index to look for a particular element instruct my_list lste.g.lst[elemId]
- For Example : I need
- In the
doSomething ()function, loop through0 to elementIds. In this loop, if certain conditions are satisfied, thelst->numberis assigned an integer value =someArray[i]where i is in range0 to N(done inappendElement) - the elements without
nextentry instruct my_list lst,are cleaned up ( Question: can this be avoided ? ) - lst->number value is used further in the code for some other processing.
Now the modified code:
struct my_list
{
int number;
struct my_list *prev;
struct my_list *next;
}
void doSomething(void){
const int N = 50000;
const int elementIds = 10000;
int i, elemId, node_i;
struct my_list *lst;
lst = new struct my_list[elementIds];
int someArray[12];
meshElem = mesh->next;
for(i=0; i<=elementIds; i++) {
lst[i].num = 0;
lst[i].next = NIL;
lst[i].prev = NIL;
}
while(meshElem != NIL){
// Element id (int)
// Note that any elemId will be in range [0 - elemId ]
elemId = meshElem->elem->id;
// Do some operations to populate "lst"
// Note that 'lst' gets values ONLY for certain
// values of i
for (i = 0; i<=N; i++){
// if certain conditions are satisfied,
// it updates the linked list element
// lst[meshIdx]. foo1(), foo2() are just some conditions...
if (foo1()){
appendElement(someArray[i], &lst[meshIdx])
}
else if (foo2()){
appendElement(someArray[i], &lst[meshIdx])
}
}
meshElem = meshelem->next;
} // End of while(meshElem != NIL)
// Clean up the linked list lst
// by removing unassigned items.
struct my_list *lst_2
for(i=1; i<=N; i++) {
lst_2 = &lst[i];
while( lst != NIL ) {
if( lst->next != NIL && lst->next->number == 0 ) {
delete lst_2->next;
lst_2->next = NIL;
} // end of if loop
lst = lst_2->next;
} // end of while while( lst != NIL )
} // End of for(i=1; i<=N; i++)
// Do some more stuff that uses struct my_list lst
for(i=1;i<=elementIds;i++) {
while( lst[i] != NIL && (node_i = lst[i]->number) ) {
if( node_i == 0) {
lst[i] = lst[i]->next;
continue;
}
// Use this "node_i" index in some other arrays to
// do more stuff.
//..
//..
//..
lst[i] = lst[i]->next;
}
}
void appendElement(int n, struct my_list *lst) {
int exists = 0;
while( lst->next != NIL ) {
if( lst->number == n ) {
exists = 1;
lst=lst->next;
}
if( exists < 1 ) {
lst->number = n2;
insertElemAfter(lst, 0);
}
}