import java.util.*;
class Main {
public static void main(String args[]) {
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
System.out.println("Top element is :" + s.peek());
System.out.print("Elements present in stack: ");
s.print();
}
}
class Stack {
static final int MAX = 1000;
int top;
int a[] = new int[MAX];
boolean isEmpty() {
return (top < 0);
}
Stack() {
top = -1;
}
boolean push(int x) {
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
} else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
} else {
int x = a[top--];
return x;
}
}
int peek() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
} else {
int x = a[top];
return x;
}
}
void print() {
for (int i = top; i > -1; i--) {
System.out.print(" " + a[i]);
}
}
}
class queuearray {
public static void main(String[] args) {
Queue q = new Queue(4);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println(q.getFront());
q.dequeue();
q.enqueue(4);
q.display();
}
}
class Queue {
int[] arr;
int size;
int capacity;
int front;
Queue(int qSize) {
size = 0;
front = 0;
capacity = qSize;
arr = new int[qSize];
}
void enqueue(int x) {
if (size == capacity) {
return;
}
arr[size] = x;
size++;
}
void dequeue() {
if (size == 0) {
return;
}
for (int i = 1; i < size; i++) {
arr[i - 1] = arr[i];
}
size--;
}
int getFront() {
if (size == 0) return -1;
return arr[front];
}
void display() {
for (int i = front; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
class linkedliststock {
public static void main(String[] args) {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(10);
stack.push(20);
stack.push(30);
stack.displayStack();
System.out.println("\n" + stack.pop() + " popped from Stack");
System.out.println("Top element is: " + stack.peek());
}
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class StackUsingLinkedList {
private Node top;
public StackUsingLinkedList() {
this.top = null;
}
public boolean isEmpty() {
return top == null;
}
public void push(int data) {
Node newNode = new Node(data);
if (top == null) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
} else {
int popped = top.data;
top = top.next;
return popped;
}
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
} else {
return top.data;
}
}
public void displayStack() {
Node currentNode = top;
System.out.println("Stack Elements: ");
while (currentNode != null) {
System.out.println(currentNode.data + " ");
currentNode = currentNode.next;
}
}
}
public class linkedlistqueue {
public static void main(String[] args) {
Queue queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println("Dequeued:" + queue.dequeue());
System.out.println("Dequeued:" + queue.dequeue());
System.out.println("peek:" + queue.peek());
System.out.println("Dequeued:" + queue.dequeue());
}
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Queue {
private Node front;
private Node rear;
public Queue() {
this.front = null;
this.rear = null;
}
public void enqueue(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public int dequeue() {
if (front == null) {
throw new IllegalStateException("Queue is empty");
}
int data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return data;
}
public boolean isEmpty() {
return front == null;
}
public int peek() {
if (front == null) {
throw new IllegalStateException("Queue is empty");
}
return front.data;
}
}
import java.util.Stack;
class GfG {
int prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(String s) {
Stack<Character> st = new Stack<>();
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result.append(c);
} else if (c == '(') {
st.push('(');
} else if (c == ')') {
while (st.peek() != '(') {
result.append(st.pop());
}
st.pop();
} else {
while (!st.isEmpty() && (prec(c) < prec(st.peek()) || prec(c) == prec(st.peek()))) {
result.append(st.pop());
}
st.push(c);
}
}
while (!st.isEmpty()) {
result.append(st.pop());
}
System.out.println("PostFix expression: " + result.toString());
}
public static void main(String[] args) {
GfG gfg = new GfG();
String exp = "L+M+N+O";
System.out.println("Infix Expression: " + exp);
gfg.infixToPostfix(exp);
}
}
public class BinaryTreeTraversal {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
System.out.println("Inorder traversal: ");
tree.inOrder(tree.root);
System.out.println("\nPreorder traversal: ");
tree.preOrder(tree.root);
System.out.println("\nPostorder traversal:");
tree.postOrder(tree.root);
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
Node root;
public BinaryTree() {
this.root = null;
}
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.print(node.data + " ");
inOrder(node.right);
}
}
public void preOrder(Node node) {
if (node != null) {
System.out.print(node.data + " ");
preOrder(node.left);
preOrder(node.right);
}
}
public void postOrder(Node node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
System.out.print(node.data + " ");
}
}
}
public class LinearSearch1 {
public static int linearSearch(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int key = 30;
int result = linearSearch(arr, key);
if (result == -1) {
System.out.println("Element not found in the array:");
} else {
System.out.println("Element found at index :" + result);
}
}
}
public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50, 60, 70, 80};
int target = 40;
int result = binarySearch(array, target);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
import java.util.Arrays;
public class Main {
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
System.out.println("Before Sorting: " + Arrays.toString(arr));
insertionSort(arr);
System.out.println("After Sorting: " + Arrays.toString(arr));
}
}
import java.util.Arrays;
public class Main {
public static void quickSort(int[] arr, int low, int high) {
if (low >= high) return;
int pivot = arr[high], i = low;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
arr[high] = arr[i];
arr[i] = pivot;
quickSort(arr, low, i - 1);
quickSort(arr, i + 1, high);
}
public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
System.out.println("Before Sorting: " + Arrays.toString(arr));
quickSort(arr, 0, arr.length - 1);
System.out.println("After Sorting: " + Arrays.toString(arr));
}
}
import java.util.Arrays;
public class Main {
public static void mergeSort(int[] arr) {
if (arr.length < 2) return;
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.length);
mergeSort(left);
mergeSort(right);
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length)
arr[k++] = (left[i] < right[j]) ? left[i++] : right[j++];
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
}
public static void main(String[] args) {
int[] arr = {15, 3, 28, 6, 12};
System.out.println("Before Sorting: " + Arrays.toString(arr));
mergeSort(arr);
System.out.println("After Sorting: " + Arrays.toString(arr));
}
}
import java.util.*;
class GraphIterative {
private int V;
private LinkedList<Integer>[] adj;
@SuppressWarnings("unchecked")
public GraphIterative(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList<>();
}
}
void addEdge(int v, int w) {
adj[v].add(w);
}
void DFS(int start) {
boolean[] visited = new boolean[V];
Stack<Integer> stack = new Stack<>();
stack.push(start);
while (!stack.isEmpty()) {
int v = stack.pop();
if (!visited[v]) {
System.out.print(v + " ");
visited[v] = true;
}
for (int neighbor : adj[v]) {
if (!visited[neighbor]) {
stack.push(neighbor);
}
}
}
}
public static void main(String[] args) {
GraphIterative g = new GraphIterative(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
System.out.println("Depth-First Search using Stack:");
g.DFS(0);
}
}
import java.util.*;
class Graph {
private int V;
private LinkedList<Integer>[] adj;
@SuppressWarnings("unchecked")
public Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList<>();
}
}
void addEdge(int v, int w) {
adj[v].add(w);
}
void BFS(int start) {
boolean[] visited = new boolean[V];
Queue<Integer> queue = new LinkedList<>();
visited[start] = true;
queue.add(start);
System.out.print("BFS Traversal: ");
while (!queue.isEmpty()) {
int v = queue.poll();
System.out.print(v + " ");
for (int neighbor : adj[v]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
}
}
}
System.out.println();
}
public static void main(String[] args) {
Graph g = new Graph(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
g.BFS(0);
}
}
import java.util.*;
class Graph {
private int V;
private LinkedList<Edge>[] adj;
static class Edge {
int dest, weight;
Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}
}
@SuppressWarnings("unchecked")
public Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList<>();
}
}
void addEdge(int src, int dest, int weight) {
adj[src].add(new Edge(dest, weight));
adj[dest].add(new Edge(src, weight));
}
void dijkstra(int src) {
int[] dist = new int[V];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;
PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
pq.add(new Edge(src, 0));
while (!pq.isEmpty()) {
Edge current = pq.poll();
int u = current.dest;
for (Edge neighbor : adj[u]) {
int v = neighbor.dest;
int weight = neighbor.weight;
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.add(new Edge(v, dist[v]));
}
}
}
System.out.println("Vertex \t Distance from Source " + src);
for (int i = 0; i < V; i++) {
System.out.println(i + "\t" + dist[i]);
}
}
public static void main(String[] args) {
Graph g = new Graph(6);
g.addEdge(0, 1, 4);
g.addEdge(0, 2, 2);
g.addEdge(1, 2, 5);
g.addEdge(1, 3, 10);
g.addEdge(2, 4, 3);
g.addEdge(3, 5, 6);
g.addEdge(4, 5, 8);
g.dijkstra(0);
}
}