DATA STRUCTURE NOTES
Unit 1 – Introduction to Data Structure
Operations of Data Structures
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
What is Data Structure?
A data structure is a way of organizing and storing data so that it can be accessed and modified
efficiently. It helps in performing operations such as searching, insertion, deletion, and sorting
effectively.
Types of Data Structures
Primitive Data Structures Non■Primitive Data Structures
int, char, float, boolean Linear and Non■Linear structures
Array, Linked List, Stack, Queue (Linear)
Tree, Graph (Non■Linear)
Algorithms
An algorithm is a step■by■step procedure or set of rules used to solve a problem or perform a
computation.
Characteristics of an Algorithm
• Input – Algorithm must have input values.
• Output – Algorithm must produce at least one output.
• Unambiguity – Each instruction should be clear.
• Finiteness – Algorithm must terminate after finite steps.
• Effectiveness – Each instruction must be basic and executable.
Algorithm Complexity
The performance of an algorithm is measured using time complexity and space complexity.
Time Complexity
Time complexity measures the amount of time an algorithm takes to run depending on the input
size.
Space Complexity
Space complexity represents the amount of memory required by the algorithm during execution.
Asymptotic Notations
• Big■O (O) – Worst case complexity
• Big■Omega (Ω) – Best case complexity
• Big■Theta (Θ) – Average case complexity
Array
An array is a collection of elements of the same data type stored in contiguous memory locations.
Array Declaration
int A[5];
char name[10];
float marks[20];
Example Program
#include <stdio.h>
int main(){
int marks[5] = {28,39,52,71,85};
int i, sum=0;
for(i=0;i<5;i++){
sum += marks[i];
}
printf("%d",sum);
}
Stack
A stack is a linear data structure that follows the LIFO (Last In First Out) principle.
Basic Stack Operations
• Push – Insert element
• Pop – Delete element
• Peek – Access top element
• Traversal – Display stack elements
• Search – Find element in stack
Stack Implementation in C
#include <stdio.h>
#define MAXSIZE 3
int stack[MAXSIZE];
int top = -1;
int isFull(){
if(top==MAXSIZE-1) return 1;
else return 0;
}
int isEmpty(){
if(top==-1) return 1;
else return 0;
}