NOTES
NOTES
LINKEDLIST :
A LinkedList is a linear data structure where elements are stored in nodes with
each node pointing to the next.
TYPES:
1.Singly LinkedList - consist of data and address to the next node ,single
direction ,collection of data and next.
class Node{
int data;
Node next;
Node(int data){
this. data = data;//parameterised constructor.
this.next = null;
}
}
2.Doubly linkedlist - consist previous data and next. two direction traversal is
possible.
3.Circular linked list - we create the loop , at the last element next we store
the head node.
STACK:
Stack is a linear data structure that follows the Lifo principle.
Common operations: push(insert),pop(delete),peek ,isEmpty.
underflow - when the stack is empty and we perform the pop operation
overflow - if isFull is true and we try to push new data then the condition is
termed as overlflow.
class Stack{
private int maxSize;
private int[] stackArray;
private int top;
public satck(int size){
this.maxSize = size;
this.stackArray = new int[maxSize];
this.top = -1;
}
}
PUSH OPERATION:
adds an element to the top of the stack.
increases the top pointer.
public void push(int value){
if(top == maxsize -1){
System.out.println("stack is empty");
return -1;
} else
[
return stackArray[++top] = value;
}
}
TASK :
1. Practice stack problem.
QUEUE:
A queue is a linear data structure that follows the fifo principle.
common operation: enqueue,dequeue,front rear, isEmpty.
Use cases: Printer queue management ,cpu task scheduling, bfs in graphs.
TYPES OF QUEUE:
Simple queue. - linear queue ,follows the fifo, enqueue, dequeue,
front,rear,isEmpty,isFull.
circular queue - front and rear pointers wrap around when they reach the end of the
array.
overcome the limtations of unused space in a simple queue.
enqueue, dequeue, front,rear, isempty,isfull.
priority queue - elements are added in the priority.
An avl tree is a self balaancing binary search tree where the difference in height
etween the left and right subtree of any is atmost one.
balancing the tree and amke it efficient.
balancing ensures that the tree remains balanced and maintains effiecient
operations.
class TreeNode{
int val;
TreeNode left;
Treenode right;
int height;
TreeNode(int x) {
val = x;
height = 1;
}
}
balancing factor : height of left subtree - height of right subtree.
range of balance factor = 0,1,-1.
disadvantages: hard to implement.
takes more processing time for balancing.
left rotation:
DIVIDE AND CONQUER:
CASE : 1 if arr[mid] == key
return mid+1;
case : 2 i f arr[mid] >key
if arr[mid] <key
return = mid+1;
adjacency list:
ArrayLidt<edge> graph[v].
Dynamic programmimng:
is a method for solving complws problems by breaking them doen into simpler
subproblems
it involves storing the results of the subproblems to avoid redundant calculations.
0/1 knapsack: