Introduction Concepts of Data Structures in C

Share the love

Concepts of Data structures:

  • Introduction
  • Array
  • Stack
  • Queue
  • Link list
  • Binary Tree and binary search
  • Heap
  • Hashing

Nowadays, Data structures in C is a very important course for Computer science programs. Here are some basic concepts of data structures in C programming.

C programming is the most popular and widely used language as it is simple and easy to learn.

Data structure is the way of storing the data properly and organizes them when needed.

ARRAY

An array is a foremost thing, to begin with understanding concepts of data structures. An array is used to store homogeneous elements i.e., similar kinds of elements, at contiguous locations so that elements can be accessed using an index. The size of an array is decided before storing the data.

Or in short, Array is a sequential collection of elements of the same data type.

There are some basic operations supported by an array:

  • Traversing – used to print all the elements of an array one by one.
  • Insertion – used to add elements at a particular index in an array.
  • Deletion – used to delete elements from an array.
  • Searching – used to search an element in an array.
  • Updating – used to update elements at a given index.

In programs, Arr is an array with n elements and k is an integer such that k<=n.

Here are some programming examples to understand data structures.

Program for Traversing an Array:

#include <stdio.h>

main() {

   int Arr[] = {1,3,5,7,8};

   int item = 10, k = 3, n = 5;

   int i = 0, j = n;   

   printf(“The array elements are :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

}

Output:

The array elements are:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 5 

Arr[3] = 7 

Arr[4] = 8 

Program for Insertion in an array:

#include <stdio.h>

main() {

   int Arr[] = {1,3,5,7,8};

   int item = 10, k = 3, n = 5;

   int i = 0, j = n;

   printf(“The array elements are :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

   n = n + 1;

   while( j >= k) {

      Arr[j+1] = Arr[j];

      j = j – 1;

   }

   Arr[k] = item;

   printf(“The array elements after insertion :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

}

Output:

The array elements are:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 5 

Arr[3] = 7 

Arr[4] = 8 

The array elements after insertion:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 5 

Arr[3] = 10 

Arr[4] = 7 

Arr[5] = 8 

Program for Deletion in an array:

#include <stdio.h>

void main() {

   int Arr[] = {1,3,5,7,8};

   int k = 3, n = 5;

   int i, j;

   printf(“The array elements are :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

   j = k;

   while( j < n) {

      Arr[j-1] = Arr[j];

      j = j + 1;

   }

   n = n -1;

   printf(“The array elements after deletion :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

}

Output:

The array elements are:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 5 

Arr[3] = 7 

Arr[4] = 8 

The array elements after deletion:

Arr[0] = 1 

Arr[1] = 3

Arr[2] = 7 

Arr[3] = 8 

Program for Searching in an Array:

#include <stdio.h>

void main() {

   int Arr[] = {1,3,5,7,8};

   int item = 5, n = 5;

   int i = 0, j = 0;

   printf(“The array elements are :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

   while( j < n){

      if( Arr[j] == item ) {

         break;

      }

      j = j + 1;

   }

   printf(“Found element %d at position %d\n”, item, j+1);

}

Output:

The array elements are:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 5 

Arr[3] = 7 

Arr[4] = 8 

Found element 5 at position 3

Program for Updating an array:

#include <stdio.h>

void main() {

   int Arr[] = {1,3,5,7,8};

   int k = 3, n = 5, item = 10;

   int i, j;

   printf(“The array elements are :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

   Arr[k-1] = item;

   printf(“The array elements after updation :\n”);

   for(i = 0; i<n; i++) {

      printf(“Arr[%d] = %d \n”, i, Arr[i]);

   }

}

Output:

The array elements are:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 5 

Arr[3] = 7 

Arr[4] = 8 

The array elements after insertion:

Arr[0] = 1 

Arr[1] = 3 

Arr[2] = 10 

Arr[3] = 7 

Arr[4] = 8 

STACK

Stack follows LIFO policy i.e., Last-In-First-Out. It means the elements added in last will be accessed first. That’s why Stack is Abstract data type(ADT) means set of rules of operations that are allowed on data.

Insertion act as PUSH and Deletion act as POP

Example – A deck of cards, stack of plates.

Below are some programming examples to understand concept of stack.

To check whether the stack is empty or not:

IS_EMPTY(S)

  if S.top == 0

      return TRUE

  return FALSE

S = stack

x = element that is to be pushed.

To add an element to the stack i.e., PUSH

PUSH(S, x)

  S.top = S.top+1

  if S.top > S.size

      Error “Stack Overflow”

  else

      S[S.top] = x

To remove an element from the stack i.e., POP

POP(S)

  if IS_EMPTY(S)

      Error “Stack Underflow”

  else

      S.top = S.top-1

      return S[S.top+1]

QUEUE

Queue follows FIFO policy i.e., First-In-First-Out. It means the element entered first will be served first.

Considering head and tail to represent queue, head points to the oldest element added to the queue and tail points to the newest element added to the queue.

Below are some programming examples to understand concept of Queue

Check whether the queue is empty that means head and tail pointed to the same location :

IS_EMPTY(Q)

  If Q.tail == Q.head

      return True

  return False

Check whether the queue is full that means the head of a queue is 1 more than the tail:

IS_FULL(Q)

  if Q.head = Q.tail+1

      return True

  Return False

The two operations that are done on Queue:

En queue – The operation that adds an element to the queue.

Enqueue(Q, x)

  if isFull(Q)

      Error “Queue Overflow”

  else

      Q[Q.tail] = x

      if Q.tail == Q.size

          Q.tail = 1

      else

          Q.tail = Q.tail+1

Dequeue – The operation that removes elements from the last queue. It is similar to pop operation.

Dequeue(Q, x)

  if isEmpty(Q)

      Error “Queue Underflow”

  else

      x = Q[Q.head]

      if Q.head == Q.size

          Q.head = 1

      else

          Q.head = Q.head+1

      return x

This brings us to the end of the blog on concepts of data structures in C. We hope that you were able to gain valuable insights from the same. Happy Learning!

Read also:

How to connect Brother Printer to computer and MacBook with the help of a cable

How does website design affects SEO and UX MARKETING?


Share the love

One thought on “Introduction Concepts of Data Structures in C

Comments are closed.