Prerequisites:
Before you get into data structures and algorithms you should have basic prior knowledge in C language, C++ or any other programming languages along with the topics like loops, functions, arrays, pointers, recursions and structures.
What will we learn in this Tutorial?
Learn various codes based on Data Structures and their Algorithms and use them in practical applications.
Increase your programming skills on Data Structures.
Implementation of Data Structures and Algorithms using C Language.
Topics to be covered –
Arrays Representation and Array Abstract Data Type
Linked List
Stack
Queues
Trees
Binary Search Tree
AVL Trees
Hashing Technique.
Graphs
Now we will start with a basic introduction to Data Structures and Algorithms.
Introduction to Data Structures and Algorithms
What is Data Structure?
Data Structure is a way of storing and organizing data in a system so that it can be used efficiently by the user. A data structure is a special layout for organizing and storing data. Basic Data Structure includes arrays, linked list, stack, queues, trees, graphs, etc.
Data Structure is classified into two types:
Linear Data Structure - In linear data structures, the elements are stored/ accessed in sequence one after the other. Example - linked list, stack and queues.
Non-linear Data structure - In non-linear data structures, the elements are stored/ accessed in non-linear or non-sequential order. Examples - trees and graphs.
What is an Algorithm?
An Algorithm is defined as a step-by-step instruction to solve a given problem. To analyze a given algorithm we need to know which algorithm takes less time and which algorithm takes a long time. Depending on this there are two types of analysis:
a.Best Case - The algorithm which takes least time for its execution.
b.Worst Case - The algorithm which takes a long time for its execution.
c. Average Case - The algorithm which takes average time for its execution.
Rules for calculating time complexity:
1. Time taken by the for loop is considered by the statements inside the for loop.
Ex:-
for (i=0; i<n ;i++) -------------(n+1)
x = x + i; ------------ n
__________
2n+1
Time complexity=O (n)
2. Time taken by Nested for loop is the product of the sizes of all loops.
Ex:-
for (i=0; i<n; i++) { ---------------- (n+1)
for (j=0; j<n; j++) { ---------------- n(n+1)
C[i][j]=A[i][j]+B[i][j]; ------------- (n*n)
}}
Time complexity=O (n²)
Abstract Data Types:
An abstract data type (ADT) is a set of operations that specifies what is to be and its implementation details are hidden. The basic idea is that the implementation of these operations is written once in the program and any other part of the program that needs to perform an operation on the ADT can do so by calling the appropriate function.
Example: Insertion, deletion, merging and set operations on arrays are Abstract Data types.
Memory Segments:
Code Segment - Code segment includes all machine code of the compiled program..
Initialized Data Segment - Initialized data segment stores global, static and external variables that are initialized beforehand.
Uninitialized Data Segment - This Data segment is initialized to arithmetic 0 before the program starts executing.
Stack - Stack segment stores all of the local variables and is used for passing arguments to the functions by call by reference or call by the address which is to be executed after the function call is over.
Heap - Heap segment is a segment where memory is allocated dynamically. Here memory is allocated by using malloc and calloc functions.