#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Help with implementing a linked list (C++)


    Hi guys.

    I want to implement a linked list class for me to use in my upcoming school assignments.

    here's the interface:

    Code:
    class LinkedList
    {
    private:
    	class Node
    	{
    		Node* next;
    		Node* prev;
    		int key;
    		friend class LinkedList;
    	};
    	Node* head;
    	Node* lastLocation;
    	Node* tail;
    	int length;
    public:
    	LinkedList();
    	LinkedList(const int aKey);
    	void Push(const int aKey);
    	void Add(const int aKey);
    	Node* Find(const int aKey);
    	void Remove(const int aKey);
    	~LinkedList();
    };
    so far i've implemented almost everything, but i'm having a problem with Node* Find(const int aKey);
    after being told here that Node is private and therfore the Find() method cannot return it, I tried to think about other ways to do it but couldn't think of one.
    Is there a way to implement a method that gets a key as parameter and returns a pointer the the node that holds that key without exclude the Node type and define it as an independent class?

    Thanks in advanced!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    I suppose the first question would be, what would you do with a Node* once you've got it out of the class?

    There would be no mechanism for you to do ptr->member outside the class to retrieve any data, and no other member function of your LinkedList class takes a Node* as a parameter.

    It seems to me the best thing to return is a boolean true (the key was found), or false (the key was not found).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    yeah, you're right.
    didn't think about it...

    Thanks for the help!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    What's the difference between Push and Add?

    Why is node a class? Consider the difference between class and struct.

    What's the point of a linked list anyway?
    I no longer wish to be associated with this site.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Originally Posted by jwdonahue
    What's the difference between Push and Add?
    Add() adds a Node at the end of the list, Push() adds it to the head of the list.

    Originally Posted by jwdonahue
    Why is node a class? Consider the difference between class and struct.
    We're not allowed to use structs in our assignments, only classes.

    Originally Posted by jwdonahue
    What's the point of a linked list anyway?
    The same point any other data stracture makes.
    Last edited by so.very.tired; April 8th, 2013 at 11:29 AM.

IMN logo majestic logo threadwatch logo seochat tools logo