C++ Lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sake
    New Member
    • Jan 2007
    • 46

    C++ Lists

    Hi, Just a quick question about lists.

    This seemed a bit elementary at first, but nothing turned up on the web:
    How do I get a linked list in C++ with multiple elements.
    I need to copy this sort of thing, but using the <list> header:

    Code:
    struct linked{
      int element1;
      int element2;
      linked *next;
    };
    Is this even possible? If so, can I mix up the types of data? Like have an integer and a string element in the same list.

    Thanks,
    Austen
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If you are using C++ then you would be much better off using a <list> which will do a lot of the hardwork for you.

    If you want to hold different data types then the easiest method is to use a structure with an enum and a union in it

    Code:
    struct MyData {
        enum DataTypeId {
            DATA_TYPE_1_ID,
            DATA_TYPE_2_ID
        } type;
        union DataUnion {
            DATA_TYPE_1 type1;
            DATA_TYPE_2 type2;
        } data;
    }
    However I would only recommend that only if your data types are of similar sizes, I worked with a system once where every data type (1000+ of them) where held in structure like this from 4 byte integers up to structures of 1500+ bytes. Since it is a union internally every time you allocate data for one it has to allocate the data for the largest element so when allocating data for a 4 byte int it was allocating 1500+ bytes. Couple with the fact it's pointer usage was abysmal and it kept overwriting pointers that still pointed to allocated data meant that it leaked memory like a hula-hoop.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by sake
      Is this even possible? If so, can I mix up the types of data? Like have an integer and a string element in the same list.
      You can do this but when you mix types the different types need different handling. Generally, a list with ints and strings can't be sorted because there's no way to compare elements. However, if you insist, research the MIcrosoft VARIANT discriminated union.

      You will be much better off with a list<int> and a list<string>.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by sake
        Hi, Just a quick question about lists.

        This seemed a bit elementary at first, but nothing turned up on the web:
        How do I get a linked list in C++ with multiple elements.
        I need to copy this sort of thing, but using the <list> header:

        Code:
        struct linked{
          int element1;
          int element2;
          linked *next;
        };
        Is this even possible? If so, can I mix up the types of data? Like have an integer and a string element in the same list.

        Thanks,
        Austen
        I think (but I am not positive) that the others have misunderstood your question. Are you asking "How do I use list to hold a bunch of different elements?" If so, generate a struct and then pass that struct to the list as a template parameter:
        Code:
        struct linked{
          int element1;
          int element2;
        };
        list<linked> myList;
        Like any other struct, you can put whatever you want in it. Including strings, ints, doubles, user defined types, etc...

        Let me know if I have understood you correctly.


        Adrian

        Comment

        • sake
          New Member
          • Jan 2007
          • 46

          #5
          Originally posted by AdrianH
          I think (but I am not positive) that the others have misunderstood your question. Are you asking "How do I use list to hold a bunch of different elements?" If so, generate a struct and then pass that struct to the list as a template parameter:
          Code:
          struct linked{
            int element1;
            int element2;
          };
          list<linked> myList;
          Like any other struct, you can put whatever you want in it. Including strings, ints, doubles, user defined types, etc...

          Let me know if I have understood you correctly.


          Adrian
          Thanks for your reply.

          So after I've declared that, how do I access the members? The only way I can think of is to declare a linked variable.
          Code:
          struct linked{
            int element1;
            int element2;
          };
          
          using namespace std;
          
          int main(){
            linked me;
            list <linked> myList;
            me.element1 = 6;
            me.element2 = 2;
            myList.push_back(me);
            return 0;
          }
          From there, however, how do I print out the contents of the list?

          -Austen

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            If you want to have a variable that knows how to print itself, you should use a class and add the functions yourself (or overload the << operator).

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by sake
              Thanks for your reply.

              So after I've declared that, how do I access the members? The only way I can think of is to declare a linked variable.
              Code:
              struct linked{
                int element1;
                int element2;
              };
              
              using namespace std;
              
              int main(){
                linked me;
                list <linked> myList;
                me.element1 = 6;
                me.element2 = 2;
                myList.push_back(me);
                return 0;
              }
              From there, however, how do I print out the contents of the list?

              -Austen
              Use an iterator. A simple method is to do this:
              Code:
              list<linked>::iterator end = myList.end()
              list<linked>::iterator item = myList.begin();
              for (; item != end; ++item) {
                cout << item->element1 << endl;
              }
              Where I put element1, you can put any member element or member function you wish that is used by your struct. An iterator works very much like a pointer.

              If you are outputing all of the data in a consistant way, overload the operator <<() function for use with that struct/class. (if you haven't realised it yet, struct and class are almost identical).

              Adrian

              Comment

              • sake
                New Member
                • Jan 2007
                • 46

                #8
                Works great. Thanks alot :-)

                Comment

                • shashibhushan0286
                  New Member
                  • May 2007
                  • 2

                  #9
                  it is not reply just about link list.

                  I want comlete code of link list through class in c++. Pls send me as soon as possible on my e-mail.

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by shashibhushan02 86
                    it is not reply just about link list.

                    I want comlete code of link list through class in c++. Pls send me as soon as possible on my e-mail.
                    Sorry, we don't give out the complete code here on TSDN. But you can ask for help where you are having difficulty.

                    If you really want the complete code though, try the header file that came with the compiler. But I will warn you, it is most likely fairly difficult to read. It would also get you a definite F in any course you are taking as it would be way beyond your abilities, considering that you are asking use to provide you code for a list ;) (which is fairly simple BTW).

                    Good luck with that :D,


                    Adrian

                    Comment

                    Working...