I need a linked list expert !!!!!!!

Started by
32 comments, last by Jouei 16 years, 10 months ago
Ok well for the most part this is what i was trying to create. The following code here! http://www.hexdollhosting.com/MapLoader.txt Was designed to load in a map for 3d purposes it seems to work fine execept for one large problem. I created a function for Displaying Each sector witch is a linked list and within each sector is another linked list witch holds polygon location information. But when i call the function from the main to display the information it displays the same information twice. i have only loaded in two sectors. it displays the same information for each sector. i hope thats clear... :S each sector has an ID tag to help find it in the list. i feed that Number into the Function and it displays data for the sector but when i feed in two different sector id tags i get the same sectors information. Now heres the real twist and what iv been scratching my head over for the last oh id say 7 hours ... a long 7 hours. is this. when i call the display function with in the AddSector function it displays fine. but not when i call it from the Main........ Well hopefuly im not just rambleing on here but any input would be nice a fresh set of eyes on it might figure it out.
Advertisement
Why are you reinventing the wheel? If you need a linked list, use std::list. Although from looking at your code, you don't really need a linked list and could use std::vector instead.

Additionally:
0) In C++, you want to include <cstdio> and not <stdio.h>
1) In C++, you want to use std::string instead of char arrays for string data.
2) In C++, you might want to use iostreams for file IO.

You should use the debugger to step through your display routines and see why you end up with the same information displayed twice. Looking at your crude linked list implementation, I'm not entirely sure its operating the way you think it is...
Umm because i like to and because im behind the times ...

Please do if you have time elberate on this linked list in the std.

id much apreceate it
There are a few things I noted about your programming:

bool DisplaySector(...){...   POLYLIST * temp;				temp = Map.SectorHeader->PolyHeader;				do				{				if (temp == NULL)				else				{ ...


Better would be:
bool DisplaySector(...){...   POLYLIST * temp;   temp = Map.SectorHeader->PolyHeader;   while ( temp != NULL ) { ...


Another thing I noticed is the initialization of your variables. You can asign initial values at the same line as you declare them instead of the next line (this is not required though).

After debugging a bit I found your problem: it is in the code above :P Please have another at the lines in the first source block. It should be easily found now.

Crafter 2D: the open source 2D game framework

?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]

Well even after doing the recomened change in the Display Function .. i am still having the same problem althought im not saying that change was the fix you were refering to.

iv been at this awhile and i am abit drain if you could be more specific as the the proble so i can fix it and go to bed lol id aprrecate it!
Step through your code with a debugger (check out this article). The problem should become rather obvious.

And then: std::vector. Use it. Rolling your own is a waste of time when in the context of writing code that has some kind of production use. You say you "like" it, but you've spent seven hours debugging a broken linked list when you could have used a perfectly good, standard, alternate container (the std::vector) which would have let you spend that seven hours doing something more productive. Are you sure you like it? :)
lol i appologize i mean the likeing it part quite jokingly ;-)

i wasnt aware of the classes that the std had avalible the book i had read awhile back for C++ was well old then lol.

if you think 7 hours is bad i spet three days oh just trying to find information on subclass a win32 control and got nowhere .. still havent . but thanks i may just rewrite it in the end it just feels like such a waste . -.^and frankly i still havent the foggiest as to where the mistake is LOL...........

and i have been steping through it as we speak ill just save that for another day so i can kick my self in the meteaforical .. Butt and feel abit dissapointed in myself lol

Well thanks for all your help sry i was not more usefull in figureing out myself oh well live and learn!
SECTOR::~SECTOR()						//Clean up{	POLYLIST * PolyPtr = NULL;	if(PolyHeader == NULL)		return;	while(PolyPtr != NULL)	{		PolyPtr = PolyHeader->Next;		delete PolyPtr;		PolyHeader = PolyPtr;	}	return;}


This is wrong as well, this translates to:
			// Point to the second element in the list		PolyPtr = PolyHeader->Next;				// Free the memory that the second element is occupying		delete PolyPtr;				// Make the head pointer point to the location of freeed memory that		// was the second element and no longer exists. 		PolyHeader = PolyPtr;

Steven Yau
[Blog] [Portfolio]

Quote:
but thanks i may just rewrite it

Refactor, don't rewrite.
That is a very valid point however by rewrite it i mean to say. Redo

Im just gona break it down it a massivly simplistic form and then see if i cant get it to work. so in a way yes i am rewriting it.

but for the most part i dont even have a clue where the problem spawns from so i cant make any solid conclusion as to what i did wrong so going through it step by step again ... -.^ and oh me an linked list have a horible past.

peraphs mabye i can find this oh so called simple thing i did wrong.

although i wouldnt count on it just yet :p cause ill probaly gloss over it again.

This topic is closed to new replies.

Advertisement