Differentiate between Union and Structure
| Union | Structure |
|---|---|
| A union is a user-defined data type that allows multiple data types to be stored in the same memory location. | A structure is a user-defined data type that allows a collection of variables to be grouped together under a single name. |
| In a union, only one member can be accessed at a time. Accessing one member overwrites the other members. | In a structure, all members can be accessed individually. |
| The size of a union is determined by the largest member in the union. | The size of a structure is determined by the sum of the sizes of all its members. |
| Unions are typically used when the memory needs to be conserved or when we want to interpret a piece of memory in different ways. | Structures are typically used when we want to group together related data items. |
| Unions are less commonly used compared to structures. | Structures are more commonly used compared to unions. |
Primitive and Non-Primitive Data Structure
| Primitive Data Structure | Non-Primitive Data Structure |
|---|---|
| Primitive data structures are basic data types that can be directly operated upon by the machine. | Non-primitive data structures are derived data types that are created by combining or aggregating primitive data types. |
| Primitive data structures have a fixed size in memory. | Non-primitive data structures can have a variable size in memory. |
| Primitive data structures are usually not dynamic, meaning that their size cannot be changed during runtime. | Non-primitive data structures are often dynamic, meaning that their size can be changed during runtime. |
| Primitive data structures are passed by value, which means that the actual data value is copied to a new location in memory. | Non-primitive data structures are passed by reference, which means that only the memory address of the data is passed to a new location. |
| Examples of primitive data structures include integers, floating-point numbers, characters, and boolean values. | Examples of non-primitive data structures include arrays, lists, trees, graphs, and objects. |
Linear and Non Linear Data Structure
| # | Linear Data Structure | Non-Linear Data Structure |
|---|---|---|
| 1 | A linear data structure is a data structure where data elements are arranged sequentially or linearly, and each element has a predecessor and a successor except the first and last elements. | A non-linear data structure is a data structure where data elements are not arranged sequentially or linearly, and each element may have zero, one or many predecessors and successors. |
| 2 | Linear data structures are easy to traverse and search, and are suitable for many applications such as arrays, stacks, queues, and linked lists. | Non-linear data structures are more complex to traverse and search, and are suitable for applications such as trees, graphs, and hash tables. |
| 3 | Examples of linear data structures include arrays, stacks, queues, and linked lists. | Examples of non-linear data structures include trees, graphs, and hash tables. |
| 4 | Linear data structures can be easily represented using arrays or linked lists. | Non-linear data structures require more complex representations such as trees, graphs, or hash tables. |
| 5 | Linear data structures have a linear space complexity, which means that the amount of memory used is directly proportional to the number of data elements. | Non-linear data structures can have a non-linear space complexity, which means that the amount of memory used may not be directly proportional to the number of data elements. |