Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Linked List Of Objects (http://www.programmingforums.org/showthread.php?t=13151)

Ade May 13th, 2007 10:00 PM

Linked List Of Objects
 
Hi,

I need to be able to hold a linked list of pointers to objects of a class I made myself. I know to add to a linked list something like this:

:

        //construction
        LinkedList();

        //add a new node to the last
        void addToList(int *data);


Just wondering what I replace the (int *data) part with?


Cheers

Seif May 13th, 2007 10:05 PM

replace the int with the data type you are wanting to save to your linked list ? not quite sure if get what you mean. are all the objects of the same type ?

Ade May 13th, 2007 10:08 PM

Sorry for not explaining myself very well.

I have a class Vehicle so in my code I have a line like...

:


Vehicle *myVeh = new Vehicle;


So that's what I want to store in the linkedlist, the myVeh pointer. NOt sure what I would replace int with.

Seif May 13th, 2007 10:11 PM

void addToList(Vehicle *data); ?

I'm assuming that the current implementation you are using are storing pointers to integer values ?

void addToList( new Vehicle(...) );

or just pass the addToList myVeh.

Seif May 13th, 2007 10:13 PM

oooh Dawei, I saw that post before you removed it ;)

DaWei May 13th, 2007 10:14 PM

LOL. It was a virtual duplicate, including the time stamp.

Seif May 13th, 2007 10:17 PM

Quote:

Originally Posted by DaWei (Post 127844)
LOL. It was a virtual duplicate, including the time stamp.

aye virtually... apart from the syntax error of course. :p I'll catch you out some day.

Ade May 13th, 2007 10:33 PM

That is what I thought was very unsure though.

I tried it and now I get this Error:

error C2664: 'Node::setData' : cannot convert parameter 1 from 'Vehicle *' to 'int *'

The code around the error location is...

:


void LinkedList::addToList(Vehicle *data)
{
        Node *newNodePtr;

        //craete new instance of Node
        newNodePtr = new Node;

        //set data
        newNodePtr->setData(data);      <<< ERROR LINE <<<

        //check if list is empty
        if( _begin == NULL)
        {
                _begin = newNodePtr;
        }
        else
        {
                newNodePtr->setPrevNode(_end);
                _end->setNextNode(newNodePtr);
        }

        //set current pointer end end pointer
        _end = newNodePtr;
        _current = newNodePtr;
}


Any idea?

Seif May 13th, 2007 10:37 PM

yeah, you haven't changed the node to take a pointer to a Vehicle. You will need to change whatever it is in your Node class that is int * to Vehicle *.
At current you are trying to set a Pointer of an Integer type to a pointer of a Vehicle type.
If you are still stuck, show me your Node class and I'll show you where.

Ade May 14th, 2007 9:02 AM

Hi, thanks a lot for your replies, tried a few things but can't seem to get it right.

here's my code

list.h...
:

#include "vehicle.h"

//forward declaration
class Node;

//class definition
class LinkedList
{
public:
        //construction
        LinkedList();

        //add a new node to the last
        void addToList(int *data);

        //find an element in list and set current pointer
        void find( int key);

        //get data from element pointed at by current pointer
        int* getCurrent(void);

        //delete element pointed at by current pointer
        void deleteCurrent(void);

private:
        //data members
        Node *_begin;                //pointer to first element in list
        Node *_end;                //pointer to last element in list
        Node *_current;        //pointer to current element in list
};


list.cpp
:


#include "stdafx.h"
#include <iostream>
using namespace std;

//class definition

#define NOT_IN_LIST "not in list"

class Node
{
public:
        //construction and destruction
        Node();
        ~Node();

        //to set and get the next node in the list
        void setNextNode(Node *next);
        Node* getNextNode(void);

        //to set and get the previous node in the list
        void setPrevNode(Node *next);
        Node* getPrevNode(void);

        //to set and access data hold in node
        void setData(int *data);
        int* getData(void);

private:
        //data members
        Node *_next;        //pointer to next node in list
        Node *_prev;        //pointer to previous node in list
        int *_data;        //pointer to data hold in node
};



Node::Node()
{
        //initialise the next and previous node pointers
        _next = NULL;
        _prev = NULL;

        //initialise the data pointer
        _data = NULL;
}




Node::~Node()
{
        //delete data that is hold in node
        delete _data;
}



void Node::setNextNode(Node *ptr)
{
        //next node pointer points to new location
        _next = ptr;
}




Node* Node::getNextNode(void)
{
        //return pointer to next node
        return _next;
}






void Node::setPrevNode(Node *ptr)
{
        //previous node pointer points to new location
        _prev = ptr;
}




Node* Node::getPrevNode(void)
{
        //return pointer to previous node
        return _prev;
}


void Node::setData(int *ptr)
{
        //data pointer points to new location
        _data = ptr;
}




int* Node::getData(void)
{
        //return pointer to data location
        return _data;
}




LinkedList::LinkedList()
{
        //initialise node pointers
        _begin = NULL;
        _end = NULL;
        _current = NULL;
}



void LinkedList::addToList(int *data)
{
        Node *newNodePtr;

        //craete new instance of Node
        newNodePtr = new Node;

        //set data
        newNodePtr->setData(data);

        //check if list is empty
        if( _begin == NULL)
        {
                _begin = newNodePtr;
        }
        else
        {
                newNodePtr->setPrevNode(_end);
                _end->setNextNode(newNodePtr);
        }

        //set current pointer end end pointer
        _end = newNodePtr;
        _current = newNodePtr;
}


int* LinkedList::getCurrent(void)
{
        if(_current != NULL)
        {
                return _current->getData();
        }
        else
        {
                throw exception(NOT_IN_LIST);
        }
}



void LinkedList::deleteCurrent(void)
{
        if(_current != NULL )
        {
                //Warning: something is missing here! Can you see what?!
                _current->getPrevNode()->setNextNode(_current->getNextNode());
                _current->getNextNode()->setPrevNode(_current->getPrevNode());
               
                delete _current;
                _current = NULL;
        }
        else
        {
                throw exception(NOT_IN_LIST);
        }
}


void LinkedList::find(int key)
{
        Node *nodePtr;

        //let pointer point to first element in list
        nodePtr = _begin;

        while(nodePtr != NULL )
        {
                if(*nodePtr->getData()== key)
                {
                        _current = nodePtr;
                        return;
                }
                nodePtr = nodePtr->getNextNode();
        }

        _current = NULL;
        throw exception(NOT_IN_LIST);
}




I've tried changing all the 'int' to 'Vehicle' but as yet have had no joy.

To recap, I'm trying to use this code given to us by our lecturer so that the linked list will contain pointers to objects of the Vehicle class.

Anyone have any ideas?

Oh, and does anyone know what's missing from the deleteCurrent() function?

Thanks in advanced


All times are GMT -5. The time now is 2:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC