Advertisement

linked lists

Started by July 20, 2004 12:36 AM
20 comments, last by python_regious 20 years, 4 months ago
quick question. im learning linked lists and i have a decent test program to play with. whenever i output the results it goes right back to the top. take a look @ my code.

void main()
{
	node*	head = NULL;
	node*	ptr = head;

	ptr = new node;
	for(int x=0; x<10; x++)
	{
		ptr->data = x;

		if(head == NULL)
			head = ptr;
		else
		{
			ptr->next = new node;
			ptr = ptr->next;
			ptr->next = NULL;
		}
	}
	
	node*	temp = head;
	while(temp != NULL)
	{
		cout << temp->data << endl;
		temp = temp->next;
	}
}

any ideas on why its going back to the top?
node* head = NULL;

...

if(head == NULL)
{
}
else //never happens
{
//next == new.
}
[size="2"]I like the Walrus best.
Advertisement
im not too clear on what your saying but i will say this. when it does output it does:

1
2
3
4
5
6
7
8
9
0

"0" should be output first.
bacuase "ptr->data = x" comes before "if(head == NULL)",change there places and all will be ok
Also consider using STL where all this is already implemented and tested.
You should never let your fears become the boundaries of your dreams.
Quote: Original post by _DarkWIng_
Also consider using STL where all this is already implemented and tested.

Quote: Original post by adam17
quick question. im learning linked lists and i have a decent test program to play with. whenever i output the results it goes right back to the top. take a look @ my code.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Advertisement
Just to expand a bit on the solution to the problem (which shadowwz gave). The first time through the loop you assign the data to the node and make head point to that node, but you you leave ptr pointing to the first node. Therefore the second time through the loop you change the data in the first node and then add a second node. You need to add the second node first and then set the node data. With your code as it is you are actually overwriting the first value in the list and the last value is undefined (you got zero, I got 4420696 when I ran your code).

Secondly: Yes, play with linked lists as much as you like until you understand them fully. As soon as you understand what's going on and why then you should drop your own linked lists and move to the STL since it's a stable, fast, flexible, bug-free implementation that you probably won't be able to better.

Enigma
Quote: Original post by Enigma
Secondly: Yes, play with linked lists as much as you like until you understand them fully. As soon as you understand what's going on and why then you should drop your own linked lists and move to the STL since it's a stable, fast, flexible, bug-free implementation that you probably won't be able to better.


Indeed, the millionth or so ll implementation I did was a little pointless. STL is just nicer, quicker ( to use - possibly not runtime speed - if you're a damn good programmer ), and you save time for coding fun stuff. [smile]
If at first you don't succeed, redefine success.
Its good to know how to code the basics tho if you should ever need them.
Once, i posted a linked link source code for the comunity, i had a furious flame war about not using stl , of course i wasted my education and i am a very unfriendly and unhelpfull person.
YOU ARE A BUNCH OF KIDS,
now rate me noooooooooooooooooooooooooooobs

This topic is closed to new replies.

Advertisement