I figured out why my problem before wasn''t working..and implemented a way to add a new thing it to "next" in my list. However, I need to go the END of the list so that I don''t just keep writing to the pointer->next. This dosen''t work, and I figured it would, by my skillz aren''t all that great:
for (obj = obj; obj->next != NULL; obj = obj->next)
{
}
shouldn''t that make it so that obj points to the final position, so that obj->next points to an open position? Geez, I donno. It''s starting to tork me off.....if you can help, thanks a bunch.
Dare wa neko o koroshiteimasuka? (Ha! Learn Nihongo!)
More Linked Lists!
The way I do it is in my LinkedList class, I have 3 pointers. First, Current, and Last. When I want to add a node, I either add it on the front, on the back, or wherever I am right now (this is double linked btw). The way I have written it lets all the nice little accessor functions handle the messy nodes... the user never even sees one.
--------------------
You are not a real programmer until you end all your sentences with semicolons;
--------------------
You are not a real programmer until you end all your sentences with semicolons;
Visit the ROAD Programming Website for more programming help.
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
It sounds like you don''t really need a double linked list. If you just want to keep track of the end so you can add stuff to it, everytime you create a new node set a ''end'' varialbe to that node. So at the beginning the first node also equals the end none, when you add a new node, the end reference points to the new new one, etc. I know I explained this badly, but i''m sure you can figure it out. Cheers!
From what I can see, that loop code should work fine. However it will fail if you don''t initialize the next pointers to NULL before building the list. Also, if you add/remove things you should make sure the pointers in the new object have been initialized first.
For example, this is wrong:
If the list loop fails because of an invalid pointer, then you should get an access violation.
- null_pointer
Sabre Multimedia
For example, this is wrong:
struct obj_list {
obj* first;
};
struct obj {
obj* next;
};
void main()
{
// create an empty list
obj_list my_list;
// add the first 2 members
my_list.first = new obj;
my_list.first->next = new obj;
for( obj* current = my_list.first; current->next != NULL; current = current->next )
{
// parse the list
// ERROR: this will execute 3 times and fail on the
// third time, because we haven''t set the pointers to
// NULL in the beginning. The pointers point to random
// data unless we''ve initialized them.
}
// destroy the first 2 nodes
// (this will never get here)
delete my_list.first->next;
delete my_list.next;
}
If the list loop fails because of an invalid pointer, then you should get an access violation.
- null_pointer
Sabre Multimedia
like null_pointer said, init the next pointers to NULL in the constructor of the object (c++) or set them to null when adding. For your loop, I would use a pointer, not an actual object.
obj * op;
for(op = &first op->next != NULL; op = op->next);
obj * nobject;
nobject = new obj;
op->next = nobject;
//if you're using c++ properly the next line is unnecissary
nobject->next = NULL;
Edited by - iwasbiggs on June 5, 2000 7:29:47 PM
obj * op;
for(op = &first op->next != NULL; op = op->next);
obj * nobject;
nobject = new obj;
op->next = nobject;
//if you're using c++ properly the next line is unnecissary
nobject->next = NULL;
Edited by - iwasbiggs on June 5, 2000 7:29:47 PM
___________________________Freeware development:ruinedsoft.com
ok, here it is, plain and simple.
TADA!
=======================================
A man with no head is still a man.
A head with no man is plain freaky.
Edited by - Zipster on June 5, 2000 9:17:44 PM
APointer *end;APointer *newp = new APointer;end->next = list_start; // Assuming list_start points to the beggining of the listwhile(end->next) end->next = end->next->next; obj->next = newp;newp->next = NULL;
TADA!
=======================================
A man with no head is still a man.
A head with no man is plain freaky.
Edited by - Zipster on June 5, 2000 9:17:44 PM
Alrighty. Arigato Gozaimashita. Thanks alot. Whatever. These linked lists sure are frustrating, though. You people agree?
Dare wa neko o koroshiteimasuka? (Ha! Learn Nihongo!)
Dare wa neko o koroshiteimasuka? (Ha! Learn Nihongo!)
"Now watch as I run away in a womanly fashion." - Batman
Sponge99: Yes, especially when writing the add/remove functions for a list -- it can be very irritating when you keep getting access violations and the if() statements don''t work like you think, or a million other reasons. Linked lists are really neat when they work properly. There are also a couple of different kinds. It''s kind of neat to write a class for them though.
- null_pointer
Sabre Multimedia
- null_pointer
Sabre Multimedia
I really don't see the big deal with linked lists. They're very trivial. Sorry if I'm insulting anyone here. I can't emphasise enough how important it is to understand these structures, as nearly all the more advanced ones such as trees, hash tables, graphs, etc will use the concepts in similar ways.
Here's a quick rundown of an implementation of an intrusive doubly linked list.
Have a next pointer and a prev pointer in your structure. And store a first and a last pointer as globals. Initialise all pointers to NULL when you make them.
Now, these little macros should be self-explanatory, but since people are having trouble, I will comment each line. This is in macro form for simplicity and compatibility: anyone with a little experience could make this into a template function, or embed it into a class. (Note: the do/loop block is there to ensure the macro doesn't get screwed up in certain situations: if you reimplement this as a template function or something, then you won't need all that.) Treat it as pseudo code since I'm not near a compiler right now.
Now, no more linked list questions! *screams*
Edited by - Kylotan on June 7, 2000 6:52:18 AM
Here's a quick rundown of an implementation of an intrusive doubly linked list.
Have a next pointer and a prev pointer in your structure. And store a first and a last pointer as globals. Initialise all pointers to NULL when you make them.
Now, these little macros should be self-explanatory, but since people are having trouble, I will comment each line. This is in macro form for simplicity and compatibility: anyone with a little experience could make this into a template function, or embed it into a class. (Note: the do/loop block is there to ensure the macro doesn't get screwed up in certain situations: if you reimplement this as a template function or something, then you won't need all that.) Treat it as pseudo code since I'm not near a compiler right now.
/* This adds 'link' to the list defined by first and last */#define ADDLINK(link, first, last)do{ /* If there is no 'first' pointer, the list is * obviously empty, so set that pointer to point * to this node */ if ( !(first) ) (first) = (link); else /* There was a 'first' pointer, so the list exists. * We need to therefore add the new node after the * current last node. */ (last)->next = (link); /* Our new node is at the end of the list, so make * sure its next pointer is NULL. */ (link)->next = NULL; /* Our new node is at the end of the list, so its * previous pointer should be to whatever was at * the end of the list. If it is the only node in * the list, then 'last' will be NULL, and it's * ok and correct to copy that too. */ (link)->prev = (last); /* Finally, make sure the 'last' pointer points to * the new end of the list. */ (last) = (link);} while(0)/* This inserts 'link' into the list defined by 'next' and 'prev' *-before- the node pointed to by 'insert'. Don't pass in a NULL * insert - use ADDLINK instead. */#define INSERT(link, insert, first)do{ /* Whatever is behind insert will end up behind link. * If insert is the first in the list, its prev pointer * will be NULL, but that's ok. */ (link)->prev = (insert)->prev; /* If insert's prev pointer was NULL, it must have been * at the start of the list. That means our new node will * need to be set as the new start of the list. */ if ( !(insert)->prev ) (first) = (link); else /* There was a node behind 'insert'. 'link' will go after * this node, so set its next pointer accordingly. */ (insert)->prev->next = (link); /* 'link' goes before 'insert'. */ (insert)->prev = (link); (link)->next = (insert);} while(0)/* This removes the node pointed to by 'link' from the list. */#define UNLINK(link, first, last)do{ /* If link was at the start of the list, we have to * change the start to point to whatever came next. * If the list only had 1 node, this will be NULL, but * that's fine. */ if ( !(link)->prev ) (first) = (link)->next; else /* Otherwise, the node that preceded 'link' needs to * be set to point to the node -after- 'link'. Again, * this may happily be NULL. */ (link)->prev->next = (link)->next; /* If there is no node after 'link', then it must * have been the last one in the list. So change the * last pointer to the preceding link, which can be NULL. */ if ( !(link)->next ) (last) = (link)->prev; else /* Otherwise, there was another node after link. Tell it * to point its prev pointer at whatever comes before * 'link' */ (link)->next->prev = (link)->prev;} while(0)
Now, no more linked list questions! *screams*
Edited by - Kylotan on June 7, 2000 6:52:18 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement