fbpx

Mastering Data Structures in C: Organizing and Optimizing Data for Efficient Programming



INTRODUCTION

Data structures and algorithms is a foundational concept in computer programming language. Data structure is a way to store and organize the data so that it can be used efficiently. As per name indicates itself its organizing the data in memory. The data structure is not like any programming language it is set of algorithms that we can use in any programming language to create a structured data in memory.

  1. Primitive Data Structures:

Primitive data structures are the fundamental types of data. They are the building blocks for more complex data structures. Here are the key primitive types:

  • Integer (int): Used for whole numbers, both positive and negative.
  • Float (float): Stores single-precision floating-point numbers.
  • Double (double): For double-precision floating-point numbers.
  • Character (char): Stores a single character, such as ‘A’ or ‘z’.
  1. Non-Primitive Data Structures:

These are more complex and are derived from the primitive types. They allow us to organize data in more sophisticated ways. Non-primitive structures can be divided into two main categories: linear and non-linear.

A. Linear Data Structures:

In linear data structures, elements are arranged in a sequence manner. Each element connects to its neighbors, making it easy to traverse through them. Here are some common linear data structures:

  • Arrays:
    • An array is a collection of elements of the same data type stored in contiguous memory locations.
    • You can access elements using indices.
    • Example syntax: data_typearray_name[size];
  • Linked Lists:
    • A linked list consists of nodes, where each node contains data and a link (pointer) to the next node.
      • Singly Linked List:-Each node points to the next node.
      • Doubly Linked List:- Each node points to both the next and the previous nodes.
      • Circular Linked List:- The last node points back to the first, forming a circle.
  • Stacks:
    • A stack follows the LIFO(Last In First Out) principle, meaning the last item added to the stack is the first to be removed.
    • Additions and deletions are made at the top of the stack.
    • Key operations include:
      • Adding an element to the top is called push.
      • Removing the top element is called pop.
  • Queues:
    • A queue follows the FIFO (First In First Out) principle, where the first item added to the stack is the first to be removed.
    • Additions are made at one end and deletions are made at the other end.
    • Key operations include:
      • Adding an element to the back is called enqueue.
      • Removing an element from the front is called dequeue.
      •  

B. Non-Linear Data Structures:

Non-linear data structures organize the data elements are not in sequence manner, allowing for more complex relationships. They can be hierarchical or interconnected.

  • Trees:
    • A tree is a hierarchical structure with a root node and child nodes.
    • Types include:
      • Binary Tree:- Each node has at most two children.
      • Binary Search Tree (BST):- A binary tree where the left child is smaller than the parent, and the right child is larger.
      • AVL Tree:- A self-balancing binary search tree.
      • Heap:- A complete binary tree used for priority queues.
  • Graphs:
    • A graph consists of nodes (vertices) connected by edges.
    • Types include:
      • Directed Graph (Digraph): Edges have a direction.
      • Undirected Graph: Edges connect vertices without a direction.
      • Weighted Graph: Each edge has an associated weight or cost.
      • Unweighted Graph: Edges have no weights.
 

Differences Between Linear and Non-Linear Data Structures:

Aspect

Linear Data Structures

Non-Linear Data Structures

Storage Structure

Sequential and contiguous

Hierarchical or interconnected

Memory Utilization

Fixed size, less flexible

Dynamic, more flexible

Complexity

Simpler to implement

More complex and sophisticated

Examples

Arrays, Linked Lists, Stacks, Queues

Trees, Graphs

Functions:

Function is a reusable block of code that performs a specific task. They help break your code into manageable sections, improving readability and maintainability.

Advantages of Functions:

  • Code Reusability
  • Easier Debugging
  • Improved Readability
  • Scalability

Differences BetweenCall by Value and Call by Reference:

 

Feature

Call by Value

Call by Reference

Definition

Passes a copy of the value

Passes the address of the variable

Parameter Type

Actual values of arguments

Memory addresses (pointers)

Effect on Original Data

No effect on original data

Modifies the original data

Memory Usage

More memory for copying

Less memory for passing addresses

Performance

Can be slower for large data

Typically faster for large data

Side Effects

No side effects

Can have side effects

Example Syntax

modifyValue(num);

modifyValue(&num);

Arrays:

An array is a collection of elements of the same data type stored in contiguous memory locations.

Key Points about Arrays:

  1. Arrays are Always Passed by Address: When you pass an array to a function, you’re actually passing a pointer to the first element. This allows the function to modify the original array directly.
  2. Array Name Refers to Base Address: The name of the array acts as a pointer to the first element.

Example 1: C Program to Store and Display Array Elements:

Code:

 

#include <stdio.h>

voidgetting_arr_ele(intarr[], intlen);

voiddisplay_arr(intarr[], intlen);

 

int main() {

int n;

printf(“Enter the number of Array elements: “);

scanf(“%d”, &n);

intarr[n];

getting_arr_ele(arr, n);

display_arr(arr, n);

return 0;

}

 

voidgetting_arr_ele(intarr[], intlen) {

for (int i = 0; i <len; i++) {

scanf(“%d”, &arr[i]);

    }

}

 

voiddisplay_arr(intarr[], intlen) {

for (int i = 0; i <len; i++) {

printf(“%d “, arr[i]);

    }

}

Example 2: Finding Leaders in an Array:

Finding all the leaders in an array. An element is considered a leader if it’s greater than all the elements to its right.

Input: A line of integers (e.g., 16 17 4 3 5 2).

Output: Print all the leaders, separated by spaces (e.g., 17 5 2).

Note: The rightmost element is always a leader.

 

                    Code:

#include <stdio.h>

 

voidlea_in_arr(intarr[], intlen);

 

int main() {

int n;

printf(“Enter the number of Array elements: “);

scanf(“%d”, &n);

intarr[10];

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

scanf(“%d”, &arr[i]);

    }

lea_in_arr(arr, n);

return 0;

}

 

voidlea_in_arr(intarr[], intlen) {

int max = arr[len – 1];

printf(“%d “, max);

for (int i = len – 2; i >= 0; i–) {

if (arr[i] > max) {

printf(“%d “, arr[i]);

max = arr[i];

        }

    }

}