1. ARRAY IMPLEMENTATION OF STACKS class Stack { static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack 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 StackArray { public static void main(String args[]) { Stack s = new Stack(); s.push(10); s.push(20); s.push(30); System.out.println(" STACK USING ARRAYS \ n \ n "); 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(); } } OUTPUT STACK USING ARRAYS 10 pushed into stack 20 pushed into stack 30 pushed into stack 30 Popped from stack Top element is : 20 Elements present in stack : 20 10 2. QUEUE USING AN ARRAY class Queue { static private int front, rear, capacity; static private int queue[]; Queue(int c) { front = rear = 0; capacity = c; queue = new int[capacity]; } static void queueEnqueue(int data) { if (capacity == rear) { System.out.printf(" \ nQueue is full \ n"); return; } else { queue[rear] = data; rear++; } return; } static void queueDequeue() { if (front == rear) { System.out.printf(" \ nQueue is empty \ n"); return; } else { int element = queue[front]; if (front >= rear) { front = - 1; rear = - 1; } else { front++; } System.out.println(element + " Deleted"); return; } } static void queueDisplay() { int i; if (front == rear) { System.out.printf(" \ nQueue is Empty \ n"); return; } for (i = front; i < rear; i++) { System.out.printf(" %d < -- ", queue[i]," \ n"); } return; } static void queueFront() { if (front == rear) { System.out.printf(" \ nQueue is Empty \ n"); return; } System.out.printf(" \ nFront Element is: %d", queue[front]); return; } } class StaticQueueinjava { public static void main(String[] args) { Queue q = new Queue(4); System.out.printf(" \ n QUEUE USING ARRAYS \ n \ n"); q.queueDisplay(); q.queueEnqueue(20); q.queueEnqueue(30); q.queueEnqueue(40); q.queueEnqueue(50); q.queueDisplay(); q.queueEnqueue(60); q.queueDisplay(); q.queueDequeue(); q.queueDequeue(); System.out.printf(" \ n \ nafter two node deletion \ n \ n"); q.queueDisplay(); q.queueFront(); } } OUTPUT QUEUE USING ARRAYS Queue is Empty 20 < -- 30 < -- 40 < -- 50 < -- Queue is full 20 < -- 30 < -- 40 < -- 50 < -- 20 deleted 30 deleted after two node deletion 40 < -- 50 < -- Front Element is: 40 3. STACK USING SINGLY LINKED LIST import static java.lang .System.exit; class StackLink { public static void main(String[] args) { StackUsingLinkedlist obj= new StackUsingLinkedlist(); obj.push(11); obj.push(22); obj.push(33); obj.push(44); System.out.printf(" STACK USING LINKED LIST "); obj.display(); System.out.printf(" \ nTop element is %d \ n",obj.peek()); obj.pop(); obj.pop(); obj.display(); System.out.printf(" \ nTop element is %d \ n",obj.peek()); } } // Create Stack Using Linked list class StackUsingLinkedlist { private class Node { int data; // integer data Node link; // reference variable Node type } Node top; StackUsingLinkedlist() { this.top = null; } public void push(int x) // insert at the beginning { Node temp = new Node(); if (temp == null) { System.out.print(" \ nHeap Overflow"); return; } temp.data = x; temp.link = top; top = temp; } public boolean isEmpty() { return top == null; } public int peek() { if (!isEmpty()) { return top.data; } else { System.out.println("Stack is empty") ; return - 1; } } public void pop() // remove at the beginning { if (top == null) { System.out.print(" \ nStack Underflow"); return; } System.out.println(" E lement Popped is " + top.data); top = (top ).link; } public void display() { if (top == null) { System.out.printf(" \ nStack Underflow"); exit(1); } else { Node temp = top; while (temp != null) { // print node data System.out.print(temp.data); // assign temp link to temp temp = temp.link; if(temp != null) System.out.print(" - > "); } } } } OUTPUT STACK USING LINKED LIST 44 - > 33 - > 22 - > 11 Top element is 44 Element popped is 44 Element popped is 33 22 - > 11 Top element is 22 4. QUEUE USING LINKED LIST class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class Queue { Node front, rear; int length; public Queue() { this.front = this.rear = null; this.length=0; } void enqueue(int key) { this.length++; Node temp = new Node(key); if (this.rear == null) { this.front = this.rear = temp; return; } this.rear.next = temp; this.rear = temp; } void deque() { if (this.front == null) return; Node temp = this.front; this.front = this.front.next; if (this.front == null) this.rear = null; temp.next = null; } int peek() { if (this.front != null) return this.front.data; return Integer.MIN_VALUE; } int size(){ return this.length; } void printQueue(){ Node temp=this.front; System.out.print("Element of Queue : "); while(temp!=null){ System.out.print(temp.data+" "); temp=temp.next; } System.out.println(); } } public class QueueList { public static void main(String[] args) { Queue q = new Queue(); System.out.println(" \ nQUEUE USING LINKED LIST \ n \ n " ) ; q.enqueue(10); q.enqueue(20); q.printQueue(); System.out.println("Size :" + q.size()); q.deque(); q.deque(); q.enqueue(30); q.enqueue(40); q.enqueue(50); q.printQueue(); System.out.println("Size :" + q.size()); System.out.println("Front item is: " + q.peek()); } } OUTPUT QUEUE USING LINKED LIST Element of Queue : 10 20 Size :2 Element of Queue : 30 40 50 Size :5 Front item is: 30 5. INFIX TO POSTFIX CONVERSION import java.util.Stack; public class InfixToPostfix { // Function to return precedence of operators static int prec(char c) { if (c == '^') return 3; else if (c == '/' || c == '*') return 2; else if (c == '+' || c == ' - ') return 1; else return - 1; } static char associativity(char c) { if (c == '^') return 'R'; return 'L'; // Default to left - associative } static void infixToPostfix(String s) { StringBuilder result = new StringBuilder(); Stack<Character> stack = new Stack<>(); 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 == '(') { stack.push(c); } else if (c == ')') { while (!stack.isEmpty() && stack.peek() != '(') { result.append(stack.pop()); } stack.pop(); // Pop '(' } else { while (!stack.isEmpty() && (prec(s.charAt(i)) < prec(stack.peek()) || prec(s.charAt(i)) == prec(stack.peek()) && associativity(s.charAt(i)) == 'L')) { result.append(stack.pop()); } stack.push(c); } } while (!stack.isEmpty()) { result.append(stack.pop()); } System.out.println(result); } public static void main(String[] args) { String exp = "a+b*(c^d - e)^(f+g*h) - i"; System.out.println(" INFIX TO POSTFIX CONVERSION \ n \ n "); // Function call infixToPostfix(exp); } } OUTPUT INFIX TO POSTFIX CONVERSION abcd^e - fgh*+^*+i - 6. BINARY TREE TRAVERSAL( INORDER,PREORDER,POSTORDER) import java.io.*; class Node { int data; Node left; Node right; Node(int v) { this.data = v; this.left = this.right = null; } } class BTT{ // Inorder Traversal public static void printInorder(Node node) { if (node == null) return; // Traverse left subtree printInorder(node.left); // Visit node System.out.print(node.data + " "); // Traverse right subtree printInorder(node.right); } public static void printPreorder(Node node) { if (node == null) return; // Visit node System.out.print(node.data + " "); // Traverse left subtree printPreorder(node.left); // Traverse right subtree printPreorder(node.right); } public static void printPostorder(Node node) { if (node == null) return; // Traverse left subtree printPostorder(node.left); // Traverse right subtree printPostorder(node.right); // Visit node System.out.print(node.data + " "); } public static void main(String[] args) { // Build the tree Node root = new Node(100); root.left = new Node(20); root.right = new Node(200); root.left.left = new Node(10); root.left.right = new Node(30); root.right.left = new Node(150); root.right.right = new Node(300); // Function call System.out.print("Inorder Traversal: "); printInorder(root); System.out.print(" \ nPreorder Traversal: "); printPreorder(root); System.out.print(" \ nPostorder Traversal: "); printPostorder(root); } } OUTPUT Inorder Traversal: 10 20 30 100 150 200 300 Preorder Traversal: 100 20 10 30 200 150 300 Postorder Traversal: 20 10 30 200 150 300 100 7. LINEAR SEARCH public class LinearSearchExample{ 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 a[]){ int [] a1= {10,20,30,50,70,90}; int key = 50; System.out.println(" LINEAR SEARCH \ n \ n " ); System.out.println(key+" is found at index: "+linearSearch(a1, key)); } } OUTPUT LINEAR SEARCH 50 is found at index: 3 7. BINARY SEARCH class BinarySearch { int binarySearch(int arr[], int l, int r, int x) { while (l <= r) { int mid = (l + r) / 2; if (arr[mid] == x) { return mid; } else if (arr[mid] > x) { r = mid - 1; } e lse { l = mid + 1; } } return - 1; } public static void main(String args[]) { BinarySearch ob = new BinarySearch(); int arr[] = { 2, 3, 4, 10, 40 }; int n = arr.length; int x = 10; int result = ob.binarySearch(arr, 0, n - 1, x); System.out.println(" BINARY SEARCH \ n \ n "); if (result == - 1) System.out.println("Element not present"); else System.out.pr intln("Element found at index " + result); } } OUTPUT BINARY SEARCH Element found at index 3