├── README.md ├── Tree ├── README.md ├── binarySearchTree │ ├── BinarySearchTree.java │ ├── Demo.java │ ├── Demo1.java │ └── Node.java ├── binaryTree │ ├── BinaryTree.java │ ├── Demo.java │ ├── Node.java │ └── QueueA.java └── heap │ ├── Demo.java │ ├── Demo1.java │ └── Heap.java ├── hashing ├── OpenAddressing │ ├── Demo.java │ ├── HashTable.java │ └── studentRecord.java ├── OpenAddressing1 │ ├── Demo.java │ ├── HashTable.java │ └── studentRecord.java ├── README.md └── SeparateChaining │ ├── Demo.java │ ├── HashTable.java │ ├── Node.java │ ├── SingleLinkedList.java │ └── studentRecord.java ├── linked-list ├── HeaderList │ ├── Demo.java │ ├── HeaderList.java │ └── Node.java ├── README.md ├── circularLinkedList │ ├── CircularLinkedList.java │ ├── ConcatenateDemo.java │ ├── Demo.java │ └── Node.java ├── doubleLinkedList │ ├── DoubleLinkedList.java │ ├── Node.java │ └── demo.java ├── singleLinkedList │ ├── ConcatenateDemo.java │ ├── Demo.java │ ├── MergingDemo.java │ ├── Node.java │ └── SingleLinkedList.java └── sortedLinkedList │ ├── Demo.java │ ├── Node.java │ └── SortedLinkedList.java ├── queue ├── README.md ├── circularQueue │ ├── CircularQueue.java │ └── Demo.java ├── deque │ ├── Demo.java │ └── DequeA.java ├── priorityQueue │ ├── Demo.java │ ├── Node.java │ └── PriorityQueueL.java ├── queueArray │ ├── Demo.java │ └── QueueA.java ├── queueCircularLinked │ ├── Demo.java │ ├── Node.java │ └── QueueCL.java └── queueLinked │ ├── Demo.java │ ├── Node.java │ └── QueueL.java ├── recursion ├── BaseConversion.java ├── Display1toN.java ├── Euclids.java ├── Exponentiation.java ├── Factorial.java ├── Fibonacci.java ├── README.md ├── SumOfDigits.java └── TowerOfHanoi.java ├── searching ├── BinarySearch.java ├── BinarySearchRecursive.java ├── LinearSearch.java ├── LinearSearchSortedArray.java ├── LinearSearchWithSentinel.java └── README.md ├── sorting ├── BubbleSort.java ├── HeapSort.java ├── InsertionSort.java ├── MergeSortIterative.java ├── MergeSortRecursive.java ├── Merging.java ├── Merging1.java ├── QuickSort.java ├── README.md ├── SelectionSort.java ├── ShellSort.java ├── address-calculation-sort │ ├── AddressCalcSort.java │ └── SortedLinkedList.java ├── bianry-tree-sort │ ├── BinarySearchTree.java │ ├── BinaryTreeSort.java │ └── TreeNode.java └── radix-sort │ ├── Node.java │ └── RadixSort.java └── stack ├── README.md ├── parentheses ├── Demo.java └── StackA.java ├── postfix ├── Demo.java ├── StackChar.java └── StackInt.java ├── stackArray ├── Demo.java └── StackA.java └── stackLinked ├── Demo.java ├── Node.java └── StackL.java /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Java ( DSA ) 2 | 3 | This [Data Structures and Algorithms In Java (DSA)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Instructor - Deepali Srivastava, Author of [Ultimate Python Programming](https://www.amazon.in/Ultimate-Python-Programming-programs-questions/dp/935551655X) 7 | * Thoroughly detailed course with complete working programs 8 | * Contains lots of animations to help you visualize the concepts 9 | * Includes over 100 Quiz questions 10 | * Builds a solid foundation in Data Structures and Algorithms 11 | * Prepares you for coding interviews 12 | * Lifetime Access 13 | 14 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 15 | 16 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 19 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 20 | 21 | 29 | 30 | ## Copyright 31 | © Copyright Deepali Srivastava : All rights reserved. 32 | Not to be used for commercial purposes. 33 | -------------------------------------------------------------------------------- /Tree/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Java 2 | 3 | This [“Data Structures and Algorithms in Java”](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 16 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 19 | 20 | 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /Tree/binarySearchTree/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binarySearchTree; 7 | 8 | import java.lang.NullPointerException; 9 | 10 | public class BinarySearchTree 11 | { 12 | private Node root; 13 | 14 | public BinarySearchTree() 15 | { 16 | root = null; 17 | } 18 | 19 | public boolean isEmpty() 20 | { 21 | return (root==null); 22 | } 23 | public void insert(int x) 24 | { 25 | root = insert(root,x); 26 | } 27 | 28 | private Node insert(Node p, int x) 29 | { 30 | if(p==null) 31 | p=new Node(x); 32 | else if(x < p.info) 33 | p.lchild = insert(p.lchild,x); 34 | else if(x > p.info) 35 | p.rchild = insert(p.rchild,x); 36 | else 37 | System.out.println(x + " already present in tree"); 38 | return p; 39 | } 40 | 41 | public void insert1(int x) 42 | { 43 | Node p=root; 44 | Node par=null; 45 | 46 | while(p!=null) 47 | { 48 | par=p; 49 | if(x < p.info) 50 | p=p.lchild; 51 | else if(x > p.info) 52 | p=p.rchild; 53 | else 54 | { 55 | System.out.println(x + " already present in the tree"); 56 | return; 57 | } 58 | } 59 | 60 | Node temp=new Node(x); 61 | 62 | if(par==null) 63 | root=temp; 64 | else if(x < par.info) 65 | par.lchild=temp; 66 | else 67 | par.rchild=temp; 68 | } 69 | 70 | public boolean search(int x) 71 | { 72 | return(search(root,x)!=null); 73 | } 74 | private Node search(Node p, int x) 75 | { 76 | if(p==null) 77 | return null; /*key not found*/ 78 | if(x < p.info)/*search in left subtree*/ 79 | return search(p.lchild, x); 80 | if(x > p.info)/*search in right subtree*/ 81 | return search(p.rchild, x); 82 | return p; /*key found*/ 83 | } 84 | 85 | public boolean search1(int x) 86 | { 87 | Node p = root; 88 | while(p!=null) 89 | { 90 | if(x < p.info) 91 | p=p.lchild; /*Move to left child*/ 92 | else if(x > p.info) 93 | p=p.rchild; /*Move to right child */ 94 | else /*x found*/ 95 | return true; 96 | } 97 | return false; 98 | } 99 | 100 | public void delete(int x) 101 | { 102 | root = delete(root,x); 103 | } 104 | 105 | private Node delete(Node p, int x) 106 | { 107 | Node ch,s; 108 | 109 | if(p==null) 110 | { 111 | System.out.println(x + " not found"); 112 | return p; 113 | } 114 | if(x < p.info) /*delete from left subtree*/ 115 | p.lchild = delete(p.lchild, x); 116 | else if(x > p.info) /*delete from right subtree*/ 117 | p.rchild = delete(p.rchild, x); 118 | else 119 | { 120 | /*key to be deleted is found*/ 121 | if( p.lchild!=null && p.rchild!=null ) /*2 children*/ 122 | { 123 | s=p.rchild; 124 | while(s.lchild!=null) 125 | s=s.lchild; 126 | p.info=s.info; 127 | p.rchild = delete(p.rchild,s.info); 128 | } 129 | else /*1 child or no child*/ 130 | { 131 | if(p.lchild != null) /*only left child*/ 132 | ch=p.lchild; 133 | else /*only right child or no child*/ 134 | ch=p.rchild; 135 | p=ch; 136 | } 137 | } 138 | return p; 139 | } 140 | 141 | public void delete1(int x) 142 | { 143 | Node p=root; 144 | Node par=null; 145 | while(p!=null) 146 | { 147 | if(x==p.info) 148 | break; 149 | par=p; 150 | if(x < p.info) 151 | p=p.lchild; 152 | else 153 | p=p.rchild; 154 | } 155 | 156 | if(p==null) 157 | { 158 | System.out.println(x + " not found"); 159 | return; 160 | } 161 | 162 | /*Case C: 2 children*/ 163 | /*Find inorder successor and its parent*/ 164 | Node s,ps; 165 | if(p.lchild!=null && p.rchild!=null) 166 | { 167 | ps=p; 168 | s=p.rchild; 169 | while(s.lchild!=null) 170 | { 171 | ps=s; 172 | s=s.lchild; 173 | } 174 | p.info=s.info; 175 | p=s; 176 | par=ps; 177 | } 178 | 179 | /*Case B and Case A : 1 or no child*/ 180 | Node ch; 181 | if(p.lchild!=null) /*node to be deleted has left child */ 182 | ch=p.lchild; 183 | else /*node to be deleted has right child or no child*/ 184 | ch=p.rchild; 185 | 186 | if(par==null) /*node to be deleted is root node*/ 187 | root=ch; 188 | else if(p==par.lchild)/*node is left child of its parent*/ 189 | par.lchild=ch; 190 | else /*node is right child of its parent*/ 191 | par.rchild=ch; 192 | } 193 | 194 | public int min() 195 | { 196 | if(isEmpty()) 197 | throw new NullPointerException("Tree is empty"); 198 | return min(root).info; 199 | } 200 | 201 | private Node min(Node p) 202 | { 203 | if(p.lchild==null) 204 | return p; 205 | return min(p.lchild); 206 | } 207 | 208 | public int max() 209 | { 210 | if(isEmpty()) 211 | throw new NullPointerException("Tree is empty"); 212 | return max(root).info; 213 | } 214 | 215 | private Node max(Node p) 216 | { 217 | if(p.rchild==null) 218 | return p; 219 | return max(p.rchild); 220 | } 221 | 222 | public int min1() 223 | { 224 | if(isEmpty()) 225 | throw new NullPointerException("Tree is empty"); 226 | Node p=root; 227 | while(p.lchild!=null) 228 | p=p.lchild; 229 | return p.info; 230 | } 231 | 232 | public int max1() 233 | { 234 | if(isEmpty()) 235 | throw new NullPointerException("Tree is empty"); 236 | Node p=root; 237 | while(p.rchild!=null) 238 | p=p.rchild; 239 | return p.info; 240 | } 241 | 242 | public void display() 243 | { 244 | display(root,0); 245 | System.out.println(); 246 | } 247 | private void display(Node p,int level) 248 | { 249 | int i; 250 | if(p==null) 251 | return; 252 | 253 | display(p.rchild, level+1); 254 | System.out.println(); 255 | 256 | for(i=0; i hR) 324 | return 1+hL; 325 | else 326 | return 1+hR; 327 | }/*End of height()*/ 328 | } 329 | -------------------------------------------------------------------------------- /Tree/binarySearchTree/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binarySearchTree; 7 | import java.util.Scanner; 8 | public class Demo 9 | { 10 | public static void main(String[] args) 11 | { 12 | BinarySearchTree bt = new BinarySearchTree(); 13 | int choice,x; 14 | 15 | Scanner scan = new Scanner(System.in); 16 | 17 | while(true) 18 | { 19 | System.out.println("1.Display Tree"); 20 | System.out.println("2.Search"); 21 | System.out.println("3.Insert a new node"); 22 | System.out.println("4.Delete a node"); 23 | System.out.println("5.Preorder Traversal"); 24 | System.out.println("6.Inorder Traversal"); 25 | System.out.println("7.Postorder Traversal"); 26 | System.out.println("8.Height of tree"); 27 | System.out.println("9.Find Minimum key"); 28 | System.out.println("10.Find Maximum key"); 29 | System.out.println("11.Quit"); 30 | System.out.println("Enter your choice : "); 31 | choice = scan.nextInt(); 32 | 33 | if(choice==11) 34 | break; 35 | 36 | switch(choice) 37 | { 38 | case 1: 39 | bt.display(); 40 | break; 41 | case 2: 42 | System.out.print("Enter the key to be searched : "); 43 | x = scan.nextInt(); 44 | 45 | if(bt.search(x)) 46 | System.out.println("Key found"); 47 | else 48 | System.out.println("Key not found"); 49 | break; 50 | case 3: 51 | System.out.println("Enter the key to be inserted : "); 52 | x = scan.nextInt(); 53 | bt.insert(x); 54 | break; 55 | case 4: 56 | System.out.println("Enter the key to be deleted : "); 57 | x = scan.nextInt(); 58 | bt.delete(x); 59 | break; 60 | case 5: 61 | bt.preorder(); 62 | break; 63 | case 6: 64 | bt.inorder(); 65 | break; 66 | case 7: 67 | bt.postorder(); 68 | break; 69 | case 8: 70 | System.out.println("Height of tree is " + bt.height()); 71 | break; 72 | case 9: 73 | System.out.println("Minimum key is " + bt.min()); 74 | break; 75 | case 10: 76 | System.out.println("Maximum key is " + bt.max()); 77 | break; 78 | }/*End of switch */ 79 | }/*End of while */ 80 | scan.close(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Tree/binarySearchTree/Demo1.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binarySearchTree; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo1 11 | { 12 | public static void main(String[] args) 13 | { 14 | BinarySearchTree bt = new BinarySearchTree(); 15 | int choice,x; 16 | 17 | Scanner scan = new Scanner(System.in); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Display Tree"); 22 | System.out.println("2.Search"); 23 | System.out.println("3.Insert a new node"); 24 | System.out.println("4.Delete a node"); 25 | System.out.println("5.Preorder Traversal"); 26 | System.out.println("6.Inorder Traversal"); 27 | System.out.println("7.Postorder Traversal"); 28 | System.out.println("8.Height of tree"); 29 | System.out.println("9.Find Minimum key"); 30 | System.out.println("10.Find Maximum key"); 31 | System.out.println("11.Quit"); 32 | System.out.println("Enter your choice : "); 33 | choice = scan.nextInt(); 34 | 35 | if(choice==11) 36 | break; 37 | 38 | switch(choice) 39 | { 40 | case 1: 41 | bt.display(); 42 | break; 43 | case 2: 44 | System.out.print("Enter the key to be searched : "); 45 | x = scan.nextInt(); 46 | 47 | if(bt.search1(x)) 48 | System.out.println("Key found"); 49 | else 50 | System.out.println("Key not found"); 51 | break; 52 | case 3: 53 | System.out.println("Enter the key to be inserted : "); 54 | x = scan.nextInt(); 55 | bt.insert1(x); 56 | break; 57 | case 4: 58 | System.out.println("Enter the key to be deleted : "); 59 | x = scan.nextInt(); 60 | bt.delete1(x); 61 | break; 62 | case 5: 63 | bt.preorder(); 64 | break; 65 | case 6: 66 | bt.inorder(); 67 | break; 68 | case 7: 69 | bt.postorder(); 70 | break; 71 | case 8: 72 | System.out.println("Height of tree is " + bt.height()); 73 | break; 74 | case 9: 75 | System.out.println("Minimum key is " + bt.min1()); 76 | break; 77 | case 10: 78 | System.out.println("Maximum key is " + bt.max1()); 79 | break; 80 | }/*End of switch */ 81 | }/*End of while */ 82 | scan.close(); 83 | 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Tree/binarySearchTree/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binarySearchTree; 7 | 8 | public class Node 9 | { 10 | Node lchild; 11 | int info; 12 | Node rchild; 13 | 14 | public Node(int i) 15 | { 16 | info = i; 17 | lchild=null; 18 | rchild=null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Tree/binaryTree/BinaryTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binaryTree; 7 | 8 | public class BinaryTree 9 | { 10 | private Node root; 11 | 12 | public BinaryTree() 13 | { 14 | root=null; 15 | } 16 | 17 | public void display() 18 | { 19 | display(root,0); 20 | System.out.println(); 21 | } 22 | 23 | private void display(Node p,int level) 24 | { 25 | int i; 26 | if(p==null) 27 | return; 28 | 29 | display(p.rchild, level+1); 30 | System.out.println(); 31 | 32 | for(i=0; i hR) 122 | return 1+hL; 123 | else 124 | return 1+hR; 125 | } 126 | 127 | public void createTree() 128 | { 129 | root = new Node('P'); 130 | root.lchild = new Node('Q'); 131 | root.rchild = new Node('R'); 132 | root.lchild.lchild = new Node('A'); 133 | root.lchild.rchild = new Node('B'); 134 | root.rchild.lchild = new Node('X'); 135 | } 136 | } -------------------------------------------------------------------------------- /Tree/binaryTree/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binaryTree; 7 | 8 | public class Demo 9 | { 10 | public static void main(String[] args) 11 | { 12 | BinaryTree bt = new BinaryTree(); 13 | 14 | bt.createTree(); 15 | 16 | bt.display(); 17 | System.out.println(); 18 | 19 | System.out.println("Preorder : "); 20 | bt.preorder(); 21 | System.out.println(""); 22 | 23 | System.out.println("Inorder : "); 24 | bt.inorder(); 25 | System.out.println(); 26 | 27 | System.out.println("Postorder : "); 28 | bt.postorder(); 29 | System.out.println(); 30 | 31 | System.out.println("Level order : "); 32 | bt.levelOrder(); 33 | System.out.println(); 34 | 35 | System.out.println("Height of tree is " + bt.height()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Tree/binaryTree/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binaryTree; 7 | 8 | public class Node 9 | { 10 | Node lchild; 11 | char info; 12 | Node rchild; 13 | 14 | public Node(char ch) 15 | { 16 | info=ch; 17 | lchild=null; 18 | rchild=null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Tree/binaryTree/QueueA.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package binaryTree; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | public class QueueA 11 | { 12 | private Node[] queueArray; 13 | private int front; 14 | private int rear; 15 | 16 | public QueueA() 17 | { 18 | queueArray = new Node[10]; 19 | front = -1; 20 | rear = -1; 21 | } 22 | public QueueA(int maxSize) 23 | { 24 | queueArray = new Node[maxSize]; 25 | front = -1; 26 | rear = -1; 27 | } 28 | public boolean isEmpty() 29 | { 30 | return (front==-1 || front==rear+1); 31 | } 32 | 33 | public boolean isFull() 34 | { 35 | return (rear == queueArray.length - 1 ); 36 | } 37 | 38 | public int size() 39 | { 40 | if(isEmpty()) 41 | return 0; 42 | else 43 | return rear-front+1; 44 | } 45 | 46 | public void insert(Node x) 47 | { 48 | if(isFull()) 49 | { 50 | System.out.println("Queue Overflow\n"); 51 | return; 52 | } 53 | if(front==-1) 54 | front=0; 55 | rear=rear+1; 56 | queueArray[rear]=x; 57 | } 58 | 59 | public Node Delete() 60 | { 61 | Node x; 62 | if(isEmpty()) 63 | { 64 | System.out.println("Queue Underflow"); 65 | throw new NoSuchElementException(); 66 | } 67 | x=queueArray[front]; 68 | front=front+1; 69 | return x; 70 | } 71 | 72 | public Node peek() 73 | { 74 | if(isEmpty()) 75 | { 76 | System.out.println("Queue Underflow\n"); 77 | throw new NoSuchElementException(); 78 | } 79 | return queueArray[front]; 80 | } 81 | 82 | public void display() 83 | { 84 | int i; 85 | 86 | if(isEmpty()) 87 | { 88 | System.out.println("Queue is empty\n"); 89 | return; 90 | } 91 | 92 | System.out.println("Queue is :\n\n"); 93 | for(i=front; i<=rear; i++) 94 | System.out.print(queueArray[i] + " "); 95 | 96 | System.out.println(); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /Tree/heap/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package heap; 7 | 8 | import java.util.Scanner; 9 | 10 | 11 | public class Demo 12 | { 13 | public static void main(String[] args) 14 | { 15 | Scanner scan = new Scanner(System.in); 16 | 17 | Heap h = new Heap(20); 18 | 19 | int choice,value; 20 | 21 | while(true) 22 | { 23 | System.out.println("1.Insert"); 24 | System.out.println("2.Delete root"); 25 | System.out.println("3.Display"); 26 | System.out.println("4.Exit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | 30 | if(choice==4) 31 | break; 32 | 33 | switch(choice) 34 | { 35 | case 1: 36 | System.out.print("Enter the value to be inserted : "); 37 | value = scan.nextInt(); 38 | h.insert(value); 39 | break; 40 | case 2: 41 | System.out.println("Maximum value is " + h.deleteRoot()); 42 | break; 43 | case 3: 44 | h.display(); 45 | break; 46 | default: 47 | System.out.println("Wrong choice"); 48 | } 49 | } 50 | scan.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Tree/heap/Demo1.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package heap; 7 | 8 | public class Demo1 9 | { 10 | public static void buildHeap_TopDown(int[] a, int n) 11 | { 12 | for(int i=2; i<=n; i++) 13 | restoreUp(i,a); 14 | } 15 | 16 | public static void buildHeap_BottomUp(int[] a, int n) 17 | { 18 | for(int i=n/2; i>=1; i--) 19 | restoreDown(i,a,n); 20 | } 21 | 22 | private static void restoreUp(int i, int[] a) 23 | { 24 | int k=a[i]; 25 | int iparent=i/2; 26 | 27 | while(a[iparent]=1 && a[iparent]=a[lchild] && k>=a[rchild] ) 44 | { 45 | a[i]=k; 46 | return; 47 | } 48 | else if(a[lchild] > a[rchild]) 49 | { 50 | a[i]=a[lchild]; 51 | i=lchild; 52 | } 53 | else 54 | { 55 | a[i]=a[rchild]; 56 | i=rchild; 57 | } 58 | lchild=2*i; 59 | rchild=lchild+1; 60 | } 61 | 62 | /*If number of nodes is even*/ 63 | if(lchild==n && k=1 && a[iparent]=a[lchild] && k>=a[rchild] ) 68 | { 69 | a[i]=k; 70 | return; 71 | } 72 | else if(a[lchild] > a[rchild]) 73 | { 74 | a[i]=a[lchild]; 75 | i=lchild; 76 | } 77 | else 78 | { 79 | a[i]=a[rchild]; 80 | i=rchild; 81 | } 82 | lchild=2*i; 83 | rchild=lchild+1; 84 | } 85 | 86 | /*If number of nodes is even*/ 87 | if(lchild==n && k " ); 76 | 77 | if(array[i]!=null && array[i].getstudentId()!=-1) 78 | System.out.println( array[i] ); 79 | else 80 | System.out.println("___"); 81 | } 82 | } 83 | 84 | 85 | public studentRecord Delete(int key) 86 | { 87 | int h = hash(key); 88 | int location=h; 89 | 90 | for(int i=1; i " ); 31 | 32 | if(array[i]!=null && array[i].getstudentId()!=-1) 33 | System.out.println( array[i] ); 34 | else 35 | System.out.println("___"); 36 | } 37 | } 38 | 39 | public studentRecord Search(int key) 40 | { 41 | int h = hash(key); 42 | int location=h; 43 | 44 | for(int i=1; i= m/2) 58 | { 59 | rehash( nextPrime(2*m) ); 60 | System.out.println( "New Table Size is : " + m ); 61 | } 62 | Insert(newRecord); 63 | } 64 | 65 | public void Insert(studentRecord newRecord) 66 | { 67 | 68 | int key = newRecord.getstudentId(); 69 | int h = hash(key); 70 | 71 | int location = h; 72 | 73 | for( int i=1; i 0 && n <= m/8) 101 | { 102 | rehash(nextPrime(m/2)); 103 | System.out.println( "New Table Size is : " + m ); 104 | } 105 | } 106 | location = (h+i)%m; 107 | } 108 | } 109 | 110 | int hash(int key) 111 | { 112 | return(key%m); 113 | } 114 | 115 | private void rehash(int newSize) 116 | { 117 | HashTable temp = new HashTable(newSize); 118 | 119 | for (int i = 0; i < m; i++) 120 | if (array[i] != null && array[i].getstudentId()!=-1) 121 | temp.Insert(array[i]); 122 | 123 | array = temp.array; 124 | m = newSize; 125 | } 126 | 127 | int nextPrime(int x) 128 | { 129 | while( !isPrime(x) ) 130 | x++; 131 | return x; 132 | } 133 | boolean isPrime(int x) 134 | { 135 | for(int i=2; i 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package SeparateChaining; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int id,choice; 15 | String name; 16 | 17 | Scanner scan = new Scanner(System.in); 18 | 19 | System.out.print("Enter initial size of table : "); 20 | int size = scan.nextInt(); 21 | 22 | HashTable table = new HashTable(size); 23 | 24 | while(true) 25 | { 26 | System.out.println("1.Insert a record"); 27 | System.out.println("2.Search a record"); 28 | System.out.println("3.Delete a record"); 29 | System.out.println("4.Display table"); 30 | System.out.println("5.Exit"); 31 | 32 | System.out.print("Enter your choice : "); 33 | choice = scan.nextInt(); 34 | 35 | if (choice == 5) 36 | break; 37 | 38 | switch(choice) 39 | { 40 | case 1 : 41 | System.out.print("Enter student id : "); 42 | id = scan.nextInt(); 43 | System.out.print("Enter student name : "); 44 | name = scan.next(); 45 | 46 | studentRecord aRecord = new studentRecord(id,name); 47 | 48 | table.Insert(aRecord); 49 | break; 50 | case 2 : 51 | System.out.print("Enter a key to be searched : "); 52 | id = scan.nextInt(); 53 | aRecord = table.Search(id); 54 | 55 | if( aRecord==null ) 56 | System.out.println("Key not found"); 57 | else 58 | System.out.println(aRecord); 59 | 60 | break; 61 | case 3: 62 | System.out.print("Enter a key to be deleted : "); 63 | id = scan.nextInt(); 64 | table.Delete(id); 65 | break; 66 | case 4: 67 | table.DisplayTable(); 68 | break; 69 | } 70 | } 71 | scan.close(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/HashTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package SeparateChaining; 7 | 8 | public class HashTable 9 | { 10 | private SingleLinkedList[] array; 11 | private int m; //size of the array 12 | private int n; //number of records 13 | 14 | public HashTable() 15 | { 16 | this(11); 17 | } 18 | 19 | public HashTable(int tableSize) 20 | { 21 | m = tableSize; 22 | array = new SingleLinkedList[tableSize]; 23 | } 24 | 25 | int hash(int key) 26 | { 27 | return(key%m); 28 | } 29 | 30 | public void DisplayTable() 31 | { 32 | System.out.println("Number of records = " + n ); 33 | for(int i=0; i " ); 36 | 37 | if(array[i]!= null) 38 | array[i].displayList(); 39 | else 40 | System.out.println("___"); 41 | } 42 | } 43 | 44 | public studentRecord Search(int key) 45 | { 46 | int h = hash(key); 47 | 48 | if( array[h]!= null) 49 | return array[h].search(key); 50 | 51 | return null; 52 | } 53 | 54 | public void Insert(studentRecord newRecord) 55 | { 56 | int key = newRecord.getstudentId(); 57 | int h = hash(key); 58 | 59 | if(array[h]==null) 60 | array[h] = new SingleLinkedList(); 61 | 62 | array[h].insertInBeginning(newRecord); 63 | n++; 64 | } 65 | 66 | public void Delete(int key) 67 | { 68 | int h = hash(key); 69 | 70 | if(array[h]!=null) 71 | { 72 | array[h].deleteNode(key); 73 | n--; 74 | } 75 | else 76 | System.out.println("Value " + key + " not present\n"); 77 | } 78 | } 79 | 80 | 81 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package SeparateChaining; 7 | 8 | public class Node 9 | { 10 | public studentRecord info; 11 | public Node link; 12 | 13 | public Node(studentRecord rec) 14 | { 15 | info=rec; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/SingleLinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package SeparateChaining; 7 | 8 | public class SingleLinkedList 9 | { 10 | private Node start; 11 | 12 | public SingleLinkedList() 13 | { 14 | start=null; 15 | } 16 | 17 | public void displayList() 18 | { 19 | Node p; 20 | if(start==null) 21 | { 22 | System.out.println("___"); 23 | return; 24 | } 25 | p=start; 26 | while(p!=null) 27 | { 28 | System.out.print(p.info + " "); 29 | p=p.link; 30 | } 31 | System.out.println(); 32 | }/*End of displayList()*/ 33 | 34 | 35 | public studentRecord search(int x) 36 | { 37 | Node p=start; 38 | while(p!=null) 39 | { 40 | if(p.info.getstudentId()==x) 41 | break; 42 | p=p.link; 43 | } 44 | if(p==null) 45 | { 46 | System.out.println(x + " not found in list"); 47 | return null; 48 | } 49 | else 50 | return p.info; 51 | }/*End of search()*/ 52 | 53 | public void insertInBeginning(studentRecord data) 54 | { 55 | Node temp=new Node(data); 56 | temp.link=start; 57 | start=temp; 58 | } 59 | 60 | public void deleteNode(int x) 61 | { 62 | if(start==null) 63 | { 64 | System.out.println("Value " + x + " not present\n"); 65 | return; 66 | } 67 | 68 | /*Deletion of first node*/ 69 | if(start.info.getstudentId()==x) 70 | { 71 | start=start.link; 72 | return; 73 | } 74 | 75 | /*Deletion in between or at the end*/ 76 | Node p=start; 77 | while(p.link!=null) 78 | { 79 | if(p.link.info.getstudentId()==x) 80 | break; 81 | p=p.link; 82 | } 83 | 84 | if(p.link==null) 85 | System.out.println("Value " + x + " not present\n"); 86 | else 87 | p.link = p.link.link; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /hashing/SeparateChaining/studentRecord.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package SeparateChaining; 7 | 8 | public class studentRecord 9 | { 10 | private int studentId; 11 | private String studentName; 12 | 13 | public studentRecord(int i, String name) 14 | { 15 | studentId=i; 16 | studentName = name; 17 | } 18 | public int getstudentId() 19 | { 20 | return studentId; 21 | } 22 | public void setstudentId(int i) 23 | { 24 | studentId = i; 25 | } 26 | public String toString() 27 | { 28 | return studentId + " " + studentName + " "; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /linked-list/HeaderList/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package HeaderList; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,data,x,k; 15 | Scanner scan = new Scanner(System.in); 16 | 17 | HeaderList List = new HeaderList(); 18 | List.createList(); 19 | 20 | while(true) 21 | { 22 | System.out.println("1.Display list"); 23 | System.out.println("2.Insert a node at the end of the list"); 24 | System.out.println("3.Insert a new node before a node"); 25 | System.out.println("4.Insert at a given position"); 26 | System.out.println("5.Delete a node"); 27 | System.out.println("6.Reverse the list"); 28 | System.out.println("7.Quit"); 29 | System.out.println("Enter your choice : "); 30 | choice=scan.nextInt(); 31 | 32 | if(choice==7) 33 | break; 34 | 35 | switch(choice) 36 | { 37 | case 1: 38 | List.displayList(); 39 | break; 40 | case 2: 41 | System.out.print("Enter the element to be inserted : "); 42 | data = scan.nextInt(); 43 | List.insertAtEnd(data); 44 | break; 45 | case 3: 46 | System.out.print("Enter the element to be inserted : "); 47 | data = scan.nextInt(); 48 | System.out.print("Enter the element before which to insert : "); 49 | x = scan.nextInt(); 50 | List.insertBefore(data,x); 51 | break; 52 | case 4: 53 | System.out.print("Enter the element to be inserted : "); 54 | data = scan.nextInt(); 55 | System.out.print("Enter the position at which to insert : "); 56 | k = scan.nextInt(); 57 | List.insertAtPosition(data,k); 58 | break; 59 | case 5: 60 | System.out.print("Enter the element to be deleted : "); 61 | data = scan.nextInt(); 62 | List.deleteNode(data); 63 | break; 64 | case 6: 65 | List.reverse(); 66 | break; 67 | default: 68 | System.out.println("Wrong choice"); 69 | } 70 | }/*End of while */ 71 | 72 | } 73 | 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /linked-list/HeaderList/HeaderList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package HeaderList; 7 | 8 | import java.util.Scanner; 9 | 10 | public class HeaderList 11 | { 12 | private Node head; 13 | 14 | public HeaderList() 15 | { 16 | head=new Node(0); 17 | } 18 | 19 | public void displayList() 20 | { 21 | if(head.link==null) 22 | { 23 | System.out.println("List is empty\n"); 24 | return; 25 | } 26 | Node p=head.link; 27 | System.out.println("List is :\n"); 28 | while(p!=null) 29 | { 30 | System.out.println(p.info + " "); 31 | p=p.link; 32 | } 33 | System.out.println(); 34 | }/*End of displayList()*/ 35 | 36 | public void insertAtEnd(int data) 37 | { 38 | Node temp = new Node(data); 39 | 40 | Node p=head; 41 | while(p.link!=null) 42 | p=p.link; 43 | 44 | p.link=temp; 45 | temp.link=null; 46 | }/*End of insertAtEnd()*/ 47 | 48 | public void createList() 49 | { 50 | int i,n,data; 51 | Scanner scan = new Scanner(System.in); 52 | 53 | System.out.println("Enter the number of nodes : "); 54 | n=scan.nextInt(); 55 | 56 | for(i=1; i<=n; i++) 57 | { 58 | System.out.println("Enter the element to be inserted : "); 59 | data=scan.nextInt(); 60 | insertAtEnd(data); 61 | } 62 | }/*End of createList()*/ 63 | 64 | public void insertBefore(int data,int x) 65 | { 66 | Node temp; 67 | 68 | /*Find pointer to predecessor of node containing x*/ 69 | Node p=head; 70 | while(p.link!=null) 71 | { 72 | if(p.link.info==x) 73 | break; 74 | p=p.link; 75 | } 76 | 77 | if(p.link==null) 78 | System.out.println(x + " not present in the list"); 79 | else 80 | { 81 | temp=new Node(data); 82 | temp.link=p.link; 83 | p.link=temp; 84 | } 85 | }/*End of insertBefore()*/ 86 | 87 | public void insertAtPosition(int data,int k) 88 | { 89 | Node temp; 90 | int i; 91 | 92 | Node p=head; 93 | for(i=1; i<=k-1 && p!=null; i++) 94 | p=p.link; 95 | 96 | if(p==null) 97 | System.out.println("You can insert only upto " + (i-1) + "th position\n\n"); 98 | else 99 | { 100 | temp=new Node(data); 101 | temp.link=p.link; 102 | p.link=temp; 103 | } 104 | }/*End of insertAtPosition()*/ 105 | 106 | public void deleteNode(int data) 107 | { 108 | Node p=head; 109 | while(p.link!=null) 110 | { 111 | if(p.link.info==data) 112 | break; 113 | p=p.link; 114 | } 115 | 116 | if(p.link==null) 117 | System.out.println(data + "Element %d not found"); 118 | else 119 | p.link = p.link.link; 120 | }/*End of deleteNode()*/ 121 | 122 | public void reverse() 123 | { 124 | Node prev, p, next; 125 | prev=null; 126 | p=head.link; 127 | while(p!=null) 128 | { 129 | next=p.link; 130 | p.link=prev; 131 | prev=p; 132 | p=next; 133 | } 134 | head.link=prev; 135 | }/*End of reverse()*/ 136 | } 137 | -------------------------------------------------------------------------------- /linked-list/HeaderList/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package HeaderList; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linked-list/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Java 2 | 3 | This [“Data Structures and Algorithms in Java”](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 16 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 19 | 20 | 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /linked-list/circularLinkedList/CircularLinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package circularLinkedList; 7 | import java.util.Scanner; 8 | 9 | public class CircularLinkedList 10 | { 11 | private Node last; 12 | 13 | public CircularLinkedList() 14 | { 15 | last=null; 16 | } 17 | public void displayList() 18 | { 19 | Node p; 20 | if(last==null) 21 | { 22 | System.out.println("List is empty\n"); 23 | return; 24 | } 25 | 26 | p=last.link; 27 | do 28 | { 29 | System.out.print(p.info + " "); 30 | p=p.link; 31 | }while(p!=last.link); 32 | System.out.println(); 33 | }/*End of displayList()*/ 34 | 35 | public void insertInBeginning(int data) 36 | { 37 | Node temp=new Node(data); 38 | temp.link=last.link; 39 | last.link=temp; 40 | }/*End of insertInBeginning( )*/ 41 | 42 | public void insertInEmptyList(int data) 43 | { 44 | Node temp=new Node(data); 45 | last=temp; 46 | last.link=last; 47 | }/*End of insertInEmptyList()*/ 48 | 49 | public void insertAtEnd(int data) 50 | { 51 | Node temp=new Node(data); 52 | temp.link=last.link; 53 | last.link=temp; 54 | last=temp; 55 | }/*End of insertAtEnd( )*/ 56 | 57 | public void createList() 58 | { 59 | int i,n,data; 60 | 61 | Scanner scan = new Scanner(System.in); 62 | 63 | System.out.print("Enter the number of nodes : "); 64 | n = scan.nextInt(); 65 | 66 | if(n==0) 67 | return; 68 | System.out.print("Enter the element to be inserted : "); 69 | data = scan.nextInt(); 70 | insertInEmptyList(data); 71 | 72 | for(i=2; i<=n; i++) 73 | { 74 | System.out.print("Enter the element to be inserted : "); 75 | data = scan.nextInt(); 76 | insertAtEnd(data); 77 | } 78 | }/*End of createList()*/ 79 | 80 | public void insertAfter(int data,int x) 81 | { 82 | Node p=last.link; 83 | do 84 | { 85 | if(p.info==x) 86 | break; 87 | p=p.link; 88 | }while(p!=last.link); 89 | 90 | if(p==last.link && p.info!=x) 91 | System.out.println(x + " not present in the list"); 92 | else 93 | { 94 | Node temp=new Node(data); 95 | temp.link=p.link; 96 | p.link=temp; 97 | if(p==last) 98 | last=temp; 99 | } 100 | }/*End of insertAfter()*/ 101 | 102 | public void deleteFirstNode() 103 | { 104 | if(last==null) /*List is empty*/ 105 | return; 106 | if(last.link==last) /*List has only one node*/ 107 | { 108 | last=null; 109 | return; 110 | } 111 | last.link=last.link.link; 112 | } 113 | 114 | public void deleteLastNode() 115 | { 116 | if(last==null) /*List is empty*/ 117 | return; 118 | if(last.link==last) /*List has only one node*/ 119 | { 120 | last=null; 121 | return; 122 | } 123 | Node p=last.link; 124 | while(p.link!=last) 125 | p=p.link; 126 | p.link=last.link; 127 | last=p; 128 | } 129 | 130 | public void deleteNode(int x) 131 | { 132 | if(last==null)/*List is empty*/ 133 | return; 134 | 135 | if(last.link==last && last.info==x) /*Deletion of only node*/ 136 | { 137 | last=null; 138 | return; 139 | } 140 | 141 | if(last.link.info==x) /*Deletion of first node*/ 142 | { 143 | last.link=last.link.link; 144 | return; 145 | } 146 | 147 | Node p=last.link; 148 | while(p.link!=last.link) 149 | { 150 | if(p.link.info==x) 151 | break; 152 | p=p.link; 153 | } 154 | 155 | if(p.link==last.link) 156 | System.out.println(x + " not found in the list"); 157 | else 158 | { 159 | p.link=p.link.link; 160 | if(last.info==x) 161 | last=p; 162 | } 163 | }/*End of deleteNode()*/ 164 | 165 | public void concatenate(CircularLinkedList list) 166 | { 167 | if(last==null) 168 | { 169 | last=list.last; 170 | return; 171 | } 172 | 173 | if(list.last==null) 174 | return; 175 | 176 | Node p=last.link; 177 | last.link=list.last.link; 178 | list.last.link=p; 179 | last=list.last; 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /linked-list/circularLinkedList/ConcatenateDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package circularLinkedList; 7 | 8 | public class ConcatenateDemo 9 | { 10 | public static void main(String[] args) 11 | { 12 | CircularLinkedList List1 = new CircularLinkedList(); 13 | CircularLinkedList List2 = new CircularLinkedList(); 14 | 15 | System.out.println("Enter first list :- "); 16 | List1.createList(); 17 | System.out.println("Enter second list :- "); 18 | List2.createList(); 19 | 20 | System.out.print("First "); 21 | List1.displayList(); 22 | System.out.print("Second "); 23 | List2.displayList(); 24 | 25 | List1.concatenate(List2); 26 | System.out.print("First "); 27 | List1.displayList(); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /linked-list/circularLinkedList/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package circularLinkedList; 7 | import java.util.Scanner; 8 | 9 | public class Demo 10 | { 11 | public static void main(String[] args) 12 | { 13 | int choice,data,x; 14 | Scanner scan = new Scanner(System.in); 15 | CircularLinkedList List = new CircularLinkedList(); 16 | 17 | List.createList(); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Display List"); 22 | System.out.println("2.Insert in empty list"); 23 | System.out.println("3.Insert in the beginning"); 24 | System.out.println("4.Insert at the end"); 25 | System.out.println("5.Insert after a node"); 26 | System.out.println("6.Delete first node"); 27 | System.out.println("7.Delete last node"); 28 | System.out.println("8.Delete any node"); 29 | System.out.println("9.Quit"); 30 | 31 | System.out.print("Enter your choice : "); 32 | choice = scan.nextInt(); 33 | 34 | if(choice==9) 35 | break; 36 | 37 | switch(choice) 38 | { 39 | case 1: 40 | List.displayList(); 41 | break; 42 | case 2: 43 | System.out.print("Enter the element to be inserted : "); 44 | data = scan.nextInt(); 45 | List.insertInEmptyList(data); 46 | break; 47 | case 3: 48 | System.out.print("Enter the element to be inserted : "); 49 | data = scan.nextInt(); 50 | List.insertInBeginning(data); 51 | break; 52 | case 4: 53 | System.out.print("Enter the element to be inserted : "); 54 | data = scan.nextInt(); 55 | List.insertAtEnd(data); 56 | break; 57 | case 5: 58 | System.out.print("Enter the element to be inserted : "); 59 | data = scan.nextInt(); 60 | System.out.print("Enter the element after which to insert : "); 61 | x = scan.nextInt(); 62 | List.insertAfter(data,x); 63 | break; 64 | case 6: 65 | List.deleteFirstNode(); 66 | break; 67 | case 7: 68 | List.deleteLastNode(); 69 | break; 70 | case 8: 71 | System.out.print("Enter the element to be deleted : "); 72 | data = scan.nextInt(); 73 | List.deleteNode(data); 74 | break; 75 | default: 76 | System.out.println("Wrong choice"); 77 | }/*End of switch*/ 78 | System.out.println(); 79 | }/*End of while*/ 80 | scan.close(); 81 | System.out.println("Exiting"); 82 | }/*End of main( )*/ 83 | } 84 | 85 | 86 | -------------------------------------------------------------------------------- /linked-list/circularLinkedList/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package circularLinkedList; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linked-list/doubleLinkedList/DoubleLinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package doubleLinkedList; 7 | import java.util.Scanner; 8 | 9 | public class DoubleLinkedList 10 | { 11 | private Node start; 12 | 13 | public DoubleLinkedList() 14 | { 15 | start=null; 16 | } 17 | public void displayList() 18 | { 19 | Node p; 20 | if(start==null) 21 | { 22 | System.out.println("List is empty"); 23 | return; 24 | } 25 | 26 | p=start; 27 | System.out.print("List is : "); 28 | while(p!=null) 29 | { 30 | System.out.print(p.info + " "); 31 | p=p.next; 32 | } 33 | System.out.println(); 34 | }/*End of displayList()*/ 35 | 36 | public void insertInBeginning(int data) 37 | { 38 | Node temp=new Node(data); 39 | temp.next=start; 40 | start.prev=temp; 41 | start=temp; 42 | }/*End of insertInBeginning()*/ 43 | 44 | public void insertInEmptyList(int data) 45 | { 46 | Node temp=new Node(data); 47 | start=temp; 48 | }/*End of insertInEmptyList()*/ 49 | 50 | public void insertAtEnd(int data) 51 | { 52 | Node temp=new Node(data); 53 | 54 | Node p=start; 55 | while(p.next!=null) 56 | p=p.next; 57 | p.next=temp; 58 | temp.prev=p; 59 | }/*End of insertAtEnd()*/ 60 | 61 | public void createList() 62 | { 63 | int i,n,data; 64 | 65 | Scanner scan = new Scanner(System.in); 66 | 67 | System.out.print("Enter the number of nodes : "); 68 | n = scan.nextInt(); 69 | 70 | if(n==0) 71 | return; 72 | 73 | System.out.print("Enter the first element to be inserted : "); 74 | data = scan.nextInt(); 75 | insertInEmptyList(data); 76 | 77 | for(i=2; i<=n; i++) 78 | { 79 | System.out.println("Enter the next element to be inserted : "); 80 | data = scan.nextInt(); 81 | insertAtEnd(data); 82 | } 83 | }/*End of createList()*/ 84 | 85 | public void insertAfter(int data,int x) 86 | { 87 | Node temp=new Node(data); 88 | Node p=start; 89 | while(p!=null) 90 | { 91 | if(p.info==x) 92 | break; 93 | p=p.next; 94 | } 95 | 96 | if(p==null) 97 | System.out.println(x + " not present in the list"); 98 | else 99 | { 100 | temp.prev=p; 101 | temp.next=p.next; 102 | if(p.next!=null) 103 | p.next.prev=temp; /*should not be done when p refers to last node*/ 104 | p.next=temp; 105 | } 106 | }/*End of insertAfter()*/ 107 | 108 | public void insertBefore(int data,int x) 109 | { 110 | if(start==null) 111 | { 112 | System.out.println("List is empty"); 113 | return; 114 | } 115 | if(start.info==x) 116 | { 117 | Node temp=new Node(data); 118 | temp.next=start; 119 | start.prev=temp; 120 | start=temp; 121 | return; 122 | } 123 | 124 | Node p=start; 125 | while(p!=null) 126 | { 127 | if(p.info==x) 128 | break; 129 | p=p.next; 130 | } 131 | 132 | if(p==null) 133 | System.out.println(x + " not present in the list"); 134 | else 135 | { 136 | Node temp=new Node(data); 137 | temp.prev=p.prev; 138 | temp.next = p; 139 | p.prev.next=temp; 140 | p.prev=temp; 141 | } 142 | }/*End of insertBefore()*/ 143 | 144 | public void deleteFirstNode() 145 | { 146 | if(start==null) /*list is empty*/ 147 | return; 148 | if(start.next==null) /*list has only one node*/ 149 | { 150 | start=null; 151 | return; 152 | } 153 | start=start.next; 154 | start.prev=null; 155 | } 156 | 157 | public void deleteLastNode() 158 | { 159 | if(start==null) /*list is empty*/ 160 | return; 161 | if(start.next==null) /*list has only one node*/ 162 | { 163 | start=null; 164 | return; 165 | } 166 | Node p=start; 167 | while(p.next!=null) 168 | p=p.next; 169 | p.prev.next=null; 170 | } 171 | 172 | public void deleteNode(int x) 173 | { 174 | if(start==null) /*list is empty*/ 175 | return; 176 | 177 | if(start.next==null) /*list has only one node*/ 178 | { 179 | if(start.info==x) 180 | start=null; 181 | else 182 | System.out.println(x + " not found"); 183 | return; 184 | } 185 | 186 | /*Deletion of first node*/ 187 | if(start.info==x) 188 | { 189 | start=start.next; 190 | start.prev=null; 191 | return; 192 | } 193 | 194 | Node p=start.next; 195 | while(p.next!=null) 196 | { 197 | if(p.info==x) 198 | break; 199 | p=p.next; 200 | } 201 | 202 | if(p.next!=null) /*node to be deleted is in between*/ 203 | { 204 | p.prev.next=p.next; 205 | p.next.prev=p.prev; 206 | } 207 | else /*p refers to last node*/ 208 | { 209 | if(p.info==x)/*node to be deleted is last node*/ 210 | p.prev.next=null; 211 | else 212 | System.out.println(x + " not found"); 213 | } 214 | }/*End of deleteNode()*/ 215 | 216 | public void reverseList() 217 | { 218 | if(start==null) 219 | return; 220 | 221 | Node p1=start; 222 | Node p2=p1.next; 223 | p1.next=null; 224 | p1.prev=p2; 225 | while(p2!=null) 226 | { 227 | p2.prev=p2.next; 228 | p2.next=p1; 229 | p1=p2; 230 | p2=p2.prev; 231 | } 232 | start=p1; 233 | }/*End of reverseList()*/ 234 | 235 | } 236 | -------------------------------------------------------------------------------- /linked-list/doubleLinkedList/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package doubleLinkedList; 7 | 8 | public class Node 9 | { 10 | public Node prev; 11 | public int info; 12 | public Node next; 13 | 14 | public Node(int i) 15 | { 16 | info=i; 17 | prev=null; 18 | next=null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linked-list/doubleLinkedList/demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package doubleLinkedList; 7 | import java.util.Scanner; 8 | 9 | public class demo 10 | { 11 | public static void main(String[] args) 12 | { 13 | int choice,data,x; 14 | Scanner scan = new Scanner(System.in); 15 | 16 | DoubleLinkedList List = new DoubleLinkedList(); 17 | List.createList(); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Display List"); 22 | System.out.println("2.Insert in empty list"); 23 | System.out.println("3.Insert a node in beginning of the list"); 24 | System.out.println("4.Insert a node at the end of the list"); 25 | System.out.println("5.Insert a node after a specified node"); 26 | System.out.println("6.Insert a node before a specified node"); 27 | System.out.println("7.Delete first node"); 28 | System.out.println("8.Delete last node"); 29 | System.out.println("9.Delete any node"); 30 | System.out.println("10.Reverse the list"); 31 | System.out.println("11.Quit"); 32 | System.out.println("Enter your choice : "); 33 | choice = scan.nextInt(); 34 | 35 | if(choice==11) 36 | break; 37 | 38 | switch(choice) 39 | { 40 | case 1: 41 | List.displayList(); 42 | break; 43 | case 2: 44 | System.out.print("Enter the element to be inserted : "); 45 | data = scan.nextInt(); 46 | List.insertInEmptyList(data); 47 | break; 48 | case 3: 49 | System.out.print("Enter the element to be inserted : "); 50 | data = scan.nextInt(); 51 | List.insertInBeginning(data); 52 | break; 53 | case 4: 54 | System.out.print("Enter the element to be inserted : "); 55 | data = scan.nextInt(); 56 | List.insertAtEnd(data); 57 | break; 58 | case 5: 59 | System.out.print("Enter the element to be inserted : "); 60 | data = scan.nextInt(); 61 | System.out.print("Enter the element after which to insert : "); 62 | x = scan.nextInt(); 63 | List.insertAfter(data,x); 64 | break; 65 | case 6: 66 | System.out.print("Enter the element to be inserted : "); 67 | data = scan.nextInt(); 68 | System.out.print("Enter the element before which to insert : "); 69 | x = scan.nextInt(); 70 | List.insertBefore(data,x); 71 | break; 72 | case 7: 73 | List.deleteFirstNode(); 74 | break; 75 | case 8: 76 | List.deleteLastNode(); 77 | break; 78 | case 9: 79 | System.out.print("Enter the element to be deleted : "); 80 | data = scan.nextInt(); 81 | List.deleteNode(data); 82 | break; 83 | case 10: 84 | List.reverseList(); 85 | break; 86 | default: 87 | System.out.println("Wrong choice"); 88 | }/*End of switch*/ 89 | System.out.println(); 90 | }/*End of while*/ 91 | scan.close(); 92 | System.out.println("Exiting"); 93 | }/*End of main()*/ 94 | } 95 | -------------------------------------------------------------------------------- /linked-list/singleLinkedList/ConcatenateDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package singleLinkedList; 7 | 8 | public class ConcatenateDemo 9 | { 10 | public static void main(String[] args) 11 | { 12 | SingleLinkedList List1 = new SingleLinkedList(); 13 | SingleLinkedList List2 = new SingleLinkedList(); 14 | 15 | System.out.println("Enter first list :- "); 16 | List1.createList(); 17 | System.out.println("Enter second list :- "); 18 | List2.createList(); 19 | 20 | System.out.print("First "); 21 | List1.displayList(); 22 | System.out.print("Second "); 23 | List2.displayList(); 24 | 25 | List1.concatenate(List2); 26 | System.out.print("First "); 27 | List1.displayList(); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /linked-list/singleLinkedList/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package singleLinkedList; 7 | import java.util.Scanner; 8 | 9 | public class Demo 10 | { 11 | public static void main(String[] args) 12 | { 13 | int choice,data,k,x; 14 | 15 | Scanner scan = new Scanner(System.in); 16 | 17 | SingleLinkedList List = new SingleLinkedList(); 18 | 19 | List.createList(); 20 | 21 | while(true) 22 | { 23 | System.out.println("1.Display list"); 24 | System.out.println("2.Count the number of nodes"); 25 | System.out.println("3.Search for an element"); 26 | System.out.println("4.Insert in empty list/Insert in beginning of the list"); 27 | System.out.println("5.Insert a node at the end of the list"); 28 | System.out.println("6.Insert a node after a specified node"); 29 | System.out.println("7.Insert a node before a specified node"); 30 | System.out.println("8.Insert a node at a given position"); 31 | System.out.println("9.Delete first node"); 32 | System.out.println("10.Delete last node"); 33 | System.out.println("11.Delete any node"); 34 | System.out.println("12.Reverse the list"); 35 | System.out.println("13.Bubble sort by exchanging data"); 36 | System.out.println("14.Bubble sort by exchanging links"); 37 | System.out.println("15.MergeSort"); 38 | System.out.println("16.Insert Cycle"); 39 | System.out.println("17.Detect Cycle"); 40 | System.out.println("18.Remove cycle"); 41 | System.out.println("19.Quit"); 42 | 43 | System.out.print("Enter your choice : "); 44 | choice = scan.nextInt(); 45 | 46 | if(choice==19) 47 | break; 48 | 49 | switch(choice) 50 | { 51 | case 1: 52 | List.displayList(); 53 | break; 54 | case 2: 55 | List.countNodes(); 56 | break; 57 | case 3: 58 | System.out.print("Enter the element to be searched : "); 59 | data=scan.nextInt(); 60 | List.search(data); 61 | break; 62 | case 4: 63 | System.out.print("Enter the element to be inserted : "); 64 | data=scan.nextInt(); 65 | List.insertInBeginning(data); 66 | break; 67 | case 5: 68 | System.out.print("Enter the element to be inserted : "); 69 | data=scan.nextInt(); 70 | List.insertAtEnd(data); 71 | break; 72 | case 6: 73 | System.out.print("Enter the element to be inserted : "); 74 | data=scan.nextInt(); 75 | System.out.print("Enter the element after which to insert : "); 76 | x=scan.nextInt(); 77 | List.insertAfter(data,x); 78 | break; 79 | case 7: 80 | System.out.print("Enter the element to be inserted : "); 81 | data=scan.nextInt(); 82 | System.out.print("Enter the element before which to insert : "); 83 | x=scan.nextInt(); 84 | List.insertBefore(data,x); 85 | break; 86 | case 8: 87 | System.out.print("Enter the element to be inserted : "); 88 | data=scan.nextInt(); 89 | System.out.print("Enter the position at which to insert : "); 90 | k=scan.nextInt(); 91 | List.insertAtPosition(data,k); 92 | break; 93 | case 9: 94 | List.deleteFirstNode(); 95 | break; 96 | case 10: 97 | List.deleteLastNode(); 98 | break; 99 | case 11: 100 | System.out.print("Enter the element to be deleted : "); 101 | data=scan.nextInt(); 102 | List.deleteNode(data); 103 | break; 104 | case 12: 105 | List.reverseList(); 106 | break; 107 | case 13: 108 | List.BubbleSortExData(); 109 | break; 110 | case 14: 111 | List.BubbleSortExLinks(); 112 | break; 113 | case 15: 114 | List.mergeSort(); 115 | break; 116 | case 16: 117 | System.out.print("Enter the element at which the cycle has to be inserted : "); 118 | data=scan.nextInt(); 119 | List.insertCycle(data); 120 | break; 121 | case 17: 122 | if(List.hasCycle()) 123 | System.out.println("List has a cycle"); 124 | else 125 | System.out.println("List does not have a cycle"); 126 | break; 127 | case 18: 128 | List.removeCycle(); 129 | break; 130 | default: 131 | System.out.println("Wrong choice"); 132 | }/*End of switch*/ 133 | System.out.println(); 134 | }/*End of while*/ 135 | scan.close(); 136 | System.out.println("Exiting"); 137 | }/*End of main()*/ 138 | } 139 | 140 | -------------------------------------------------------------------------------- /linked-list/singleLinkedList/MergingDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package singleLinkedList; 7 | 8 | public class MergingDemo 9 | { 10 | public static void main(String[] args) 11 | { 12 | SingleLinkedList list1 = new SingleLinkedList(); 13 | SingleLinkedList list2 = new SingleLinkedList(); 14 | 15 | list1.createList(); 16 | list2.createList(); 17 | 18 | list1.BubbleSortExData(); 19 | list2.BubbleSortExData(); 20 | 21 | System.out.println("First List - "); list1.displayList(); 22 | System.out.println("Second List - "); list2.displayList(); 23 | 24 | SingleLinkedList list3; 25 | 26 | list3 = list1.merge1(list2); 27 | System.out.println("Merged List - "); list3.displayList(); 28 | 29 | System.out.println("First List - "); list1.displayList(); 30 | System.out.println("Second List - "); list2.displayList(); 31 | 32 | list3 = list1.merge2(list2); 33 | System.out.println("Merged List - "); list3.displayList(); 34 | 35 | System.out.println("First List - "); list1.displayList(); 36 | System.out.println("Second List - "); list2.displayList(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /linked-list/singleLinkedList/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package singleLinkedList; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linked-list/singleLinkedList/SingleLinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package singleLinkedList; 7 | import java.util.Scanner; 8 | 9 | import sortedLinkedList.Node; 10 | 11 | public class SingleLinkedList 12 | { 13 | private Node start; 14 | 15 | public SingleLinkedList() 16 | { 17 | start=null; 18 | } 19 | 20 | public void displayList() 21 | { 22 | Node p; 23 | if(start==null) 24 | { 25 | System.out.println("List is empty"); 26 | return; 27 | } 28 | System.out.print("List is : "); 29 | p=start; 30 | while(p!=null) 31 | { 32 | System.out.print(p.info + " "); 33 | p=p.link; 34 | } 35 | System.out.println(); 36 | }/*End of displayList()*/ 37 | 38 | public void countNodes() 39 | { 40 | int n=0; 41 | Node p=start; 42 | while(p!=null) 43 | { 44 | n++; 45 | p=p.link; 46 | } 47 | System.out.println("Number of nodes in the list = " + n); 48 | }/*End of countNodes() */ 49 | 50 | public boolean search(int x) 51 | { 52 | Node p; 53 | 54 | int position=1; 55 | p=start; 56 | while(p!=null) 57 | { 58 | if(p.info==x) 59 | break; 60 | position++; 61 | p=p.link; 62 | } 63 | if(p==null) 64 | { 65 | System.out.println(x + " not found in list"); 66 | return false; 67 | } 68 | else 69 | { 70 | System.out.println(x + " is at position " + position); 71 | return true; 72 | } 73 | }/*End of search()*/ 74 | 75 | public void insertInBeginning(int data) 76 | { 77 | Node temp=new Node(data); 78 | temp.link=start; 79 | start=temp; 80 | } 81 | 82 | public void insertAtEnd(int data) 83 | { 84 | Node p; 85 | Node temp=new Node(data); 86 | 87 | if(start==null) 88 | { 89 | start=temp; 90 | return; 91 | } 92 | 93 | p=start; 94 | while(p.link!=null) 95 | p=p.link; 96 | 97 | p.link=temp; 98 | } 99 | 100 | public void createList() 101 | { 102 | int i,n,data; 103 | 104 | Scanner scan = new Scanner(System.in); 105 | 106 | System.out.print("Enter the number of nodes : "); 107 | n = scan.nextInt(); 108 | 109 | if(n==0) 110 | return; 111 | 112 | for(i=1; i<=n; i++) 113 | { 114 | System.out.print("Enter the element to be inserted : "); 115 | data = scan.nextInt(); 116 | insertAtEnd(data); 117 | } 118 | }/*End of createList()*/ 119 | 120 | 121 | public void insertAfter(int data,int x) 122 | { 123 | Node p=start; 124 | while(p!=null) 125 | { 126 | if(p.info==x) 127 | break; 128 | p=p.link; 129 | } 130 | 131 | if(p==null) 132 | System.out.println(x + " not present in the list"); 133 | else 134 | { 135 | Node temp=new Node(data); 136 | temp.link=p.link; 137 | p.link=temp; 138 | } 139 | } 140 | 141 | public void insertBefore(int data,int x) 142 | { 143 | Node temp; 144 | 145 | /*If list is empty*/ 146 | if(start==null) 147 | { 148 | System.out.println("List is empty"); 149 | return; 150 | } 151 | 152 | /*x is in first node,new node is to be inserted before first node*/ 153 | if(x==start.info) 154 | { 155 | temp=new Node(data); 156 | temp.link=start; 157 | start=temp; 158 | return; 159 | } 160 | 161 | /*Find reference to predecessor of node containing x*/ 162 | Node p=start; 163 | while(p.link!=null) 164 | { 165 | if(p.link.info==x) 166 | break; 167 | p=p.link; 168 | } 169 | 170 | if(p.link==null) 171 | System.out.println(x + " not present in the list"); 172 | else 173 | { 174 | temp=new Node(data); 175 | temp.link=p.link; 176 | p.link=temp; 177 | } 178 | }/*End of insertBefore()*/ 179 | 180 | public void insertAtPosition(int data,int k) 181 | { 182 | Node temp; 183 | int i; 184 | 185 | if(k==1) 186 | { 187 | temp=new Node(data); 188 | temp.link=start; 189 | start=temp; 190 | return; 191 | } 192 | 193 | Node p=start; 194 | for(i=1; i q.info) 302 | { 303 | int temp=p.info; 304 | p.info=q.info; 305 | q.info=temp; 306 | } 307 | } 308 | } 309 | }/*End of BubbleSortExData()*/ 310 | 311 | public void BubbleSortExLinks() 312 | { 313 | Node end,r,p,q,temp; 314 | 315 | for(end=null; end!=start.link; end=p) 316 | { 317 | for(r=p=start; p.link!=end; r=p,p=p.link) 318 | { 319 | q=p.link; 320 | if(p.info > q.info) 321 | { 322 | p.link=q.link; 323 | q.link=p; 324 | if(p!=start) 325 | r.link=q; 326 | else 327 | start=q; 328 | temp=p; 329 | p=q; 330 | q=temp; 331 | } 332 | } 333 | } 334 | }/*End of BubbleSortExLinks()*/ 335 | 336 | public SingleLinkedList merge1(SingleLinkedList list) 337 | { 338 | SingleLinkedList mergeList = new SingleLinkedList(); 339 | mergeList.start = merge1(start, list.start); 340 | return mergeList; 341 | } 342 | 343 | private Node merge1(Node p1, Node p2) 344 | { 345 | Node startM; 346 | 347 | if(p1.info <= p2.info) 348 | { 349 | startM = new Node(p1.info); 350 | p1=p1.link; 351 | } 352 | else 353 | { 354 | startM = new Node(p2.info); 355 | p2=p2.link; 356 | } 357 | 358 | Node pM=startM; 359 | 360 | while(p1!=null && p2!=null) 361 | { 362 | if(p1.info <= p2.info) 363 | { 364 | pM.link=new Node(p1.info); 365 | p1=p1.link; 366 | } 367 | else 368 | { 369 | pM.link=new Node(p2.info); 370 | p2=p2.link; 371 | } 372 | pM=pM.link; 373 | } 374 | 375 | /*If second list has finished and elements left in first list*/ 376 | while(p1!=null) 377 | { 378 | pM.link=new Node(p1.info); 379 | p1=p1.link; 380 | pM=pM.link; 381 | } 382 | 383 | /*If first list has finished and elements left in second list*/ 384 | while(p2!=null) 385 | { 386 | pM.link=new Node(p2.info); 387 | p2=p2.link; 388 | pM=pM.link; 389 | } 390 | return startM; 391 | } 392 | 393 | public SingleLinkedList merge2(SingleLinkedList list) 394 | { 395 | SingleLinkedList mergeList = new SingleLinkedList(); 396 | mergeList.start = merge2(start, list.start); 397 | return mergeList; 398 | } 399 | 400 | private Node merge2(Node p1, Node p2) 401 | { 402 | Node startM; 403 | 404 | if(p1.info <= p2.info) 405 | { 406 | startM = p1; 407 | p1 = p1.link; 408 | } 409 | else 410 | { 411 | startM = p2; 412 | p2 = p2.link; 413 | } 414 | 415 | Node pM = startM; 416 | 417 | while( p1!=null && p2!=null ) 418 | { 419 | if( p1.info <= p2.info ) 420 | { 421 | pM.link = p1; 422 | pM = pM.link; 423 | p1 = p1.link; 424 | } 425 | else 426 | { 427 | pM.link = p2; 428 | pM = pM.link; 429 | p2 = p2.link; 430 | } 431 | } 432 | 433 | if(p1==null) 434 | pM.link=p2; 435 | else 436 | pM.link=p1; 437 | return startM; 438 | } 439 | 440 | 441 | public void mergeSort() 442 | { 443 | start = mergeSortRec(start); 444 | } 445 | 446 | private Node mergeSortRec(Node listStart) 447 | { 448 | if( listStart==null || listStart.link==null )/*if list empty or has one element*/ 449 | return listStart; 450 | 451 | /*if more than one element*/ 452 | Node start1 = listStart; 453 | Node start2 = divideList(listStart); 454 | start1 = mergeSortRec(start1); 455 | start2 = mergeSortRec(start2); 456 | Node startM = merge2(start1, start2); 457 | return startM; 458 | } 459 | private Node divideList(Node p) 460 | { 461 | Node q=p.link.link; 462 | while(q!=null && q.link!=null) 463 | { 464 | p=p.link; 465 | q=q.link.link; 466 | } 467 | Node start2 = p.link; 468 | p.link = null; 469 | return start2; 470 | } 471 | 472 | 473 | public boolean hasCycle() 474 | { 475 | if(findCycle()==null) 476 | return false; 477 | else 478 | return true; 479 | } 480 | 481 | private Node findCycle() 482 | { 483 | if(start==null || start.link==null) 484 | return null; 485 | 486 | Node slowR=start, fastR=start; 487 | 488 | while(fastR!=null && fastR.link!=null) 489 | { 490 | slowR=slowR.link; 491 | fastR=fastR.link.link; 492 | if( slowR==fastR ) 493 | return slowR; 494 | } 495 | return null; 496 | }/*End of findCycle()*/ 497 | 498 | public void removeCycle() 499 | { 500 | Node c=findCycle(); 501 | if(c==null) 502 | return; 503 | System.out.println("Node at which the cycle was detected is " + c.info); 504 | 505 | Node p=c, q=c; 506 | int lenCycle=0; 507 | do 508 | { 509 | lenCycle++; 510 | q=q.link; 511 | }while(p!=q); 512 | System.out.println("Length of cycle is : " + lenCycle); 513 | 514 | int lenRemList=0; 515 | p=start; 516 | while(p!=q) 517 | { 518 | lenRemList++; 519 | p=p.link; 520 | q=q.link; 521 | } 522 | 523 | System.out.println("Number of nodes not included in the cycle are : " + lenRemList); 524 | 525 | int lengthList = lenCycle + lenRemList; 526 | System.out.println("Length of the list is : " + lengthList); 527 | 528 | p=start; 529 | for(int i=1; i<=lengthList-1; i++) 530 | p=p.link; 531 | p.link=null; 532 | } 533 | 534 | public void insertCycle(int x) 535 | { 536 | if(start==null) 537 | return; 538 | 539 | Node p=start,px=null, prev=null; 540 | 541 | while(p!=null) 542 | { 543 | if(p.info==x) 544 | px=p; 545 | prev=p; 546 | p=p.link; 547 | } 548 | if(px!=null) 549 | prev.link=px; 550 | else 551 | System.out.println(x + " not present in list"); 552 | }/*End of insertCycle()*/ 553 | } 554 | -------------------------------------------------------------------------------- /linked-list/sortedLinkedList/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package sortedLinkedList; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,data; 15 | 16 | Scanner scan = new Scanner(System.in); 17 | 18 | SortedLinkedList List = new SortedLinkedList(); 19 | List.createList(); 20 | 21 | while(true) 22 | { 23 | System.out.println("1.Display List"); 24 | System.out.println("2.Insert a new node"); 25 | System.out.println("3.Search"); 26 | System.out.println("4.Exit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | if(choice==4) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | List.displayList(); 36 | break; 37 | case 2: 38 | System.out.println("Enter the element to be inserted : "); 39 | data = scan.nextInt(); 40 | List.insertInOrder(data); 41 | break; 42 | case 3: 43 | System.out.println("Enter the element to be searched : "); 44 | data=scan.nextInt(); 45 | List.search(data); 46 | break; 47 | default: 48 | System.out.println("Wrong choice\n"); 49 | } 50 | }/*End of while*/ 51 | }/*End of main()*/ 52 | } -------------------------------------------------------------------------------- /linked-list/sortedLinkedList/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package sortedLinkedList; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /linked-list/sortedLinkedList/SortedLinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package sortedLinkedList; 7 | 8 | import java.util.Scanner; 9 | 10 | public class SortedLinkedList 11 | { 12 | private Node start; 13 | 14 | public SortedLinkedList() 15 | { 16 | start=null; 17 | } 18 | public void insertInOrder(int data) 19 | { 20 | Node temp=new Node(data); 21 | 22 | /*List empty or new node to be inserted before first node*/ 23 | if(start==null || data 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /queue/circularQueue/CircularQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package circularQueue; 7 | import java.util.NoSuchElementException; 8 | 9 | public class CircularQueue 10 | { 11 | private int[] queueArray; 12 | private int front; 13 | private int rear; 14 | 15 | public CircularQueue() 16 | { 17 | queueArray = new int[10]; 18 | front=-1; 19 | rear=-1; 20 | } 21 | public CircularQueue(int maxSize) 22 | { 23 | queueArray = new int[maxSize]; 24 | front=-1; 25 | rear=-1; 26 | } 27 | public boolean isEmpty() 28 | { 29 | return (front==-1); 30 | } 31 | public boolean isFull() 32 | { 33 | return ((front==0 && rear==queueArray.length-1) || (front==rear+1)); 34 | } 35 | 36 | public void insert(int x) 37 | { 38 | if(isFull()) 39 | { 40 | System.out.println("Queue Overflow\n"); 41 | return; 42 | } 43 | if(front==-1) 44 | front=0; 45 | if(rear==queueArray.length-1) 46 | rear=0; 47 | else 48 | rear=rear+1; 49 | queueArray[rear]=x; 50 | } 51 | 52 | public int Delete() 53 | { 54 | if(isEmpty()) 55 | { 56 | System.out.println("Queue Underflow"); 57 | throw new NoSuchElementException(); 58 | } 59 | 60 | int x=queueArray[front]; 61 | 62 | if(front==rear) /*queue has only one element*/ 63 | { 64 | front=-1; 65 | rear=-1; 66 | } 67 | else if(front==queueArray.length-1) 68 | front=0; 69 | else 70 | front=front+1; 71 | return x; 72 | } 73 | 74 | public int peek() 75 | { 76 | if(isEmpty()) 77 | { 78 | System.out.println("Queue Underflow"); 79 | throw new NoSuchElementException(); 80 | } 81 | return queueArray[front]; 82 | } 83 | 84 | public void display() 85 | { 86 | if(isEmpty()) 87 | { 88 | System.out.println("Queue is empty"); 89 | return; 90 | } 91 | 92 | System.out.println("Queue is : "); 93 | 94 | int i=front; 95 | if(front<=rear) 96 | { 97 | while(i<=rear) 98 | System.out.print(queueArray[i++] + " "); 99 | } 100 | else 101 | { 102 | while(i<=queueArray.length-1) 103 | System.out.println(queueArray[i++] + " "); 104 | i=0; 105 | while(i<=rear) 106 | System.out.println(queueArray[i++] + " "); 107 | } 108 | System.out.println(); 109 | } 110 | 111 | public int size() 112 | { 113 | if(isEmpty()) 114 | return 0; 115 | if(isFull()) 116 | return queueArray.length; 117 | 118 | int i=front; 119 | int sz=0; 120 | if(front<=rear) 121 | while(i<=rear) 122 | { 123 | sz++; 124 | i++; 125 | } 126 | else 127 | { 128 | while(i<=queueArray.length-1) 129 | { 130 | sz++; 131 | i++; 132 | } 133 | i=0; 134 | while(i<=rear) 135 | { 136 | sz++; 137 | i++; 138 | } 139 | } 140 | return sz; 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /queue/circularQueue/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package circularQueue; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,x; 15 | Scanner scan = new Scanner(System.in); 16 | 17 | CircularQueue qu = new CircularQueue(8); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Insert an element in the queue"); 22 | System.out.println("2.Delete an element from the queue"); 23 | System.out.println("3.Display element at the front"); 24 | System.out.println("4.Display all elements of the queue"); 25 | System.out.println("5.Display size of the queue"); 26 | System.out.println("6.Quit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | if(choice==6) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | System.out.print("Enter the element to be inserted : "); 36 | x = scan.nextInt(); 37 | qu.insert(x); 38 | break; 39 | case 2: 40 | x=qu.Delete(); 41 | System.out.println("Element deleted is : " + x); 42 | break; 43 | case 3: 44 | System.out.println("Element at the front is : " + qu.peek()); 45 | break; 46 | case 4: 47 | qu.display(); 48 | break; 49 | case 5: 50 | System.out.println("Size of queue is " + qu.size()); 51 | break; 52 | default: 53 | System.out.println("Wrong choice"); 54 | } 55 | System.out.println(); 56 | 57 | } 58 | scan.close(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /queue/deque/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package deque; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,x; 15 | 16 | Scanner scan = new Scanner(System.in); 17 | DequeA dq = new DequeA(8); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Insert at the front end"); 22 | System.out.println("2.Insert at the rear end"); 23 | System.out.println("3.Delete from front end"); 24 | System.out.println("4.Delete from rear end"); 25 | System.out.println("5.Display all elements of deque"); 26 | System.out.println("6.Quit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | 30 | if(choice==6) 31 | break; 32 | 33 | switch(choice) 34 | { 35 | case 1: 36 | System.out.print("Enter the element to be inserted : "); 37 | x = scan.nextInt(); 38 | dq.insertFront(x); 39 | break; 40 | case 2: 41 | System.out.print("Enter the element to be inserted : "); 42 | x = scan.nextInt(); 43 | dq.insertRear(x); 44 | break; 45 | case 3: 46 | System.out.println("Element deleted from front end is " + dq.deleteFront()); 47 | break; 48 | case 4: 49 | System.out.println("Element deleted from rear end is " + dq.deleteRear()); 50 | break; 51 | case 5: 52 | dq.display(); 53 | break; 54 | default: 55 | System.out.println("Wrong choice"); 56 | } 57 | } 58 | scan.close(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /queue/deque/DequeA.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package deque; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | public class DequeA 11 | { 12 | private int[] queueArray; 13 | private int front; 14 | private int rear; 15 | 16 | public DequeA() 17 | { 18 | queueArray = new int[10]; 19 | front = -1; 20 | rear = -1; 21 | } 22 | public DequeA(int maxSize) 23 | { 24 | queueArray = new int[maxSize]; 25 | front = -1; 26 | rear = -1; 27 | } 28 | public void insertFront(int x) 29 | { 30 | if(isFull()) 31 | { 32 | System.out.println("Queue Overflow"); 33 | return; 34 | } 35 | if(front==-1) 36 | { 37 | front=0; 38 | rear=0; 39 | } 40 | else if(front==0) 41 | front=queueArray.length-1; 42 | else 43 | front=front-1; 44 | queueArray[front]=x; 45 | } 46 | 47 | public void insertRear(int x) 48 | { 49 | if(isFull()) 50 | { 51 | System.out.println("Queue Overflow"); 52 | return; 53 | } 54 | if(front==-1) 55 | front=0; 56 | 57 | if(rear==queueArray.length-1) 58 | rear=0; 59 | else 60 | rear=rear+1; 61 | queueArray[rear]=x; 62 | } 63 | 64 | public int deleteFront() 65 | { 66 | int x; 67 | if( isEmpty() ) 68 | { 69 | System.out.println("Queue Underflow"); 70 | throw new NoSuchElementException(); 71 | } 72 | x=queueArray[front]; 73 | 74 | if(front==rear) /*only one element*/ 75 | { 76 | front=-1; 77 | rear=-1; 78 | } 79 | else if(front==queueArray.length-1) 80 | front=0; 81 | else 82 | front=front+1; 83 | return x; 84 | } 85 | 86 | public int deleteRear() 87 | { 88 | int x; 89 | if(isEmpty()) 90 | { 91 | System.out.println("Queue Underflow"); 92 | throw new NoSuchElementException(); 93 | } 94 | x=queueArray[rear]; 95 | 96 | if(front==rear) /*only one element*/ 97 | { 98 | front=-1; 99 | rear=-1; 100 | } 101 | else if(rear==0) 102 | rear=queueArray.length-1; 103 | else 104 | rear=rear-1; 105 | return x; 106 | } 107 | 108 | public boolean isFull() 109 | { 110 | return ((front==0 && rear==queueArray.length-1) || (front==rear+1)); 111 | } 112 | 113 | public boolean isEmpty() 114 | { 115 | return (front==-1); 116 | } 117 | 118 | public void display() 119 | { 120 | int i; 121 | 122 | if(isEmpty()) 123 | { 124 | System.out.println("Queue is empty"); 125 | return; 126 | } 127 | 128 | System.out.println("Queue is : "); 129 | i=front; 130 | if(front<=rear) 131 | { 132 | while(i<=rear) 133 | System.out.print(queueArray[i++] + " "); 134 | } 135 | else 136 | { 137 | while(i<=queueArray.length-1) 138 | System.out.print(queueArray[i++] + " "); 139 | i=0; 140 | while(i<=rear) 141 | System.out.print(queueArray[i++] + " "); 142 | } 143 | System.out.println(); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /queue/priorityQueue/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package priorityQueue; 7 | import java.util.Scanner; 8 | 9 | public class Demo 10 | { 11 | public static void main(String[] args) 12 | { 13 | int choice,element,elementPriority; 14 | Scanner scan = new Scanner(System.in); 15 | 16 | PriorityQueueL pq = new PriorityQueueL(); 17 | 18 | while(true) 19 | { 20 | System.out.println("1.Insert a new element"); 21 | System.out.println("2.Delete an element"); 22 | System.out.println("3.Display the queue"); 23 | System.out.println("4.Quit"); 24 | System.out.print("Enter your choice : "); 25 | choice = scan.nextInt(); 26 | if(choice==4) 27 | break; 28 | 29 | switch(choice) 30 | { 31 | case 1: 32 | System.out.println("Enter the element to be inserted : "); 33 | element = scan.nextInt(); 34 | System.out.println("Enter its priority : "); 35 | elementPriority = scan.nextInt(); 36 | pq.insert(element, elementPriority); 37 | break; 38 | case 2: 39 | System.out.println("Deleted element is " + pq.Delete()); 40 | break; 41 | case 3: 42 | pq.display(); 43 | break; 44 | default : 45 | System.out.println("Wrong choice"); 46 | } 47 | } 48 | scan.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /queue/priorityQueue/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package priorityQueue; 7 | 8 | public class Node 9 | { 10 | public int priority; 11 | public int info; 12 | public Node link; 13 | 14 | public Node(int i, int pr) 15 | { 16 | info=i; 17 | priority=pr; 18 | link=null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /queue/priorityQueue/PriorityQueueL.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package priorityQueue; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | public class PriorityQueueL 11 | { 12 | private Node front; 13 | 14 | public PriorityQueueL() 15 | { 16 | front=null; 17 | } 18 | 19 | public void insert(int element,int elementPriority) 20 | { 21 | Node temp,p; 22 | 23 | temp = new Node(element,elementPriority); 24 | 25 | /*Queue is empty or element to be added has priority more than first element*/ 26 | if( isEmpty() || elementPriority < front.priority ) 27 | { 28 | temp.link=front; 29 | front=temp; 30 | } 31 | else 32 | { 33 | p=front; 34 | while( p.link!=null && p.link.priority<=elementPriority ) 35 | p=p.link; 36 | temp.link=p.link; 37 | p.link=temp; 38 | } 39 | } 40 | 41 | public int Delete() 42 | { 43 | int element; 44 | if(isEmpty()) 45 | { 46 | System.out.println("Queue Underflow"); 47 | throw new NoSuchElementException(); 48 | } 49 | else 50 | { 51 | element=front.info; 52 | front=front.link; 53 | } 54 | return element; 55 | } 56 | 57 | public boolean isEmpty() 58 | { 59 | return (front==null); 60 | } 61 | 62 | public void display() 63 | { 64 | Node p=front; 65 | if(isEmpty()) 66 | System.out.println("Queue is empty\n"); 67 | else 68 | { 69 | System.out.println("Queue is :"); 70 | System.out.println("Element Priority"); 71 | while(p!=null) 72 | { 73 | System.out.println(p.info + " " + p.priority ); 74 | p=p.link; 75 | } 76 | } 77 | System.out.println(""); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /queue/queueArray/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueArray; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,x; 15 | Scanner scan = new Scanner(System.in); 16 | 17 | QueueA qu = new QueueA(8); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Insert an element in the queue"); 22 | System.out.println("2.Delete an element from the queue"); 23 | System.out.println("3.Display element at the front"); 24 | System.out.println("4.Display all elements of the queue"); 25 | System.out.println("5.Display size of the queue"); 26 | System.out.println("6.Quit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | if(choice==6) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | System.out.print("Enter the element to be inserted : "); 36 | x = scan.nextInt(); 37 | qu.insert(x); 38 | break; 39 | case 2: 40 | x=qu.Delete(); 41 | System.out.println("Element deleted is : " + x); 42 | break; 43 | case 3: 44 | System.out.println("Element at the front is : " + qu.peek()); 45 | break; 46 | case 4: 47 | qu.display(); 48 | break; 49 | case 5: 50 | System.out.println("Size of queue is " + qu.size()); 51 | break; 52 | default: 53 | System.out.println("Wrong choice"); 54 | } 55 | System.out.println(); 56 | 57 | } 58 | scan.close(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /queue/queueArray/QueueA.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueArray; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | public class QueueA 11 | { 12 | private int[] queueArray; 13 | private int front; 14 | private int rear; 15 | 16 | public QueueA() 17 | { 18 | queueArray = new int[10]; 19 | front = -1; 20 | rear = -1; 21 | } 22 | public QueueA(int maxSize) 23 | { 24 | queueArray = new int[maxSize]; 25 | front = -1; 26 | rear = -1; 27 | } 28 | public boolean isEmpty() 29 | { 30 | return (front==-1 || front==rear+1); 31 | } 32 | 33 | public boolean isFull() 34 | { 35 | return (rear == queueArray.length - 1 ); 36 | } 37 | 38 | public int size() 39 | { 40 | if(isEmpty()) 41 | return 0; 42 | else 43 | return rear-front+1; 44 | } 45 | 46 | public void insert(int x) 47 | { 48 | if(isFull()) 49 | { 50 | System.out.println("Queue Overflow\n"); 51 | return; 52 | } 53 | if(front==-1) 54 | front=0; 55 | rear=rear+1; 56 | queueArray[rear]=x; 57 | } 58 | 59 | public int Delete() 60 | { 61 | int x; 62 | if(isEmpty()) 63 | { 64 | System.out.println("Queue Underflow"); 65 | throw new NoSuchElementException(); 66 | } 67 | x=queueArray[front]; 68 | front=front+1; 69 | return x; 70 | } 71 | 72 | public int peek() 73 | { 74 | if(isEmpty()) 75 | { 76 | System.out.println("Queue Underflow\n"); 77 | throw new NoSuchElementException(); 78 | } 79 | return queueArray[front]; 80 | } 81 | 82 | public void display() 83 | { 84 | int i; 85 | 86 | if(isEmpty()) 87 | { 88 | System.out.println("Queue is empty\n"); 89 | return; 90 | } 91 | 92 | System.out.println("Queue is :\n\n"); 93 | for(i=front; i<=rear; i++) 94 | System.out.print(queueArray[i] + " "); 95 | 96 | System.out.println(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /queue/queueCircularLinked/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueCircularLinked; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,x; 15 | Scanner scan = new Scanner(System.in); 16 | 17 | QueueCL qu = new QueueCL(); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Insert an element in the queue"); 22 | System.out.println("2.Delete an element from the queue"); 23 | System.out.println("3.Display element at the front"); 24 | System.out.println("4.Display all elements of the queue"); 25 | System.out.println("5.Display size of the queue"); 26 | System.out.println("6.Quit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | if(choice==6) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | System.out.print("Enter the element to be inserted : "); 36 | x = scan.nextInt(); 37 | qu.insert(x); 38 | break; 39 | case 2: 40 | x=qu.Delete(); 41 | System.out.println("Element deleted is : " + x); 42 | break; 43 | case 3: 44 | System.out.println("Element at the front is : " + qu.peek()); 45 | break; 46 | case 4: 47 | qu.display(); 48 | break; 49 | case 5: 50 | System.out.println("Size of queue is " + qu.size()); 51 | break; 52 | default: 53 | System.out.println("Wrong choice"); 54 | } 55 | System.out.println(); 56 | } 57 | scan.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /queue/queueCircularLinked/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueCircularLinked; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /queue/queueCircularLinked/QueueCL.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueCircularLinked; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | public class QueueCL 11 | { 12 | private Node rear; 13 | 14 | public QueueCL() 15 | { 16 | rear=null; 17 | } 18 | 19 | public boolean isEmpty() 20 | { 21 | return (rear==null); 22 | } 23 | 24 | public void insert(int x) 25 | { 26 | Node temp = new Node(x); 27 | 28 | if(isEmpty()) 29 | { 30 | rear=temp; 31 | rear.link=rear; 32 | } 33 | else 34 | { 35 | temp.link=rear.link; 36 | rear.link=temp; 37 | rear=temp; 38 | } 39 | } 40 | 41 | public int Delete() 42 | { 43 | if(isEmpty()) 44 | { 45 | System.out.println("Queue Underflow"); 46 | throw new NoSuchElementException(); 47 | } 48 | Node temp; 49 | if(rear.link==rear) /*If only one element*/ 50 | { 51 | temp=rear; 52 | rear=null; 53 | } 54 | else 55 | { 56 | temp=rear.link; 57 | rear.link=temp.link; 58 | } 59 | return temp.info; 60 | } 61 | 62 | public int peek() 63 | { 64 | if(isEmpty()) 65 | { 66 | System.out.println("Queue Underflow"); 67 | throw new NoSuchElementException(); 68 | } 69 | return rear.link.info; 70 | } 71 | 72 | public void display() 73 | { 74 | if(isEmpty()) 75 | { 76 | System.out.println("Queue is empty"); 77 | return; 78 | } 79 | 80 | System.out.println("Queue is : "); 81 | Node p=rear.link; 82 | do 83 | { 84 | System.out.print(p.info + " "); 85 | p=p.link; 86 | }while(p!=rear.link); 87 | System.out.println(); 88 | } 89 | 90 | public int size() 91 | { 92 | if(isEmpty()) 93 | return 0; 94 | int s=0; 95 | Node p=rear.link; 96 | do 97 | { 98 | s++; 99 | p=p.link; 100 | }while(p!=rear.link); 101 | return s; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /queue/queueLinked/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueLinked; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,x; 15 | Scanner scan = new Scanner(System.in); 16 | 17 | QueueL qu = new QueueL(); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Insert an element in the queue"); 22 | System.out.println("2.Delete an element from the queue"); 23 | System.out.println("3.Display element at the front"); 24 | System.out.println("4.Display all elements of the queue"); 25 | System.out.println("5.Display size of the queue"); 26 | System.out.println("6.Quit"); 27 | System.out.print("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | if(choice==6) 30 | break; 31 | 32 | switch(choice) 33 | { 34 | case 1: 35 | System.out.print("Enter the element to be inserted : "); 36 | x = scan.nextInt(); 37 | qu.insert(x); 38 | break; 39 | case 2: 40 | x=qu.Delete(); 41 | System.out.println("Element deleted is : " + x); 42 | break; 43 | case 3: 44 | System.out.println("Element at the front is : " + qu.peek()); 45 | break; 46 | case 4: 47 | qu.display(); 48 | break; 49 | case 5: 50 | System.out.println("Size of queue is " + qu.size()); 51 | break; 52 | default: 53 | System.out.println("Wrong choice"); 54 | } 55 | System.out.println(); 56 | } 57 | scan.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /queue/queueLinked/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueLinked; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /queue/queueLinked/QueueL.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package queueLinked; 7 | 8 | import java.util.NoSuchElementException; 9 | 10 | public class QueueL 11 | { 12 | private Node front; 13 | private Node rear; 14 | 15 | public QueueL() 16 | { 17 | front = null; 18 | rear = null; 19 | } 20 | public int size() 21 | { 22 | int s=0; 23 | Node p=front; 24 | while(p!=null) 25 | { 26 | s++; 27 | p=p.link; 28 | } 29 | return s; 30 | } 31 | 32 | public void insert(int x) 33 | { 34 | Node temp; 35 | temp = new Node(x); 36 | 37 | if(front==null) /*If Queue is empty*/ 38 | front=temp; 39 | else 40 | rear.link=temp; 41 | rear=temp; 42 | } 43 | 44 | public int Delete() 45 | { 46 | int x; 47 | if(isEmpty()) 48 | { 49 | System.out.println("Queue Underflow"); 50 | throw new NoSuchElementException(); 51 | } 52 | x=front.info; 53 | front=front.link; 54 | return x; 55 | } 56 | 57 | public int peek() 58 | { 59 | if(isEmpty()) 60 | { 61 | System.out.println("Queue Underflow"); 62 | throw new NoSuchElementException(); 63 | } 64 | return front.info; 65 | } 66 | 67 | public boolean isEmpty() 68 | { 69 | return (front==null); 70 | } 71 | 72 | public void display() 73 | { 74 | Node p=front; 75 | if(isEmpty()) 76 | { 77 | System.out.println("Queue is empty"); 78 | return; 79 | } 80 | 81 | System.out.println("Queue is : "); 82 | while(p!=null) 83 | { 84 | System.out.print(p.info + " "); 85 | p=p.link; 86 | } 87 | System.out.println(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /recursion/BaseConversion.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class BaseConversion 9 | { 10 | BaseConversion() {} //this class is not for instantiation 11 | 12 | public static void toBinary(int n) 13 | { 14 | if(n==0) 15 | return; 16 | toBinary(n/2); 17 | System.out.print(n%2); 18 | } 19 | 20 | public static void convertBase(int n,int base) 21 | { 22 | if(n==0) 23 | return; 24 | convertBase(n/base,base); 25 | 26 | int remainder=n%base; 27 | if(remainder<10) 28 | System.out.print(remainder); 29 | else 30 | System.out.print((char) (remainder-10+'A')); 31 | } 32 | 33 | 34 | public static void main(String[] args) 35 | { 36 | int n; 37 | Scanner scan = new Scanner(System.in); 38 | 39 | System.out.print("Enter a positive decimal number : "); 40 | n=scan.nextInt(); 41 | 42 | System.out.print("Binary form : "); toBinary(n); 43 | System.out.println(); 44 | System.out.print("Binary form : "); convertBase(n,2); 45 | System.out.println(); 46 | System.out.print("Octal form : "); convertBase(n,8); 47 | System.out.println(); 48 | System.out.print("Hexadecimal form : "); convertBase(n,16); 49 | System.out.println(); 50 | scan.close(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /recursion/Display1toN.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | 9 | public class Display1toN 10 | { 11 | public static void print1(int n) 12 | { 13 | if(n==0) 14 | return; 15 | System.out.print(n + " "); 16 | print1(n-1); 17 | } 18 | 19 | public static void print2(int n) 20 | { 21 | if(n==0) 22 | return; 23 | print2(n-1); 24 | System.out.print(n + " "); 25 | } 26 | 27 | public static void main(String[] args) 28 | { 29 | Scanner scan = new Scanner(System.in); 30 | int n; 31 | System.out.println("Enter value of n : "); 32 | n = scan.nextInt(); 33 | 34 | print1(n); 35 | System.out.println(); 36 | 37 | print2(n); 38 | System.out.println(); 39 | 40 | scan.close(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /recursion/Euclids.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class Euclids 9 | { 10 | private Euclids(){} //this class is not for instantiation 11 | 12 | public static int GCD(int a, int b) 13 | { 14 | if(b==0) 15 | return a; 16 | return GCD(b, a%b); 17 | } 18 | 19 | public static void main(String[] args) 20 | { 21 | int a,b; 22 | Scanner scan = new Scanner(System.in); 23 | System.out.print("Enter values for a and b : "); 24 | a = scan.nextInt(); 25 | b = scan.nextInt(); 26 | System.out.println("GCD of " + a + " and " + b + " is " + GCD(a,b)); 27 | scan.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /recursion/Exponentiation.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | 9 | public class Exponentiation 10 | { 11 | private Exponentiation() {} //this class is not for instantiation 12 | 13 | public static float power(float x,int n) 14 | { 15 | if(n==0) 16 | return 1; 17 | return x*power(x,n-1); 18 | } 19 | 20 | public static void main(String[] args) 21 | { 22 | Scanner scan = new Scanner(System.in); 23 | 24 | float x; 25 | int n; 26 | 27 | System.out.print("Enter values for x and n : "); 28 | x = scan.nextFloat(); 29 | n = scan.nextInt(); 30 | 31 | System.out.println(x + "^" + n + "=" + power(x,n)); 32 | 33 | scan.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /recursion/Factorial.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class Factorial 9 | { 10 | private Factorial(){} //this class is not for instantiation 11 | 12 | public static long factorial(int n) 13 | { 14 | if(n==0) 15 | return 1; 16 | return n*factorial(n-1); 17 | } 18 | 19 | public static void main(String[] args) 20 | { 21 | Scanner scan = new Scanner(System.in); 22 | 23 | int n; 24 | System.out.println("Enter a number greater than or equal to zero : "); 25 | n=scan.nextInt(); 26 | 27 | System.out.println("Factorial of " + n + " is " + factorial(n)); 28 | scan.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /recursion/Fibonacci.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class Fibonacci 9 | { 10 | private Fibonacci() {} //this class is not for instantiation 11 | 12 | public static int fib(int n) 13 | { 14 | if(n==0 ) 15 | return 0; 16 | if(n==1) 17 | return 1; 18 | return fib(n-1) + fib(n-2); 19 | } 20 | 21 | public static void main(String[] args) 22 | { 23 | Scanner scan = new Scanner(System.in); 24 | 25 | System.out.println("Enter number of terms : "); 26 | int terms = scan.nextInt(); 27 | 28 | for(int i=0; i<=terms; i++) 29 | System.out.print(fib(i)+" "); 30 | scan.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /recursion/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Java 2 | 3 | This [“Data Structures and Algorithms in Java”](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 16 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 19 | 20 | 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /recursion/SumOfDigits.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class SumOfDigits 9 | { 10 | private SumOfDigits() {} //this class is not for instantiation 11 | 12 | public static long sumDigits(long n) 13 | { 14 | if(n/10==0) 15 | return n; 16 | return sumDigits(n/10) + n%10; 17 | } 18 | 19 | public static void main(String[] args) 20 | { 21 | Scanner scan = new Scanner(System.in); 22 | 23 | long n; 24 | System.out.print("Enter a number : "); 25 | n = scan.nextLong(); 26 | System.out.println(sumDigits(n)); 27 | scan.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /recursion/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | 9 | public class TowerOfHanoi 10 | { 11 | private TowerOfHanoi(){} //this class is not for instantiation 12 | 13 | public static void hanoi(int n, char source, char temp, char dest) 14 | { 15 | if(n==1) 16 | { 17 | System.out.println("Move Disk " + n + " from " + source + "-->" + dest); 18 | return; 19 | } 20 | hanoi(n-1, source, dest, temp); 21 | System.out.println("Move Disk " + n + " from " + source + "-->" + dest); 22 | hanoi(n-1, temp, source, dest); 23 | } 24 | 25 | public static void main(String[] args) 26 | { 27 | int n; 28 | Scanner scan = new Scanner(System.in); 29 | 30 | System.out.print("Enter the number of disks : "); 31 | n = scan.nextInt(); 32 | 33 | hanoi(n, 'A', 'B', 'C'); 34 | scan.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /searching/BinarySearch.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class BinarySearch 9 | { 10 | private BinarySearch(){} //this class is not for instantiation 11 | 12 | public static int search(int[] a, int n, int searchValue) 13 | { 14 | int first=0, last=n-1, mid; 15 | 16 | while( first <= last ) 17 | { 18 | mid = (first + last)/2; 19 | 20 | if( searchValue < a[mid] ) 21 | last = mid-1; /* Search in left half */ 22 | else if( searchValue > a[mid] ) 23 | first = mid+1; /* Search in right half */ 24 | else 25 | return mid; /* searchValue present at index mid */ 26 | } 27 | return -1; 28 | } 29 | 30 | public static void main(String[] args) 31 | { 32 | int i,n,searchValue, index; 33 | 34 | int[] a = new int[100]; 35 | 36 | Scanner scan = new Scanner(System.in); 37 | 38 | System.out.print("Enter the number of elements : "); 39 | n = scan.nextInt(); 40 | 41 | System.out.println("Enter the elements in sorted order - "); 42 | for(i=0; ilast) 21 | return -1; 22 | 23 | int mid = (first+last)/2; 24 | 25 | if(searchValue > a[mid]) /*Search in right half */ 26 | return search(a,mid+1,last,searchValue); 27 | else if(searchValue < a[mid]) /*Search in left half */ 28 | return search(a,first,mid-1,searchValue); 29 | else 30 | return mid; 31 | } 32 | 33 | public static void main(String[] args) 34 | { 35 | int i,n,searchValue, index; 36 | 37 | int[] a = new int[100]; 38 | 39 | Scanner scan = new Scanner(System.in); 40 | 41 | System.out.print("Enter the number of elements : "); 42 | n = scan.nextInt(); 43 | 44 | System.out.println("Enter the elements in sorted order - "); 45 | for(i=0; i= searchValue ) 18 | break; 19 | } 20 | if(a[i]==searchValue) 21 | return i; 22 | else 23 | return -1; 24 | } 25 | 26 | public static void main(String[] args) 27 | { 28 | int i,n,searchValue, index; 29 | 30 | int[] a = new int[100]; 31 | 32 | Scanner scan = new Scanner(System.in); 33 | 34 | System.out.print("Enter the number of elements : "); 35 | n = scan.nextInt(); 36 | 37 | System.out.println("Enter the elements in sorted order - "); 38 | for(i=0; i 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /sorting/BubbleSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class BubbleSort 9 | { 10 | private BubbleSort(){} //this class is not for instantiation 11 | 12 | public static void sort(int[] a, int n) 13 | { 14 | int x,j,temp,swaps; 15 | 16 | for(x=n-2; x>=0; x--) 17 | { 18 | swaps=0; 19 | for(j=0; j<=x; j++) 20 | { 21 | if(a[j] > a[j+1]) 22 | { 23 | temp = a[j]; 24 | a[j] = a[j+1]; 25 | a[j+1] = temp; 26 | swaps++; 27 | } 28 | } 29 | if(swaps==0) 30 | break; 31 | } 32 | } 33 | 34 | public static void main(String[] args) 35 | { 36 | int i,n; 37 | int[] a = new int[20]; 38 | Scanner scan = new Scanner(System.in); 39 | 40 | System.out.print("Enter the number of elements : "); 41 | n = scan.nextInt(); 42 | 43 | for(i=0; i1) 18 | { 19 | maxValue=a[1]; 20 | a[1]=a[n]; 21 | a[n]=maxValue; 22 | n--; 23 | restoreDown(1,a,n); 24 | } 25 | } 26 | 27 | public static void buildHeap_BottomUp(int[] arr, int n) 28 | { 29 | int i; 30 | for(i=n/2; i>=1; i--) 31 | restoreDown(i,arr,n); 32 | } 33 | 34 | private static void restoreDown(int i, int[] a, int n) 35 | { 36 | int k=a[i]; 37 | int lchild=2*i, rchild=lchild+1; 38 | 39 | while(rchild<=n) 40 | { 41 | if( k>=a[lchild] && k>=a[rchild] ) 42 | { 43 | a[i]=k; 44 | return; 45 | } 46 | else if(a[lchild] > a[rchild]) 47 | { 48 | a[i]=a[lchild]; 49 | i=lchild; 50 | } 51 | else 52 | { 53 | a[i]=a[rchild]; 54 | i=rchild; 55 | } 56 | lchild=2*i; 57 | rchild=lchild+1; 58 | } 59 | 60 | /*If number of nodes is even*/ 61 | if(lchild==n && k=0 && a[j]>temp; j--) 20 | a[j+1]=a[j]; 21 | 22 | a[j+1]=temp; 23 | } 24 | } 25 | 26 | public static void main(String[] args) 27 | { 28 | int i,n; 29 | int[] a = new int[20]; 30 | Scanner scan = new Scanner(System.in); 31 | 32 | System.out.print("Enter the number of elements : "); 33 | n = scan.nextInt(); 34 | 35 | for(i=0; i=n)/*if length of last sublist is less than size*/ 35 | up2=n-1; 36 | 37 | merge(a,temp,low1,up1,low2,up2); 38 | 39 | low1=up2+1; /*Take next two sublists for merging*/ 40 | } 41 | 42 | for(i=low1; i<=n-1; i++) 43 | temp[i]=a[i]; /*If any sublist is left alone*/ 44 | 45 | copy(a, temp, n); 46 | } 47 | 48 | 49 | /* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */ 50 | private static void merge( int a[], int temp[], int low1, int up1, int low2, int up2 ) 51 | { 52 | int i = low1; 53 | int j = low2; 54 | int k = low1; 55 | 56 | while( (i<=up1) && (j<=up2) ) 57 | { 58 | if(a[i]<=a[j]) 59 | temp[k++]=a[i++]; 60 | else 61 | temp[k++]=a[j++]; 62 | } 63 | 64 | while(i<=up1) 65 | temp[k++]=a[i++]; 66 | 67 | while(j<=up2) 68 | temp[k++]=a[j++]; 69 | } 70 | 71 | /*copies temp[low]....temp[up] to a[low]...a[up]*/ 72 | private static void copy(int a[], int temp[], int n) 73 | { 74 | for(int i=0; i=up) /*zero or one element in sublist*/ 20 | return; 21 | int p = partition(a,low,up); 22 | sort(a,low,p-1); /*Sort left sublist*/ 23 | sort(a,p+1,up); /*Sort right sublist*/ 24 | } 25 | 26 | private static int partition(int[] a, int low, int up) 27 | { 28 | int temp,i,j,pivot; 29 | 30 | pivot=a[low]; 31 | 32 | i=low+1; /*moves from left to right*/ 33 | j=up; /*moves from right to left*/ 34 | 35 | while(i<=j) 36 | { 37 | while( a[i] pivot ) 41 | j--; 42 | 43 | if(i 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /sorting/SelectionSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class SelectionSort 9 | { 10 | private SelectionSort(){} //this class is not for instantiation 11 | 12 | public static void sort(int[] a, int n) 13 | { 14 | int minIndex,temp,i,j; 15 | 16 | for(i=0; i=1) 21 | { 22 | for(i=h; i=0 && a[j]>temp; j=j-h) 26 | a[j+h]=a[j]; 27 | a[j+h]=temp; 28 | } 29 | h=h-2; 30 | } 31 | scan.close(); 32 | } 33 | 34 | public static void main(String[] args) 35 | { 36 | int i,n; 37 | int[] a = new int[20]; 38 | Scanner scan = new Scanner(System.in); 39 | 40 | System.out.print("Enter the number of elements : "); 41 | n = scan.nextInt(); 42 | 43 | for(i=0; i large) 22 | large = a[i]; 23 | } 24 | 25 | for(i=0; i p.info)/*search in right subtree*/ 65 | return search(p.rchild, x); 66 | return p; /*key found*/ 67 | } 68 | 69 | public void delete(int x) 70 | { 71 | root = delete(root,x); 72 | } 73 | 74 | private TreeNode delete(TreeNode p, int x) 75 | { 76 | TreeNode ch,s; 77 | 78 | if(p==null) 79 | { 80 | System.out.println(x + " not found"); 81 | return p; 82 | } 83 | if(x < p.info) /*delete from left subtree*/ 84 | p.lchild = delete(p.lchild, x); 85 | else if(x > p.info) /*delete from right subtree*/ 86 | p.rchild = delete(p.rchild, x); 87 | else 88 | { 89 | /*key to be deleted is found*/ 90 | if( p.lchild!=null && p.rchild!=null ) /*2 children*/ 91 | { 92 | s=p.rchild; 93 | while(s.lchild!=null) 94 | s=s.lchild; 95 | p.info=s.info; 96 | p.rchild = delete(p.rchild,s.info); 97 | } 98 | else /*1 child or no child*/ 99 | { 100 | if(p.lchild != null) /*only left child*/ 101 | ch=p.lchild; 102 | else /*only right child or no child*/ 103 | ch=p.rchild; 104 | p=ch; 105 | } 106 | } 107 | return p; 108 | } 109 | 110 | public int min() 111 | { 112 | if(isEmpty()) 113 | throw new NullPointerException("Tree is empty"); 114 | return min(root).info; 115 | } 116 | 117 | private TreeNode min(TreeNode p) 118 | { 119 | if(p.lchild==null) 120 | return p; 121 | return min(p.lchild); 122 | } 123 | 124 | public int max() 125 | { 126 | if(isEmpty()) 127 | throw new NullPointerException("Tree is empty"); 128 | return max(root).info; 129 | } 130 | 131 | private TreeNode max(TreeNode p) 132 | { 133 | if(p.rchild==null) 134 | return p; 135 | return max(p.rchild); 136 | } 137 | 138 | public void display() 139 | { 140 | display(root,0); 141 | System.out.println(); 142 | } 143 | private void display(TreeNode p,int level) 144 | { 145 | int i; 146 | if(p==null) 147 | return; 148 | 149 | display(p.rchild, level+1); 150 | System.out.println(); 151 | 152 | for(i=0; i hR) 221 | return 1+hL; 222 | else 223 | return 1+hR; 224 | }/*End of height()*/ 225 | 226 | public void insert1(int x) 227 | { 228 | TreeNode temp,par,p; 229 | 230 | p=root; 231 | par=null; 232 | 233 | while(p!=null) 234 | { 235 | par=p; 236 | if(x < p.info) 237 | p=p.lchild; 238 | else if(x > p.info) 239 | p=p.rchild; 240 | else 241 | System.out.println(x + " already present in the tree"); 242 | } 243 | 244 | temp=new TreeNode(x); 245 | 246 | if(par==null) 247 | root=temp; 248 | else if(x < par.info) 249 | par.lchild=temp; 250 | else 251 | par.rchild=temp; 252 | 253 | } 254 | 255 | public boolean search1(int x) 256 | { 257 | TreeNode p = root; 258 | while(p!=null) 259 | { 260 | if(x < p.info) 261 | p=p.lchild; /*Move to left child*/ 262 | else if(x > p.info) 263 | p=p.rchild; /*Move to right child */ 264 | else /*x found*/ 265 | return true; 266 | } 267 | return false; 268 | } 269 | 270 | public void delete1(int x) 271 | { 272 | TreeNode par,p,ch,s,ps; 273 | 274 | p=root; 275 | par=null; 276 | while(p!=null) 277 | { 278 | if(x==p.info) 279 | break; 280 | par=p; 281 | if(x < p.info) 282 | p=p.lchild; 283 | else 284 | p=p.rchild; 285 | } 286 | 287 | if(p==null) 288 | System.out.println(x + " not found\n"); 289 | 290 | /*Case C: 2 children*/ 291 | /*Find inorder successor and its parent*/ 292 | if(p.lchild!=null && p.rchild!=null) 293 | { 294 | ps=p; 295 | s=p.rchild; 296 | while(s.lchild!=null) 297 | { 298 | ps=s; 299 | s=s.lchild; 300 | } 301 | p.info=s.info; 302 | p=s; 303 | par=ps; 304 | } 305 | 306 | /*Case B and Case A : 1 or no child*/ 307 | if(p.lchild!=null) /*TreeNode to be deleted has left child */ 308 | ch=p.lchild; 309 | else /*TreeNode to be deleted has right child or no child*/ 310 | ch=p.rchild; 311 | 312 | if(par==null) /*TreeNode to be deleted is root TreeNode*/ 313 | root=ch; 314 | else if(p==par.lchild)/*TreeNode is left child of its parent*/ 315 | par.lchild=ch; 316 | else /*TreeNode is right child of its parent*/ 317 | par.rchild=ch; 318 | } 319 | 320 | int min1() 321 | { 322 | if(isEmpty()) 323 | { 324 | System.out.println("Tree is empty"); 325 | return -1; 326 | } 327 | TreeNode p=root; 328 | while(p.lchild!=null) 329 | p=p.lchild; 330 | return p.info; 331 | } 332 | 333 | int max1() 334 | { 335 | if(isEmpty()) 336 | { 337 | System.out.println("Tree is empty"); 338 | return -1; 339 | } 340 | TreeNode p=root; 341 | while(p.rchild!=null) 342 | p=p.rchild; 343 | return p.info; 344 | } 345 | 346 | 347 | 348 | } 349 | -------------------------------------------------------------------------------- /sorting/bianry-tree-sort/BinaryTreeSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | 9 | public class BinaryTreeSort 10 | { 11 | private BinaryTreeSort(){} //this class is not for instantiation 12 | 13 | public static void sort(int[] a, int n) 14 | { 15 | BinarySearchTree tree = new BinarySearchTree(); 16 | 17 | for(int i=0; i large) 69 | large = p.info; 70 | p=p.link; 71 | } 72 | 73 | /*Find number of digits in largest element*/ 74 | int ndigits=0; 75 | while(large!=0) 76 | { 77 | ndigits++; 78 | large/=10; 79 | } 80 | return ndigits; 81 | } 82 | 83 | /*Returns kth digit from right in n*/ 84 | public static int digit(int n, int k) 85 | { 86 | int d=0,i; 87 | for(i=1; i<=k; i++) 88 | { 89 | d=n%10; 90 | n/=10; 91 | } 92 | return d; 93 | } 94 | 95 | public static void main(String[] args) 96 | { 97 | Node temp,p; 98 | int i,n,data; 99 | 100 | Scanner scan = new Scanner(System.in); 101 | 102 | System.out.println("Enter number of elements in the list : "); 103 | n = scan.nextInt(); 104 | 105 | Node start=null; 106 | for(i=1; i<=n; i++) /*Inserting elements in linked list*/ 107 | { 108 | System.out.print("Enter element " + i + " "); 109 | data = scan.nextInt(); 110 | 111 | temp = new Node(data); 112 | if(start==null) 113 | start=temp; 114 | else 115 | { 116 | p=start; 117 | while(p.link!=null) 118 | p=p.link; 119 | p.link=temp; 120 | } 121 | } 122 | 123 | start = sort(start); 124 | 125 | System.out.println("Sorted list is :\n"); 126 | p=start; 127 | while(p!=null) 128 | { 129 | System.out.print(p.info + " "); 130 | p=p.link; 131 | } 132 | System.out.println(); 133 | scan.close(); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /stack/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Java 2 | 3 | This [“Data Structures and Algorithms in Java”](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 16 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 19 | 20 | 30 | 31 | ## Copyright 32 | © Copyright Deepali Srivastava : All rights reserved. 33 | Not to be used for commercial purposes. 34 | -------------------------------------------------------------------------------- /stack/parentheses/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package parentheses; 7 | import java.util.Scanner; 8 | 9 | public class Demo 10 | { 11 | public static void main(String[] args) 12 | { 13 | String expression; 14 | 15 | Scanner scan = new Scanner(System.in); 16 | 17 | System.out.print("Enter an expression with parentheses : "); 18 | expression = scan.nextLine(); 19 | 20 | if(isValid(expression)) 21 | System.out.println("Valid expression"); 22 | else 23 | System.out.println("Invalid expression"); 24 | scan.close(); 25 | } 26 | 27 | 28 | public static boolean isValid(String expr) 29 | { 30 | StackA st = new StackA(); 31 | 32 | char ch; 33 | for(int i=0; i=0; i--) 89 | System.out.println(stackArray[i] + " "); 90 | System.out.println(); 91 | } 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /stack/postfix/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package postfix; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | String infix; 15 | 16 | Scanner scan = new Scanner(System.in); 17 | 18 | System.out.print("Enter infix expression : "); 19 | infix = scan.nextLine(); 20 | 21 | String postfix = infixToPostfix(infix); 22 | 23 | System.out.println("Postfix expression is : " + postfix); 24 | 25 | System.out.println("Value of expression : " + evaluatePostfix(postfix)); 26 | 27 | scan.close(); 28 | } 29 | 30 | public static String infixToPostfix(String infix) 31 | { 32 | String postfix = new String(); 33 | 34 | StackChar st = new StackChar(20); 35 | 36 | char next,symbol; 37 | for(int i=0; i= precedence(symbol) ) 60 | postfix = postfix + st.pop(); 61 | st.push(symbol); 62 | break; 63 | default: /*operand*/ 64 | postfix = postfix + symbol; 65 | } 66 | } 67 | while(!st.isEmpty()) 68 | postfix = postfix + st.pop(); 69 | return postfix; 70 | } 71 | 72 | public static int precedence(char symbol) 73 | { 74 | switch(symbol) 75 | { 76 | case '(': 77 | return 0; 78 | case '+': 79 | case '-': 80 | return 1; 81 | case '*': 82 | case '/': 83 | case '%': 84 | return 2; 85 | case '^': 86 | return 3; 87 | default : 88 | return 0; 89 | } 90 | } 91 | 92 | public static int evaluatePostfix(String postfix) 93 | { 94 | StackInt st = new StackInt(20); 95 | 96 | int x,y; 97 | for(int i=0; i=0; i--) 89 | System.out.println(stackArray[i] + " "); 90 | System.out.println(); 91 | } 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /stack/postfix/StackInt.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package postfix; 7 | 8 | import java.util.EmptyStackException; 9 | 10 | public class StackInt 11 | { 12 | private int[] stackArray; 13 | private int top; 14 | 15 | public StackInt() 16 | { 17 | stackArray = new int[10]; 18 | top = -1; 19 | } 20 | 21 | public StackInt(int maxSize) 22 | { 23 | stackArray = new int[maxSize]; 24 | top=-1; 25 | } 26 | public int size() 27 | { 28 | return top+1; 29 | } 30 | 31 | public boolean isEmpty() 32 | { 33 | return (top==-1); 34 | } 35 | 36 | public boolean isFull() 37 | { 38 | return (top==stackArray.length-1); 39 | } 40 | 41 | public void push(int x) 42 | { 43 | if(isFull()) 44 | { 45 | System.out.println("Stack Overflow\n"); 46 | return; 47 | } 48 | top=top+1; 49 | stackArray[top]=x; 50 | } 51 | 52 | public int pop() 53 | { 54 | int x; 55 | if(isEmpty()) 56 | { 57 | System.out.println("Stack Underflow\n"); 58 | throw new EmptyStackException(); 59 | } 60 | x=stackArray[top]; 61 | top=top-1; 62 | return x; 63 | } 64 | 65 | public int peek() 66 | { 67 | if(isEmpty()) 68 | { 69 | System.out.println("Stack Underflow\n"); 70 | throw new EmptyStackException(); 71 | } 72 | return stackArray[top]; 73 | } 74 | 75 | 76 | public void display() 77 | { 78 | int i; 79 | 80 | System.out.println("top= " + top); 81 | 82 | if(isEmpty()) 83 | { 84 | System.out.println("Stack is empty"); 85 | return; 86 | } 87 | System.out.println("Stack is : "); 88 | for(i=top; i>=0; i--) 89 | System.out.println(stackArray[i] + " "); 90 | System.out.println(); 91 | } 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /stack/stackArray/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package stackArray; 7 | 8 | import java.util.Scanner; 9 | 10 | public class Demo 11 | { 12 | public static void main(String[] args) 13 | { 14 | int choice,x; 15 | Scanner scan = new Scanner(System.in); 16 | 17 | StackA st = new StackA(8); 18 | 19 | while(true) 20 | { 21 | System.out.println("1.Push an element on the stack"); 22 | System.out.println("2.Pop an element from the stack"); 23 | System.out.println("3.Display the top element"); 24 | System.out.println("4.Display all stack elements"); 25 | System.out.println("5.Display size of the stack"); 26 | System.out.println("6.Quit"); 27 | System.out.println("Enter your choice : "); 28 | choice = scan.nextInt(); 29 | 30 | if(choice==6) 31 | break; 32 | 33 | switch(choice) 34 | { 35 | case 1 : 36 | System.out.println("Enter the element to be pushed : "); 37 | x=scan.nextInt(); 38 | st.push(x); 39 | break; 40 | case 2: 41 | x=st.pop(); 42 | System.out.println("Popped element is : " + x); 43 | break; 44 | case 3: 45 | System.out.println("Element at the top is : " + st.peek()); 46 | break; 47 | case 4: 48 | st.display(); 49 | break; 50 | case 5: 51 | System.out.println("Size of stack " + st.size()); 52 | break; 53 | default: 54 | System.out.println("Wrong choice"); 55 | } 56 | System.out.println(""); 57 | } 58 | scan.close(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /stack/stackArray/StackA.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package stackArray; 7 | 8 | import java.util.EmptyStackException; 9 | 10 | public class StackA 11 | { 12 | private int[] stackArray; 13 | private int top; 14 | 15 | public StackA() 16 | { 17 | stackArray = new int[10]; 18 | top = -1; 19 | } 20 | 21 | public StackA(int maxSize) 22 | { 23 | stackArray = new int[maxSize]; 24 | top = -1; 25 | } 26 | 27 | public int size() 28 | { 29 | return top+1; 30 | } 31 | 32 | public boolean isEmpty() 33 | { 34 | return (top==-1); 35 | } 36 | 37 | public boolean isFull() 38 | { 39 | return (top==stackArray.length-1); 40 | } 41 | 42 | public void push(int x) 43 | { 44 | if(isFull()) 45 | { 46 | System.out.println("Stack Overflow"); 47 | return; 48 | } 49 | top=top+1; 50 | stackArray[top]=x; 51 | } 52 | 53 | public int pop() 54 | { 55 | int x; 56 | if(isEmpty()) 57 | { 58 | System.out.println("Stack Underflow"); 59 | throw new EmptyStackException(); 60 | } 61 | x=stackArray[top]; 62 | top=top-1; 63 | return x; 64 | } 65 | 66 | public int peek() 67 | { 68 | if(isEmpty()) 69 | { 70 | System.out.println("Stack Underflow"); 71 | throw new EmptyStackException(); 72 | } 73 | return stackArray[top]; 74 | } 75 | 76 | public void display() 77 | { 78 | int i; 79 | 80 | if(isEmpty()) 81 | { 82 | System.out.println("Stack is empty"); 83 | return; 84 | } 85 | System.out.println("Stack is : "); 86 | for(i=top; i>=0; i--) 87 | System.out.println(stackArray[i] + " "); 88 | System.out.println(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /stack/stackLinked/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package stackLinked; 7 | 8 | import java.util.Scanner; 9 | 10 | 11 | public class Demo 12 | { 13 | public static void main(String[] args) 14 | { 15 | int choice,x; 16 | Scanner scan = new Scanner(System.in); 17 | 18 | StackL st = new StackL(); 19 | 20 | while(true) 21 | { 22 | System.out.println("1.Push an element on the stack"); 23 | System.out.println("2.Pop an element from the stack"); 24 | System.out.println("3.Display the top element"); 25 | System.out.println("4.Display all stack elements"); 26 | System.out.println("5.Display size of the stack"); 27 | System.out.println("6.Quit"); 28 | System.out.print("Enter your choice : "); 29 | choice = scan.nextInt(); 30 | 31 | if(choice==6) 32 | break; 33 | switch(choice) 34 | { 35 | case 1 : 36 | System.out.print("Enter the element to be pushed : "); 37 | x=scan.nextInt(); 38 | st.push(x); 39 | break; 40 | case 2: 41 | x=st.pop(); 42 | System.out.println("Popped element is : " + x); 43 | break; 44 | case 3: 45 | System.out.println("Element at the top is : " + st.peek()); 46 | break; 47 | case 4: 48 | st.display(); 49 | break; 50 | case 5: 51 | System.out.println("Size of stack " + st.size()); 52 | break; 53 | default: 54 | System.out.println("Wrong choice"); 55 | } 56 | System.out.println(""); 57 | } 58 | scan.close(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /stack/stackLinked/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package stackLinked; 7 | 8 | public class Node 9 | { 10 | public int info; 11 | public Node link; 12 | 13 | public Node(int i) 14 | { 15 | info=i; 16 | link=null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stack/stackLinked/StackL.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | package stackLinked; 7 | 8 | import java.util.EmptyStackException; 9 | 10 | public class StackL 11 | { 12 | private Node top; 13 | 14 | public StackL() 15 | { 16 | top=null; 17 | } 18 | 19 | public int size() 20 | { 21 | int s=0; 22 | Node p=top; 23 | while(p!=null) 24 | { 25 | p=p.link; 26 | s++; 27 | } 28 | return s; 29 | } 30 | 31 | public void push(int x) 32 | { 33 | Node temp = new Node(x); 34 | temp.link=top; 35 | top=temp; 36 | } 37 | 38 | public int pop() 39 | { 40 | int x; 41 | if(isEmpty()) 42 | { 43 | System.out.println("Stack Underflow\n"); 44 | throw new EmptyStackException(); 45 | } 46 | x=top.info; 47 | top=top.link; 48 | return x; 49 | } 50 | 51 | public int peek() 52 | { 53 | if(isEmpty()) 54 | { 55 | System.out.println("Stack Underflow\n"); 56 | throw new EmptyStackException(); 57 | } 58 | return top.info; 59 | } 60 | 61 | public boolean isEmpty() 62 | { 63 | return (top==null); 64 | } 65 | 66 | public void display() 67 | { 68 | Node p=top; 69 | if(isEmpty()) 70 | { 71 | System.out.println("Stack is empty"); 72 | return; 73 | } 74 | 75 | System.out.println("Stack is : "); 76 | while(p!=null) 77 | { 78 | System.out.println(p.info + " "); 79 | p=p.link; 80 | } 81 | System.out.println(); 82 | } 83 | } 84 | --------------------------------------------------------------------------------