Introduction to Data Structures
Introduction to Data Structures
A data structure is a way of organizing and storing data so that it can be accessed and
modified efficiently. It determines the way data is arranged in memory and how operations
such as retrieval, insertion, and deletion are performed.
1. Primitive Data Structures: These are the basic building blocks for data
manipulation. Examples include:
o Integer
o Float
o Character
o Boolean
2. Non-Primitive Data Structures: These are more complex structures derived from
primitive types. They are further divided into:
o Linear Data Structures: Data elements are arranged sequentially. Examples:
Arrays
Linked Lists
Stacks
Queues
o Non-Linear Data Structures: Data elements are arranged hierarchically or
non-sequentially. Examples:
Trees
Graphs
o Hashing: Organizes data using hash functions for efficient searching.
1. Time Complexity: The amount of time taken to complete the algorithm as a function
of the input size.
2. Space Complexity: The amount of memory required to execute the algorithm.
Asymptotic Notations
Time-Space Trade-off
There is often a trade-off between time and space in algorithms. For instance:
Searching Algorithms
Linear Search
Definition: A simple method that checks every element in the array sequentially until
the desired element is found.
Time Complexity: O(n)
Algorithm
Binary Search
Algorithm
Summary
Data structures help in organizing and storing data efficiently.
Operations like insertion, deletion, and traversal are essential.
Algorithm analysis uses asymptotic notations to measure efficiency.
Searching algorithms like linear and binary search have different use cases and
complexities.
1. Array
Insertion:
c
Copy code
#include <stdio.h>
int main() {
int arr[10] = {1, 2, 3, 4, 5};
int n = 5;
Deletion:
c
Copy code
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
Traversal:
c
Copy code
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
2. Stack
c
Copy code
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void traverse() {
if (top == -1) {
printf("Stack is empty\n");
} else {
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
traverse(); // Print stack
return 0;
}
Deletion (Pop):
c
Copy code
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int main() {
push(10);
push(20);
push(30);
printf("Popped: %d\n", pop()); // Remove top element
return 0;
}
3. Queue
Insertion (Enqueue) & Traversal:
c
Copy code
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
void traverse() {
if (front == -1) {
printf("Queue is empty\n");
} else {
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
traverse(); // Print queue
return 0;
}
Deletion (Dequeue):
c
Copy code
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
int dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
return -1;
} else {
int value = queue[front++];
if (front > rear) {
front = rear = -1;
}
return value;
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printf("Dequeued: %d\n", dequeue()); // Remove front element
return 0;
}
4. Linked List
c
Copy code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
traverse(head); // Print list
return 0;
}
Deletion:
c
Copy code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
traverse(head); // Print list
delete(&head, 20); // Delete element 20
traverse(head); // Print list after deletion
return 0;
}