Understanding Link List

This is a discussion on Understanding Link List within the C++ Programming forums, part of the General Programming Boards category; Will someone help me out in building a link list with a tail and header pointers? I did google, but ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Understanding Link List

    Will someone help me out in building a link list with a tail and header pointers?
    I did google, but i couldnt find anything useful.

    Code:
    struct Node
    {
       Nutricalc Data;
       Node* next;
    };
    
    
    
    
    class FoodLists
    {
    
    public:
    
    	// constructors
        FoodLists();
        FoodLists(char fileName[]);
        FoodLists(int initCapacity);
    
        FoodLists(const FoodLists& aList);
    
    
    	// member functions
        void deletefood(int index);
        void addFood(const Nutricalc &aFood);
        void listFood();
        bool searchByName(char title[], Nutricalc& afood) const;
        bool searchByCategory(Category cat, FoodLists & food) const;
        int getSize()const;
        void writeOut(char fileName[]) const;
        
        // destructor
        ~FoodLists();
    
    	
    private:
    	//data members
       Node * head;
       Node * tail;
       int size; 
    
    / void addAtBeginning(const Nutricalc& afood);
      void addAtEnd(const Nutricalc& afood);
    //void addSorted(const Nutricalc& afood);
    
    };
    now if i want to build a list with my head and tail pointers i would do this?

    head = tail;
    tail->next = new Node; // or NULL?
    tail = tail->next;

    will that give me an empty list using the class FoodLists tail and head pointer?
    any help is much appreciated!

    Corey

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    I didn't get your question. Does the problem require using the FoodLists class? If you want to have an empty class, why do you allocate memory for the tail node? And what does it mean to use tail and head pointers of FoodLists class to create a linked list? FoodLists is already a linked list wrapper. Why don't you use public functions of the class to create a list then?

    If you want to create a list involving two nodes, this is what you might want to do.

    Node* pHead = NULL;
    Node* pTail = NULL;
    pHead = new Node();
    pHead->next = new Node();
    pTail = pHead->next;
    pTail->next = NULL;

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Sorry for the confusion. I want to use the head and tail pointer i have already created in the foodlists class. The reason why i said an empty list is i thought it would be easier to make for learning purposes. Now of course I will be filling it up with information, which i know how to do. I have some functions already made.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can probably find 1500 good descriptions of linked lists on the web. So what's the point in me (or anyone else on this forum) writing yet another one.

    Instead, post YOUR code, and we can look at it.

    Try to post a complete, standalone example, rather than fragments of code - it is surprisingly often that people post small portions of code saying "this doesn't work", where the fault is NOT in the code that has been posted, but for one or another reason the poster didn't understand/know this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 09:28 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21