Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I have create a linked list using union, is this possible??? If yes can you please show the snippet???...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    Linked list - union


    Hi All,

    I have create a linked list using union, is this possible???
    If yes can you please show the snippet???

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I don't know lets see your code

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by dayananda.ms View Post
    Hi All,

    I have create a linked list using union, is this possible???
    If yes can you please show the snippet???
    Mmmm, your post is a bit strange.

    If you have created it, then it is possible (the creation of an entity, even a list, automatically implies that the said entity exist). However, unless you are pursuing a very concrete purpose, the union itself has nothing to do with a list.

    It's possible to implement complex types like lists of union nodes, or even unions of linked lists (though that's highly counterproductive in terms of memory usage).

    So I am inclined to think that your post is full of typos and you are just asking if it's possible. The straight answer is no. If you have some code, show us so we can discuss a bit about it.

    I like to think of unions as the OR version of struct (it's just my mental model, never ever say that to a teacher if that's what you are after). A union stores one of it's member only. If an union holds int a; float b; char c; it will only store one of the three (a OR b OR c), but initially it will reserve memory for the biggest of them (float in this case). Struct in the contrary can store a AND b AND c because it reserves memory for the three of them separately.

    Alternatively, describe *what* you are after, so we can tell you the alternatives on *how* to achieve it.

    I hope I didn't confuse you too much, and sorry if what I am explaining is too trivial. Cheers

  4. #4
    Just Joined!
    Join Date
    Jun 2013
    Posts
    1
    Whats about if we create an array in union.

    union linked_node{
    unsigned int node[2];
    }


    int main()
    {
    union linked_node n1,n2,n3;
    n1.node[0] = 12;
    n2.node[0] = 22;
    n3.node[0] = 32;

    n1.node[1] = (union linked_node *)&n2;
    n2.node[1] = (union linked_node *)&n3;
    n3.node[1] = (union linked_node *)0;


    }


    Quote Originally Posted by i92guboj View Post
    Mmmm, your post is a bit strange.

    If you have created it, then it is possible (the creation of an entity, even a list, automatically implies that the said entity exist). However, unless you are pursuing a very concrete purpose, the union itself has nothing to do with a list.

    It's possible to implement complex types like lists of union nodes, or even unions of linked lists (though that's highly counterproductive in terms of memory usage).

    So I am inclined to think that your post is full of typos and you are just asking if it's possible. The straight answer is no. If you have some code, show us so we can discuss a bit about it.

    I like to think of unions as the OR version of struct (it's just my mental model, never ever say that to a teacher if that's what you are after). A union stores one of it's member only. If an union holds int a; float b; char c; it will only store one of the three (a OR b OR c), but initially it will reserve memory for the biggest of them (float in this case). Struct in the contrary can store a AND b AND c because it reserves memory for the three of them separately.

    Alternatively, describe *what* you are after, so we can tell you the alternatives on *how* to achieve it.

    I hope I didn't confuse you too much, and sorry if what I am explaining is too trivial. Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •