Data Structures in C


Data structure Concept

Data Structure:

The data structure is a way of storing, organizing representing the data.
Data structures are Array, Pointer, Structure, Unions, LinkedList, Stack, Tree Queue, Graph, Searching, Sorting, programs.
List the areas in which data structures are applied extensively?
  • Compiler Design,
  • Operating System,
  • Database Management System,
  • Statistical analysis package,
  • Numerical Analysis
  • Graphics,
  • Artificial Intelligence,
  • Simulation.
Data structures are the main part of computer science algorithms that enables the programmers to handle the data in an efficient way.

Arrays:

  • An array is a data structure organized as a linear collection of elements, for which an index (a numerical indicator of position) is used to access its content. The same information may appear in the array more than once but under a different index.
  • Elements are stored in contiguous memory locations.
  • Arrays Can access elements randomly using an index.
  • Stores homogeneous elements that are similar elements.

Syntax:

  • Array declaration.

            Datatype variablename[size];
  • Arrays can also do declaration and initialization at once.
            Datatype variablename[]={element1,element2,element3,element4,element5};

Advantages:

  • Random access.
  • Easy sorting and iteration.
  • Replacement of multiple variables.

Disadvantages:

  • The size is fixed.
  • Difficult to insert and delete.
  • If capacity is more and occupancy less, most of the array gets wasted.
  • Needs contiguous memory to get allocated.

Applications:

  • For sorting information in a linear fashion.
  • Suitable for applications that require frequent searching.

Demonstration of Array:

#include<stdio.h>
void main()
{
int rollNo[10];
for (int i=0;i<10;i++)
{
scanf("%d",&rollNo[i]);
}
for (int i=0;i<10; i++)
{
printf("%d",rollNo[i]);
}
}
Input: 121,122,123,124,125,126,127,218,128,129
Output: 121,122,123,124,125,126,127,218,128,129

2D Array:

The 2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns.

Syntax:

The syntax of declaring a two-dimensional array is very much similar to that of a one-dimensional array.
int arr[max_rows][max_columns];
#include <stdio.h>
void main()
{
   int m, n, i, j, matrix[10][10], transpose[10][10];
   printf("Enter the number of rows of the matrix\n"); 
   scanf("%d", &m);
   printf("Enter the number of columns of the matrix\n");
   scanf("%d", &n);
   printf("Enter elements of the matrix\n");
   for (i = 0; i < m; i++)
      for(j = 0; j < n; j++)
         scanf("%d", &matrix[i][j]);
   for (i = 0; i < m; i++)
      for( j = 0 ; j < n ; j++ )
         transpose[j][i] = matrix[i][j];
   printf("Transpose of the matrix:\n");
   for (i = 0; i < n; i++)
   {
   for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
           printf("\n");
   }
}
Output:
Enter the number of rows of the matrix
2
Enter the number of columns of the matrix
2
Enter elements of the matrix
1 2
3 4
Transpose of the matrix:
1 3
2 4