Data Structures in C and different ways to use them

access_time 2019-12-01T12:23:15.169Z face Learnoa.com Data Structures

Data Structures in C are utilized to store information in a sorted out and effective way. The C Programming language has numerous information structures like a cluster, stack, line, connected rundown, tree, and so on. A software engineer chooses a proper information structure and uses it as per their accommodation.

ARRAY

The collection of elements that have the same data type is known as an Array. They are stored in a sequence in the memory. Precisely, Array is a data structure where there exists a similar kind of elements. These elements aren’t treated the same way objects in C are treated in Java.
Understand it this way –

Consider yourself in a store that houses a musical instrument. There I ask you to arrange all the Casio keyboards at one place in a particular sequence. Now the result would be a sequential collection known as Array. There are two types of arrays:

  • Single dimensional Array
  • Multidimensional Array

The syntax for one-dimensional Arrays:

data_type array_name[array_size];
//Employee IDs example
#include<stdio.h>
int main()
{   
  int i=0;
  int empID[5];
  empID[0]=10280;
  empID[1]=10260; 
  empID[2]=10270; 
  empID[3]=10285;   
  for(i=0;i<4;i++)
  {
      printf("%d n,empID[i]); 
  }
  return 0;
}
//Output:
10280
10260
10270
10285
10275
The syntax for two-dimensional Arrays:
data_type array_name[rows][columns];
#include<stdio.h>
int main()
{
    int disp[2][3];
    int i, j;
    for(i=0; i<2; i++)
    {
          for(j=0;j<3;j++)
          {
                 printf("Enter value for disp[%d][%d]:", i, j);
                 scanf("%d", &disp[i][j]);
           }
    }
    printf("Two Dimensional array elements:n");
    for(i=0; i<2; i++)
    {
          for(j=0;j<3;j++)
          {
                printf("%d ", disp[i][j]);
                if(j==2)
                {
                      printf("n");
                }
          }
    }
    return 0;
}
//Output:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3
4 5 6

WHAT IS A STACK?

If the data structure is linear, then it is known as a stack. In the 1st approach, it follows the last. At the top, a new object is added. The operation of deletion and insertion are executed from one end of the stack. Quite matching stack, there is another data structure that is known as Queue.

WHAT IS A QUEUE?

If the linear data structure is stored as a collection of elements, then it is known as a Queue. It operates based on the first in first out (FIFO) algorithm.

WHAT IS A LINKED LIST?

A Linked List is linear and is similar to an array, but it is not stored in the memory sequentially. There are two parts of a linked list - the data section and the address section. The address of the next element is held in the address section of the list, also known as a node. Its size isn’t fixed. This means that the data objects can be added at any location in the list. The only limitation you will get here is that if you want to get to a node, you must to from the first node to the next needed node. There are three types of link lists:

  • Singly link list
  • Doubly Link List
  • Circular link list

Followed by Linked Lists, We shall Learn Trees.

WHAT IS A TREE?

If there are one root node and different sub-nodes, then the data structure is named as a tree. It is one of those data structures that have a design from the top of a linked list.

WHAT IS A HASHING?

Hash table, the other kind of data structure, is used in the implementation of an associative array. This is a structure that helps in mapping keys to values. The Hash table is utilized as a hash function while computing an index into a range of buckets. These are quite useful data structures.