Data Structures through Java - Study Material
1. Introduction to Data Structures
Definition: Organized way to store, manage, and retrieve data.
Types:
- Primitive: int, float, char
- Non-Primitive: Arrays, Linked Lists, Stacks, Queues, Trees, Graphs
2. Arrays
Concept: Fixed-size container of elements of the same type.
Declaration:
int[] arr = new int[5];
Traversal:
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
Exercise: Write a program to find the maximum and second maximum in an array.
3. Linked Lists
Types: Singly, Doubly, Circular Linked List
Node Class:
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
Exercise: Implement insertion at beginning and end.
4. Stacks
Data Structures through Java - Study Material
LIFO (Last In First Out)
Implemented using arrays or LinkedList
Java Stack:
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.pop();
Exercise: Convert infix expression to postfix.
5. Queues
FIFO (First In First Out)
Types: Linear, Circular, Priority Queue
Java Queue:
Queue<Integer> q = new LinkedList<>();
q.offer(10);
q.poll();
Exercise: Implement Circular Queue using array.
6. Recursion
Definition: Function calling itself
Example:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
Exercise: Recursive Fibonacci and binary search.
7. Searching Algorithms
Linear Search: Simple but slow
Binary Search: Fast but needs sorted array
Time: O(log n)
Data Structures through Java - Study Material
Binary Search Code:
int binarySearch(int[] a, int key) {
int low = 0, high = a.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] == key) return mid;
else if (a[mid] < key) low = mid + 1;
else high = mid - 1;
return -1;
8. Sorting Algorithms
Bubble Sort, Selection Sort, Insertion Sort
Exercise: Write Bubble Sort and analyze steps.
9. Trees (Intro)
Binary Tree, Binary Search Tree
Traversals: Inorder, Preorder, Postorder
Exercise: Write recursive inorder traversal
10. Complexity Analysis
Time Complexity, Space Complexity
Common notations: O(1), O(n), O(log n), O(n²)