STL Linked Lists

This is a discussion on STL Linked Lists within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make use of the STL for linked lists. However, I don't know which of <list> or <list.h> ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    STL Linked Lists

    I'm trying to make use of the STL for linked lists. However, I don't know which of <list> or <list.h> I should be using for linked lists. Should it be <list>? Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    It should be <list>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    When a list is instantiated, is it granted that the list would start out with no node?

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    if you construct it like this:
    Code:
    list<int> m_list;
    then, yes, there are no nodes.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The list is empty, its size is 0, and there's no data in it. Interestingly enough, that doesn't mean there are no nodes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thanks for your replies. I'm having a complaint when I'm instantiating my lists as described in my code of a class definition below:

    Code:
    #include <list>
    
    struct Item
    {
    	int PartNumber;
    	int Quantity;
    	int Cost;
    };
    
    struct LabourItem
    {
    	int LabourType;
    	int Cost;
    };
    
    class JobList
    {
    private:
    	list<Item> PartOrders;
    	list<LabourItem> Labour;
    public:
    	JobList();				// Default Constructor
    	void AddItem(int PartNumber, int Quantity, int Cost);
    	void AddLabourItem(int Labour, int Cost);
    	~JobList();				// Destructor
    };
    The compiler (Microsoft Visual C++ version 6.0) doesn't seem to link the red lines, even though that the types is a struct type. Do you see anything wrong with the struct types that I've defined?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Magic might happen if you say "std::list" instead.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thanks. I probably forgotten about "using namespace std;"

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I have another question regarding adding contents in the stl linked lists. For my class definition below:

    Code:
    #include <string>
    #include <vector>
    #include <exception>
    #include <list>
    using namespace std;
    
    #ifndef __Job_List_h__
    #define __Job_List_h__
    
    #include "Receptionist.h"
    #include "Technician.h"
    
    namespace Main_Class_Diagram
    {
    	class Receptionist;
    	class Technician;
    	class Job_List;
    }
    
    namespace Main_Class_Diagram
    {
    	struct Item
    	{
    		int PartNumber;
    		int Quantity;
    		int Cost;
    	};
    
    	struct LabourItem
    	{
    		int LabourType;
    		int Cost;
    	};
    }
    
    namespace Main_Class_Diagram
    {
    	class Job_List
    	{
    		private: Main_Class_Diagram::Receptionist* _unnamed_Receptionist_;
    		private: Main_Class_Diagram::Technician* _unnamed_Technician_;
    
    		private: list <Item> PartOrders;
    				 list <LabourItem> LabourOrders;
    
    		public: void AddItem(int PartNumber, int Quantity, int Cost);
    
    		public: void AddLabourItem(int Labour, int Cost);
    
    		public: void PrintInvoice();
    				void NewInvoice();
    	};
    }
    
    #endif
    Let's say that I want to implement the AddItem function:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <exception>
    #include <list>
    using namespace std;
    
    #include "Job_List.h"
    #include "Receptionist.h"
    #include "Technician.h"
    
    void Main_Class_Diagram::Job_List::AddItem(int PartNumber, int Quantity, int Cost) {
    	PartOrders.push_back();
    
    	PartOrders.PartNumber = PartNumber;
    	PartOrders.Cost = Cost;
    	PartOrders.Quantity = Quantity;
    }
    For some reason, I can't access the highlights in my IDE to the struct components properly. Does anyone know whether I'm accessing the struct components of PartOrders properly? How would I store those values into the struct contents properly?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't push_back ... nothing. Create an Item, set its fields, and push_back that Item.

    Edit: IOW, the list doesn't have any fields, which is why you can't access them. Items have fields.
    Last edited by tabstop; 04-10-2008 at 11:37 PM.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    That brings me another question: If I want to access any one of the entries, do I have to store the Item into a temporary Item somehow?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No, you should use an iterator to access elements of the list.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    By the way, each name that contains a double underscore or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. As such, instead of using __Job_List_h__ as your header inclusion guard name, use say, JOB_LIST_H_.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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