Enjoy an ad free experience by logging in. Not a member yet? Register.
1. ## 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. I don't know lets see your code

3. Originally Posted by dayananda.ms
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. \$spacer_open \$spacer_close
5. Whats about if we create an array in union.

unsigned int node[2];
}

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

}

Originally Posted by i92guboj
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
•