×

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure or Union overcome this problem. Structure or Union have used to store the mixed type of data. That is, the Structure or Union is a group of mix data type which are used separately.

Structure

The structure is a user-defined data-type in the data structure that allows the combination of various types of the data-type. It is used to describe a record. It is represented by the struct keyword in the program.

To define the structure, it uses the struct structure_name statement, as shown in syntax.

Syntax

 struct structure_name
    {   
data-type member 1;   
data-type member 2;   …   
data-type member n;   
};        

Note: struct structure_name is used to define the name of the structure and data-type member defines the data-type.

Advantages of Structure

  1. It can store different types of the data-type.
  2. It is user-friendly and simple to understand.
  3. It requires less time to write in the program.

Program

#include<stdio.h>
#include<conio.h>
void main( ) 
{   
struct Teacher 
{   
int teacher_id ; 
float salary ; 
int mobile_no ; 
}   ; 
struct Teacher t1,t2,t3; 
clrscr();   
printf("\nEnter the teacher_id, salary & mobile_no of the teacher\n"); 
scanf("%d %f %d", &t1.teacher_id, &t1.salary,   &t1.mobile_no); 
scanf("%d%f %d", &t2.teacher_id, &t2.salary, &t2.mobile_no);   
scanf("%d %f %d", &t3.teacher_id, &t3.salary,   &t3.mobile_no); 
printf("\n Entered Result "); 
printf("\n%d %f %d", t1.teacher_id, t1.salary, t1.mobile_no); 
printf("\n%d%f %d", t2.teacher_id, t2.salary, t2.mobile_no); 
printf("\n%d %f %d", t3.teacher_id, t3.salary, t3.mobile_no); 
getch();                                                                
  }    

Union

The Union is a user-defined data type in the data structure that allows different data types to be stored in the same memory location. It is represented by the union keyword in the program. A union of several members may be defined together, but at any time, only one member can be valued. Union provides an effective way to use the same memory space for multiple purposes.

To define the Union, it uses the union union_name statement, as shown in syntax.

Syntax

union union_name   
{   
data-type member 1;  
data-type member 2;   …   
data-type member n;  
 };  

Note: union union_name is used to define the name of the Union and data-type member defines the data-type.

Union works like structure while making the program of the Union, union is written instead of struct keyword.

Program

#include<stdio.h>
#include<conio.h>
void main( )
\
 {  
 union Teacher 
{   
int teacher_id ;
 float salary ;
 int mobile_no ; 
} ;
 union Teacher t1,t2,t3; 
clrscr();   
printf("\nEnter the teacher_id, salary & mobile_no of the teacher\n"); 
scanf("%d %f %d", &t1.teacher_id, &t1.salary,   &t1.mobile_no); 
scanf("%d%f %d", &t2.teacher_id, &t2.salary, &t2.mobile_no);   
scanf("%d %f %d", &t3.teacher_id, &t3.salary,   &t3.mobile_no);
printf("\n Entered Result "); 
printf("\n%d %f %d", t1.teacher_id, t1.salary, t1.mobile_no); 
printf("\n%d%f %d", t2.teacher_id, t2.salary, t2.mobile_no); 
printf("\n%d %f %d", t3.teacher_id, t3.salary, t3.mobile_no); 
getch();                                                               
   }    

Advantages of Union

  1. It takes the less memory space than the structure.
  2. It is used when two or more data member has to use the same memory place.

Difference between structure and union

Structure Union
It is represented by the struct keyword in the program. It is represented by the union keyword in the program.
In the structure, every member is assigned a separate memory location. In the Union, every member is shared the same memory location.
The memory size of the structure is equal to the sum of the memory size of each data-type member. The memory size of the union is equal to the largest size of all data-type in the union.
It supports a flexible array. It does not support a flexible array.
In the structure, it can be stored multiple values of the different data members at a time. In the union, it can be stored one value at a time.
Syntax  struct structure_name   {     data-type member 1;     data-type member 2;     …     data-type member n;    };   Syntax    union union_name   {     data-type member 1;     data-type member 2;     …     data-type member n;    };  

Related Topics

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side In this problem, we have given a singly linked list, and we need to remove all...

3 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

String Operations in Data Structures

Operations on Strings Reversing the order of words in a sentence Reversing a string is a technique that reverses or alters the order of a given string so that the last character...

9 minutes read.

Program to calculate the area of the circumcircle of an equilateral triangle

You have given one value which represents the side of the equilateral triangle. You have to find out the area of the circumcircle. Let’s take an example - For the above...

3 minutes read.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

3 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Difference between B-tree and Binary Tree

What is B-TREE? The nodes of B-tree are sorted during in-order traversal, and it is called self-balancing tree. A node in a B-tree can have more than two offspring, in contrast...

3 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Fundamental of Algorithms

An algorithm is a part of any programming solution or coding. If we have to make a solution then first we have to think of a clear idea about the...

13 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.