Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort

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;    };  



ADVERTISEMENT
ADVERTISEMENT