Data Structure
Data Structure
java
CopyEdit
int num = 100;
java
CopyEdit
float price = 10.99f;
double pi = 3.14159;
oMemory Usage:
float (4 bytes), double (8 bytes).
3. Character (char)
o Stores a single character.
o Example:
java
CopyEdit
char letter = 'A';
java
CopyEdit
boolean isPassed = true;
1. Arrays
o A collection of elements of the same type stored in contiguous memory.
o Elements are accessed using an index.
o Example:
java
CopyEdit
int numbers[] = {1, 2, 3, 4, 5};
c
CopyEdit
struct Student {
char name[50];
int age;
float marks;
};
java
CopyEdit
class Student {
String name;
int age;
float marks;
}
Characteristics of Union:
Uses shared memory, meaning it can store different types but only one at a time.
Helps in memory-efficient programs.
Occupies memory equal to the largest member in it.
Used in cases where a variable can take multiple types but only one is active at a time.
java
CopyEdit
class Book {
String title;
int pages;
}
java
CopyEdit
class Node {
int data;
Node next;
}
java
CopyEdit
Stack<Integer> stack = new Stack<>();
stack.push(10);
java
CopyEdit
class TreeNode {
int data;
TreeNode left, right;
}
1. Time Complexity
Time complexity is a measure of the amount of time an algorithm takes to complete its
execution as a function of the size of the input. It reflects the number of basic operations or
steps an algorithm performs.
Formal Definition:
Time complexity is the computational complexity that describes the relationship between the
input size (denoted as n) and the number of steps required to execute the algorithm.
1. Count the Basic Operations: Analyze loops, recursion, and other control structures.
2. Focus on Input Size (n): Time complexity is expressed as a function of n.
3. Identify Growth Rate: Use asymptotic notations to describe the rate of growth of the
algorithm's execution time.
O(1) – Constant Time: The algorithm executes in the same amount of time, regardless of
input size.
o Example: Accessing an element in an array.
O(log n) – Logarithmic Time: The algorithm's execution time grows logarithmically with the
input size.
o Example: Binary search.
O(n) – Linear Time: The execution time grows proportionally to the input size.
o Example: Traversing an array.
O(n²) – Quadratic Time: The execution time grows quadratically with the input size.
o Example: Nested loops in algorithms like bubble sort.
O(2ⁿ) – Exponential Time: Execution time doubles with each increment in input size.
o Example: Recursive solutions like solving the Tower of
2. Space Complexity
Space complexity is a measure of the amount of memory an algorithm uses during its
execution. It considers both the input size and any additional memory required for temporary
or auxiliary variables.
Formal Definition:
Space complexity is the computational complexity that describes the relationship between the
input size (denoted as n) and the memory required by the algorithm.
1. Fixed Part: Memory required for constants and variables that do not depend on the input
size.
2. Variable Part: Memory required for dynamically allocated structures, recursion stacks, or
other temporary data structures.
O(1) – Constant Space: The algorithm uses a fixed amount of memory, regardless of input
size.
o Example: Swapping two variables.
O(n) – Linear Space: The memory usage grows linearly with input size.
o Example: Storing an array of size n.
O(n²) – Quadratic Space: Memory usage grows quadratically with input size.
o Example: 2D arrays or matrices.
Stack
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. In a
stack, the last element added is the first one to be removed. It is often compared to a stack of
plates where you can only add or remove plates from the top.
Representation of a Stack:
Applications of Stack:
o Steps:
1. Check if the stack is full (in the case of a fixed-size stack).
2. If not full, increment the top pointer.
3. Add the new element at the top position.
o Time Complexity: O(1)
2. Pop Operation (Deletion):
Removes the top element from the stack.
Steps:
1. Check if the stack is empty.
2. If not empty, remove the element at the top and decrement the top pointer.
Time Complexity: O(1)
3. Peek/Top Operation:
Retrieves the value of the top element without removing it.
Steps:
1. Check if the stack is empty.
2. If not empty, return the value at the top.
Time Complexity: O(1)
4. IsEmpty Operation:
Checks whether the stack is empty.
Steps:
1. If top == -1, return true (stack is empty).
2. Otherwise, return false.
Time Complexity: O(1)
5. IsFull Operation:
Checks whether the stack is full (only applicable to fixed-size stacks).
Steps:
1. If top == maxSize - 1, return true (stack is full).
2. Otherwise, return false.
Time Complexity: O(1)
a. Infix Expression
2. Recursion
Definition of Queue
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. In a
queue, the first element added is the first one to be removed, much like a queue of people
waiting in line.
Features of Queue
1. FIFO Principle: The element that enters the queue first is removed first.
2. Two Ends:
o Front: Points to the element to be removed next.
o Rear: Points to the position where a new element will be added.
3. Applications:
o Process scheduling in operating systems.
o Managing requests in printers.
o Data buffering in communication systems.
Representation of Queue
cpp
CopyEdit
void enqueue(int queue[], int &rear, int maxSize, int value) {
if (rear == maxSize - 1) {
cout << "Queue Overflow!";
return;
}
queue[++rear] = value;
}
3. Peek/Front Operation
cpp
CopyEdit
int peek(int queue[], int front, int rear) {
if (front > rear) {
cout << "Queue is Empty!";
return -1; // Return error code
}
return queue[front];
}
4. IsEmpty Operation
5. IsFull Operation
Applications of Queue
Enqueue operation
The steps of enqueue operation are given below:
o If rear != max - 1, then rear will be incremented to mod(maxsize) and the new value
will be inserted at the rear end of the queue.
o If front != 0 and rear = max - 1, it means that queue is not full, then set the value of
rear to 0 and insert the new element there.
There are two cases in which the element cannot be inserted:
o When front ==0 && rear = max-1, which means that front is at the first position of
the Queue and rear is at the last position of the Queue.
o front== rear + 1;
Algorithm to insert an element in a circular queue
Step1: IF(REAR+1)%MAX=FRONT
Write " OVERFLOW "
Goto step 4
[End OF IF]
Step 4: EXIT
Dequeue Operation
The steps of dequeue operation are given below:
o First, we check whether the Queue is empty or not. If the queue is empty, we cannot
perform the dequeue operation.
o When the element is deleted, the value of front gets decremented by 1.
o If there is only one element left which is to be deleted, then the front and rear are
reset to -1.
Algorithm to delete an element from the circular queue
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
Step 4: EXIT