Sign in to follow this  
lukar

Linked List troubles

Recommended Posts

Hi, im making myself a linked list to understand its processes but i have ran into some pointer trouble... Maybe i still don't understand quite well how to handle them.

void PH1::AddObject(Objeto* nuevo)
{
	if(myBool)
	{
		
		std::cout << "FIRST CASE" << std::endl;
		*cabeza = *nuevo;
		cabeza->siguiente = NULL;


		std::cout << "cabeza->siguiente " << cabeza->siguiente << std::endl;

		myBool = false;



		std::cout << "END FIRST CASE" << std::endl;
	}
	else
	{
		std::cout << "SECOND CASE" << std::endl;
		std::cout << "cabeza->siguiente " << cabeza->siguiente << std::endl;
		//////////////////////////////////////////////////////////////////////////////
		nuevo->siguiente = cabeza;
		cabeza = nuevo;
		//////////////////////////////////////////////////////////////////////////////
		//For some reason the third object's siguiente pointer points to itself.

		//TEST OUTPUT
		std::cout << "yo: "  << nuevo << std::endl;
		std::cout << "cabeza->siguiente " << cabeza->siguiente << std::endl;
		std::cout << "cabeza->siguiente->siguiente " << cabeza->siguiente->siguiente << std::endl;
		std::cout << "END SECOND CASE" << std::endl;
	}
}

Sorry for the output tests... for some reasone cabeza->siguiente and cabeza->siguiente->siguiente of the third object I input to the linked list are pointing to the same memory chunk... I have been working on this 4 days and i still cant figure out how to make it work... i already had some guy from here to help me but his code is already been used here... here is his help

void PH1::AddObject(Object* o)
{
    if (!head)
    {
        head = o;
        head->next = NULL;
    }
    else
    {
        // Insert o at front of list (easier)
        o->next = head;
        head = o;
    }
}

please help me :D

Share this post


Link to post
Share on other sites
It looks like all you need to do is change one row inside if(myBool),

Change:
*cabeza = *nuevo;

To:
cabeza = nuevo;

It should do the trick ...

- Patrik Willbo

Share this post


Link to post
Share on other sites
TO LEARN :D


Oh and Wilbo, no that doesnt help, it actually makes every pointer inside the list point to the same thing... to nuevo

Share this post


Link to post
Share on other sites
No, it shouldn't point to the same ,,,
If I haven't missunderstood your variables cabeza is the same as head.
So the first time cabeza should hold the nuevo, which I assume is the same as new object.

Now, the second time addobject is called, that objects (nuevo) variable siguiente which I also assume is next, is assigned with cabeza.
And cabeza's next is NULL.
then cabeza is reassigned with nuevo, which will now hold it's next variable pointer to the old cabeza pointer.

1st time a->NULL
2nd time b->a->NULL
3rd time c->b->a->NULL
and so on...

It's exactly as the help code you got.
void PH1::AddObject( Obj* o )
{
// Insert to front of list
if( myBool )
{
o->next = NULL;
myBool = false;
}
else
o->next = head;

head = o;
}

If I'm not too tired, this is correct, or at least, should work. If I'm wrong and mistaken, please correct me now.

- Patrik Willbo

Share this post


Link to post
Share on other sites
Man... well it seems im doing something else wrong, could someone please help me make a .cpp file for this list that works? i need to learn how to do it

Share this post


Link to post
Share on other sites
Ok i redid my code for a simple list with simple objects that only hold a number in them...

But it seems its getting looped still in the display part... and it has to do with the pointers



#include "list.h"
#include "object.h"

List::List()
{
myBool = true;
}

void List::push(Object* o)
{
if(myBool)
{
head = o;
head->next = 0;
}
else
{
o->next = head;
head = o;
}
}

void List::display()
{
Object* o = head;
while(o != 0)
{

o->toString();
o = o->next;
}
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
People always make their own linked lists because the STL is invariably taught last, and people need the use of variable length arrays far before then [generally around the time they learn how to use pointers]

Personally, learning how to make linked lists was a leap in getting to know pointers.

Anyways, a small summary of my linked list code.

Note: I do not make the linked-list the class. I make the list elements the class. This way 'head', and mybool isn't needed to handle the odd case of the first list entry.


#include <iostream>

struct List{
int data;
List *next;

List(){
data=0;
next=0;
}
List(int inint){
data=inint;
next=0;
}

void push(List **);
void display();
};


void List::push(List **head){
//
// Push this onto the top of *head
//
//
// Common usage:
// List *foo=0;
// List *bar=0;
//
// bar=new List(10);
// bar->push(&foo);
//

if (*head){
this->next=*head;
}
*head=this;
}


void List::display(){
//
// Display this and all List objects linked to it
// delimited by newlines.
//

if (!this){
return;
}
cout << data << endl;
next->display();
}

void main(){

List *foo=0;
List *bar=0;

bar=new List(10);
bar->push(&foo);
bar=new List(-2);
bar->push(&foo);
bar=new List(200);
bar->push(&foo);
foo->display();
}




Class:

The class consists of a data element and a 'link' element [in this case ->next] which is a pointer to the next element. A simple linked list.

It also has a default constructor, and a constructor that takes an int as a param to initialize the structure. [remember, structures are just classes with everything public]

Now for the slightly tricky stuff in my implimentation.

First is the push(List **) function. The funny List ** is essentially a pointer to a pointer.

So a List object contains data for a list.
A *List points to that data and allows it to be changed when passed into a function.
A **List points to that pointer, and allows it to be changed when passed into a function.

Because we want the head of the list to be able to be changed, we have the programmer pass it into the function.

The push() function simply checks if the head of the list has data, and sets this->next to be the current top of the list. Since the top of the list now isn't the top of the list anymore, it changes that pointer to become the "new" top of the list.

Note that if the top of the list doesn't point to anything, the push function is nice enough to place it at the top of our new list for us.


Second is the diplay() function. The only tricky thing here is a little simple recursion. The display function prints the current data, and then tells itself to go to the next element and print that. It will continue on until this==0 [otherwise known as past the end of the list] at which point it will stop.


This little test code works for me using gcc, and should compile pretty much anywhere without issue.

The test output should be:

200
-2
10


(remember, putting things at the top will reverse their order!)

Share this post


Link to post
Share on other sites
Bah! that AP was me.

And it isn't my commonly used linked list code, just an example for a singly linked list with ints for lukar. [though mine is similar, but doubly linked so that List ** doesn't blow up the list if it doesn't happen to be the head]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this