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.
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:
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:
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.
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:
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:
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];
}
}
}
Indian Institute of Embedded Systems – IIES