From ed0ef1ccb0d9d36a0d8905dc05ef13da359e06f2 Mon Sep 17 00:00:00 2001 From: gitaman8481 <65482419+gitaman8481@users.noreply.github.com> Date: Tue, 18 Aug 2020 17:32:12 +0530 Subject: [PATCH 01/10] new folders added for program --- CollectionFramework/List.java | 14 ++++++ arrays/BasicArrays.java | 33 ++++++++++++++ arrays/FIndDublicate.java | 32 ++++++++++++++ arrays/package-info.java | 1 + basics/ControlStatements.java | 31 +++++++++++++ basics/DoWhileLoop.java | 15 +++++++ basics/Operators.java | 17 ++++++++ basics/Treedatastructure.java | 65 ++++++++++++++++++++++++++++ basics/VariablesandDataTypes.java | 15 +++++++ basics/WhileLoop.java | 16 +++++++ dataStructure/ArrayDequeDemo.java | 24 ++++++++++ dataStructure/ArrayListDemo.java | 33 ++++++++++++++ dataStructure/HashMapIntro.java | 28 ++++++++++++ dataStructure/LinkedListDemo.java | 35 +++++++++++++++ dataStructure/MyHashSet.java | 10 +++++ oops/A.java | 10 +++++ oops/Mor.java | 13 ++++++ oops/MyConstructor.java | 12 +++++ oops/Person.java | 7 +++ oops/RepairShop.java | 20 +++++++++ oops/Sonata.java | 12 +++++ oops/StaticKeyword.java | 15 +++++++ oops/Vehicle.java | 19 ++++++++ oops/Watches.java | 8 ++++ oopsEncapsulation/EncapIntro.java | 17 ++++++++ oopsEncapsulation/S.java | 30 +++++++++++++ oopsinheritance/MainClass.java | 20 +++++++++ oopsinheritance/Person.java | 15 +++++++ oopsinheritance/Singer.java | 9 ++++ oopsinheritance/Teacher.java | 9 ++++ patternsByloops/Pattern1.java | 18 ++++++++ patternsByloops/Pattern2.java | 17 ++++++++ patternsByloops/Pattern3.java | 18 ++++++++ patternsByloops/Pattern4.java | 20 +++++++++ patternsByloops/Pattern5.java | 30 +++++++++++++ patternsByloops/Pattern5_2.java | 24 ++++++++++ patternsByloops/Pattern6.java | 21 +++++++++ patternsByloops/Pattern7.java | 18 ++++++++ patternsByloops/Pattern8.java | 21 +++++++++ patternsByloops/Pattern9.java | 19 ++++++++ sdeProblems/FindDuplicate.java | 32 ++++++++++++++ sdeProblems/RotateArray.java | 38 ++++++++++++++++ sdeProblems/Test.java | 37 ++++++++++++++++ sortingAlgorithms/BubbleSort.java | 30 +++++++++++++ sortingAlgorithms/InsertionSort.java | 42 ++++++++++++++++++ sortingAlgorithms/SelectionSort.java | 42 ++++++++++++++++++ 46 files changed, 1012 insertions(+) create mode 100644 CollectionFramework/List.java create mode 100644 arrays/BasicArrays.java create mode 100644 arrays/FIndDublicate.java create mode 100644 arrays/package-info.java create mode 100644 basics/ControlStatements.java create mode 100644 basics/DoWhileLoop.java create mode 100644 basics/Operators.java create mode 100644 basics/Treedatastructure.java create mode 100644 basics/VariablesandDataTypes.java create mode 100644 basics/WhileLoop.java create mode 100644 dataStructure/ArrayDequeDemo.java create mode 100644 dataStructure/ArrayListDemo.java create mode 100644 dataStructure/HashMapIntro.java create mode 100644 dataStructure/LinkedListDemo.java create mode 100644 dataStructure/MyHashSet.java create mode 100644 oops/A.java create mode 100644 oops/Mor.java create mode 100644 oops/MyConstructor.java create mode 100644 oops/Person.java create mode 100644 oops/RepairShop.java create mode 100644 oops/Sonata.java create mode 100644 oops/StaticKeyword.java create mode 100644 oops/Vehicle.java create mode 100644 oops/Watches.java create mode 100644 oopsEncapsulation/EncapIntro.java create mode 100644 oopsEncapsulation/S.java create mode 100644 oopsinheritance/MainClass.java create mode 100644 oopsinheritance/Person.java create mode 100644 oopsinheritance/Singer.java create mode 100644 oopsinheritance/Teacher.java create mode 100644 patternsByloops/Pattern1.java create mode 100644 patternsByloops/Pattern2.java create mode 100644 patternsByloops/Pattern3.java create mode 100644 patternsByloops/Pattern4.java create mode 100644 patternsByloops/Pattern5.java create mode 100644 patternsByloops/Pattern5_2.java create mode 100644 patternsByloops/Pattern6.java create mode 100644 patternsByloops/Pattern7.java create mode 100644 patternsByloops/Pattern8.java create mode 100644 patternsByloops/Pattern9.java create mode 100644 sdeProblems/FindDuplicate.java create mode 100644 sdeProblems/RotateArray.java create mode 100644 sdeProblems/Test.java create mode 100644 sortingAlgorithms/BubbleSort.java create mode 100644 sortingAlgorithms/InsertionSort.java create mode 100644 sortingAlgorithms/SelectionSort.java diff --git a/CollectionFramework/List.java b/CollectionFramework/List.java new file mode 100644 index 0000000..689e035 --- /dev/null +++ b/CollectionFramework/List.java @@ -0,0 +1,14 @@ +package CollectionFramework; + +import java.util.LinkedList; +public class List{ + public static void main(String[] args) { + + LinkedList ll = new LinkedList<>(); + + ll.add("Aman"); + ll.add("Soni"); + ll.add(1, "Kumar"); + System.out.println(ll); + } +} diff --git a/arrays/BasicArrays.java b/arrays/BasicArrays.java new file mode 100644 index 0000000..2421967 --- /dev/null +++ b/arrays/BasicArrays.java @@ -0,0 +1,33 @@ +package arrays; + +import java.io.*; + +public class BasicArrays { + + public static void main(String[] args) { + + int a[]; + a = new int[5]; + + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + for(int i=0;i<5;i++) { + int j=i+1; + System.out.println(" Enter element no." +j); + String s = br.readLine(); + a[i] = Integer.parseInt(s); + } + for(int i=0;i<5;i++) { + System.out.println("arr["+i+"]="+a[i]); + } + } + + catch(Exception e) { + System.out.println(""+e); + } + } +} + + + + diff --git a/arrays/FIndDublicate.java b/arrays/FIndDublicate.java new file mode 100644 index 0000000..a96cce0 --- /dev/null +++ b/arrays/FIndDublicate.java @@ -0,0 +1,32 @@ +package arrays; + +public class FIndDublicate { + // Implementing an array as a HashMap for finding duplicate elements + void printRepeating(int m[],int size) { + + int i; + System.out.print("The repeating elements are:"); + + for(i=0;i=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; + else System.out.print(Math.abs(m[i]) + " "); + } + } + + //Driver Method + public static void main(String[] args) { + + FIndDublicate dublicate= new FIndDublicate(); // Making an object of FindDuplicate class + + int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array + int m_size = m.length; // variables which store size or length of array + + dublicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements + + } + +} + diff --git a/arrays/package-info.java b/arrays/package-info.java new file mode 100644 index 0000000..7fae96a --- /dev/null +++ b/arrays/package-info.java @@ -0,0 +1 @@ +package arrays; \ No newline at end of file diff --git a/basics/ControlStatements.java b/basics/ControlStatements.java new file mode 100644 index 0000000..2962c5f --- /dev/null +++ b/basics/ControlStatements.java @@ -0,0 +1,31 @@ +package basics; +// public class to implement or for run the code +public class ControlStatements { + + public static void main(String[] args) { + // for loop to implement switch case statement + for(int i =0;i<9;i++){ + // switch controle statement + switch(i) { + // case1 + case 1: + System.out.println(" i is zero."); + break; //break statement + //case2 + case 2: + System.out.println(" i is one."); + break; + //case3 + case 3: + System.out.println(" i is zero."); + break; + default: //default statement + System.out.println(" i is greater than 3."); + + } + + } + } +} + + diff --git a/basics/DoWhileLoop.java b/basics/DoWhileLoop.java new file mode 100644 index 0000000..a13f4c4 --- /dev/null +++ b/basics/DoWhileLoop.java @@ -0,0 +1,15 @@ +package basics; + +public class DoWhileLoop { + //method inside which do while loop will occur + public static void main(String[] args) { + + int n = 10; + //do while loop's do block + // must executes atleast once + do { + System.out.println(" tick " +n); + n--; + }while(n>0);// do while loop's while block + } // end of main method +}// end of class diff --git a/basics/Operators.java b/basics/Operators.java new file mode 100644 index 0000000..1d06969 --- /dev/null +++ b/basics/Operators.java @@ -0,0 +1,17 @@ +package basics; + +public class Operators { + + public static void main(String[] args) { + int a = 7,b= 6; + int c = a*b; //* (multiplication) operator + int d = a+b; // + addition operator + int e = a/b; // - substration operator + int f = a%b; // % modulus operator + int g = a^b; // ^ power operator + long h = b^a; + System.out.println(c + " " + d + " " + e + " " + f + " " + g + " " + h); + + } + +} diff --git a/basics/Treedatastructure.java b/basics/Treedatastructure.java new file mode 100644 index 0000000..cbdbe60 --- /dev/null +++ b/basics/Treedatastructure.java @@ -0,0 +1,65 @@ +package basics; + +// a node class +class Node{ + int key ; + Node left,right; + public Node(int item) { + key = item; + left = right = null; + } +} + +class Treedatastructure{ + // Root of Binary Tree + Node root; + + // Constructors + Treedatastructure(int key) + { + root = new Node(key); + } + + Treedatastructure() + { + root = null; + } + + public static void main(String[] args) + { + Treedatastructure tree = new Treedatastructure(); + + /*create root*/ + tree.root = new Node(1); + + /* following is the tree after above statement + + 1 + / \ + null null */ + + tree.root.left = new Node(2); + tree.root.right = new Node(3); + + /* 2 and 3 become left and right children of 1 + 1 + / \ + 2 3 + / \ / \ + null null null null */ + + + tree.root.left.left = new Node(4); + /* 4 becomes left child of 2 + 1 + / \ + 2 3 + / \ / \ + 4 null null null + / \ + null null + */ + + }} + + diff --git a/basics/VariablesandDataTypes.java b/basics/VariablesandDataTypes.java new file mode 100644 index 0000000..111d443 --- /dev/null +++ b/basics/VariablesandDataTypes.java @@ -0,0 +1,15 @@ +package basics; + +public class VariablesandDataTypes { + + public static void main(String[] args) { + + int age = 18; // integer data type + String name = "Aman"; // String data type + long number = 7023; // long data type + String address ="Behind Fort"; + System.out.println(age + " " + name + " " + number + " " + address); + + } + +} diff --git a/basics/WhileLoop.java b/basics/WhileLoop.java new file mode 100644 index 0000000..6c0195a --- /dev/null +++ b/basics/WhileLoop.java @@ -0,0 +1,16 @@ +package basics; + +public class WhileLoop { + + public static void main(String[] args) { + + int n = 5; + // while loop block + while(n>0) { + System.out.println(" tick " + n); + n--; // post substraction operator + } + + } + + } diff --git a/dataStructure/ArrayDequeDemo.java b/dataStructure/ArrayDequeDemo.java new file mode 100644 index 0000000..15aaa23 --- /dev/null +++ b/dataStructure/ArrayDequeDemo.java @@ -0,0 +1,24 @@ +package dataStructure; +import java.util.*; +public class ArrayDequeDemo { + + public static void main(String[] args) { + + ArrayDeque adq = new ArrayDeque(); + + adq.push("A"); + adq.push("B"); + adq.push("C"); + adq.push("D"); + adq.push("E"); + + System.out.print("Popping the stack:"); + + while(adq.peek()!=null) + System.out.print(adq.pop()+" "); + + System.out.println(); + + } + +} diff --git a/dataStructure/ArrayListDemo.java b/dataStructure/ArrayListDemo.java new file mode 100644 index 0000000..f0aae7e --- /dev/null +++ b/dataStructure/ArrayListDemo.java @@ -0,0 +1,33 @@ +package dataStructure; +import java.util.ArrayList; + +public class ArrayListDemo { + + public static void main(String[] args) { + + //Create an array list + ArrayList al = new ArrayList(); + System.out.println("Initial size of al:" + al.size()); + + //Add elements to array list + al.add("A"); + al.add("M"); + al.add("A"); + al.add("N"); + al.add("S"); + al.add("s1"); + System.out.println("Size of al after additions:" + al.size()); + + //Display the array list + System.out.println("Contents of al:" + al); + + //Remove elements from the array + al.remove("s1"); + + System.out.println("Size of al after deletions:"+ al.size()); + System.out.println("Contents of al:" + al); + + + } + +} diff --git a/dataStructure/HashMapIntro.java b/dataStructure/HashMapIntro.java new file mode 100644 index 0000000..bebd6c3 --- /dev/null +++ b/dataStructure/HashMapIntro.java @@ -0,0 +1,28 @@ +package dataStructure; +import java.util.HashMap; +public class HashMapIntro +{ + + public static void main(String[] args) + { + // Create an empty hash map + HashMap map = new HashMap<>(); + + // Add elements to the map + map.put("vishal", 10); + map.put("sachin", 30); + map.put("vaibhav", 20); + + // Print size and content + System.out.println("Size of map is:- " + map.size()); + System.out.println(map); + + // Check if a key is present and if present, print value + if ( map.containsKey("vishal")) + { + Integer a = map.get("vishal"); + System.out.println("value for key vishal is:- "+ a); + } + } + +} diff --git a/dataStructure/LinkedListDemo.java b/dataStructure/LinkedListDemo.java new file mode 100644 index 0000000..994ff70 --- /dev/null +++ b/dataStructure/LinkedListDemo.java @@ -0,0 +1,35 @@ +package dataStructure; +import java.util.LinkedList; +public class LinkedListDemo { + + public static void main(String[] args) { + // Create a linked list + LinkedList ll = new LinkedList<>(); + + // Add elements to the linked list + ll.add("A"); + ll.add("M"); + ll.add("M"); + ll.add("N"); + ll.add("S"); + ll.addFirst("S1"); + ll.addLast("S2"); + ll.add(1,"A1"); + System.out.println("Original contents of ll:"+ll); + + //Remove elements from the linked list + ll.remove("A1"); + System.out.println("Content of ll after deletion:"+ll); + + //Remove first & last elements + ll.removeFirst(); + ll.removeLast(); + System.out.println("ll after deleting first & last:" + ll); + + //Get & set value + String val = ll.get(2); + ll.set(2, val + "Changed"); + System.out.println("ll after change:"+ll); + } + +} diff --git a/dataStructure/MyHashSet.java b/dataStructure/MyHashSet.java new file mode 100644 index 0000000..e0b82f4 --- /dev/null +++ b/dataStructure/MyHashSet.java @@ -0,0 +1,10 @@ +package dataStructure; + +public class MyHashSet { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/oops/A.java b/oops/A.java new file mode 100644 index 0000000..face387 --- /dev/null +++ b/oops/A.java @@ -0,0 +1,10 @@ +package oops; + +public class A { +class B{ + int age; +} +static class C{ + String name; +} +} diff --git a/oops/Mor.java b/oops/Mor.java new file mode 100644 index 0000000..263c1c7 --- /dev/null +++ b/oops/Mor.java @@ -0,0 +1,13 @@ +package oops; + +public class Mor extends Watches { + + @Override + public void Digital() { + System.out.println("Method_overriding is digital"); + } + public void Royal() { + System.out.println("Method_overriding is royal"); + } + +} diff --git a/oops/MyConstructor.java b/oops/MyConstructor.java new file mode 100644 index 0000000..eadde6c --- /dev/null +++ b/oops/MyConstructor.java @@ -0,0 +1,12 @@ +package oops; + +public class MyConstructor { + + MyConstructor(){ + System.out.println("Obj is now created"); + } + public static void main(String[] args) { + MyConstructor obj = new MyConstructor(); + } + +} diff --git a/oops/Person.java b/oops/Person.java new file mode 100644 index 0000000..d17564a --- /dev/null +++ b/oops/Person.java @@ -0,0 +1,7 @@ +package oops; + +public class Person { +int age; +String name; +final static String breed = "HomoSapiens"; +} diff --git a/oops/RepairShop.java b/oops/RepairShop.java new file mode 100644 index 0000000..f119ee3 --- /dev/null +++ b/oops/RepairShop.java @@ -0,0 +1,20 @@ +package oops; + +public class RepairShop { + + + public static void repair (Watches ws) { + System.out.println("watch is repaired"); + } + public static void repair(Sonata sn) { + System.out.println("watch is repaired"); + } + public static void main(String[] args) { + Mor Mo = new Mor(); + Sonata sn = new Sonata(); + + repair(Mo); + repair(sn); + } + +} diff --git a/oops/Sonata.java b/oops/Sonata.java new file mode 100644 index 0000000..7d8972d --- /dev/null +++ b/oops/Sonata.java @@ -0,0 +1,12 @@ +package oops; + +public class Sonata extends Watches{ + + @Override + public void Digital() { + System.out.println("Sonata is digital"); + } + public void Royal() { + System.out.println("Sonata is royal"); + } +} diff --git a/oops/StaticKeyword.java b/oops/StaticKeyword.java new file mode 100644 index 0000000..ce2e6d9 --- /dev/null +++ b/oops/StaticKeyword.java @@ -0,0 +1,15 @@ +package oops; + +public class StaticKeyword { + public static void main(String[] args) { + Person obj = new Person(); + obj.name ="Aman"; + obj.age = 19; + System.out.println(obj.name); + System.out.println(obj.age + " age"); + System.out.println(obj.breed); + + + } + +} diff --git a/oops/Vehicle.java b/oops/Vehicle.java new file mode 100644 index 0000000..b6058cb --- /dev/null +++ b/oops/Vehicle.java @@ -0,0 +1,19 @@ +package oops; + +class transport{ + int wheels; + transport(){ + wheels = 4; + } +} + +public class Vehicle { + + public static void main(String[] args) { + + transport car = new transport(); + System.out.println(car.wheels + " wheels"); + + } + +} diff --git a/oops/Watches.java b/oops/Watches.java new file mode 100644 index 0000000..863a6d9 --- /dev/null +++ b/oops/Watches.java @@ -0,0 +1,8 @@ +package oops; + +public abstract class Watches { + + public abstract void Digital(); + public abstract void Royal(); + +} diff --git a/oopsEncapsulation/EncapIntro.java b/oopsEncapsulation/EncapIntro.java new file mode 100644 index 0000000..e069086 --- /dev/null +++ b/oopsEncapsulation/EncapIntro.java @@ -0,0 +1,17 @@ +package oopsEncapsulation; + +public class EncapIntro { + + public static void main(String[] args) { + + S obj = new S(); + + obj.setName("Milan"); + obj.setAge(16); + System.out.println(obj.getName()); + System.out.println(obj.getAge()); + + + } + +} diff --git a/oopsEncapsulation/S.java b/oopsEncapsulation/S.java new file mode 100644 index 0000000..f8de03c --- /dev/null +++ b/oopsEncapsulation/S.java @@ -0,0 +1,30 @@ +package oopsEncapsulation; + +public class S { + + /*we have to put the variables private to achieve encapsulation*/ + private int age; + + private String name; + + //setter methods + public void setAge(int age) { + if(age>20) System.out.println(" You are to old:"); + else this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + //getter methods + public int getAge() { + return age; + } + + public String getName() { + return name; + } + + +} diff --git a/oopsinheritance/MainClass.java b/oopsinheritance/MainClass.java new file mode 100644 index 0000000..c4c5dd6 --- /dev/null +++ b/oopsinheritance/MainClass.java @@ -0,0 +1,20 @@ +package oopsinheritance; + +public class MainClass { + + public static void main(String[] args) { + + Teacher T = new Teacher(); + T.name = "Mr Harry "; + T.eat(); + T.walk(); + T.teach(); + + Singer s = new Singer(); + s.name = "Justin B "; + s.sing(); + s.eat(); + + } + +} diff --git a/oopsinheritance/Person.java b/oopsinheritance/Person.java new file mode 100644 index 0000000..d4c4805 --- /dev/null +++ b/oopsinheritance/Person.java @@ -0,0 +1,15 @@ +package oopsinheritance; + +public class Person { + + protected String name; + + public void walk() { + System.out.println(name + "is walking"); + } + + public void eat() { + System.out.println(name + "is eating"); + } + +} diff --git a/oopsinheritance/Singer.java b/oopsinheritance/Singer.java new file mode 100644 index 0000000..0d39ff4 --- /dev/null +++ b/oopsinheritance/Singer.java @@ -0,0 +1,9 @@ +package oopsinheritance; + +public class Singer extends Person { + + public void sing() { + System.out.println(name + "is singing"); + } + +} diff --git a/oopsinheritance/Teacher.java b/oopsinheritance/Teacher.java new file mode 100644 index 0000000..71e38d8 --- /dev/null +++ b/oopsinheritance/Teacher.java @@ -0,0 +1,9 @@ +package oopsinheritance; + +public class Teacher extends Person { + + public void teach() { + System.out.println(name + "is teaching"); + } + +} diff --git a/patternsByloops/Pattern1.java b/patternsByloops/Pattern1.java new file mode 100644 index 0000000..4546ec7 --- /dev/null +++ b/patternsByloops/Pattern1.java @@ -0,0 +1,18 @@ +package patternsByloops; + +public class Pattern1 { + + public static void main(String[] args) { + + int n = 6; + + for(int i= 0;i=i;j--) //j for horizontal stars + System.out.print("* "); //printing the stars + + System.out.println(); + } + } + +} diff --git a/patternsByloops/Pattern3.java b/patternsByloops/Pattern3.java new file mode 100644 index 0000000..d2a20ca --- /dev/null +++ b/patternsByloops/Pattern3.java @@ -0,0 +1,18 @@ +package patternsByloops; + +public class Pattern3 { + + public static void main(String[] args) { + + int n = 6; //size of the pattern + + for(int i=1;i<=n;i++) { //i for horizontal stars operation + + for(int j=n-1;j>=i;j--) //j for vertical stars operation + System.out.print("* "); //printing the stars + + System.out.println(); // for new line + } + } + +} diff --git a/patternsByloops/Pattern4.java b/patternsByloops/Pattern4.java new file mode 100644 index 0000000..3635f7e --- /dev/null +++ b/patternsByloops/Pattern4.java @@ -0,0 +1,20 @@ +package patternsByloops; + +public class Pattern4 { + public static void main(String[] args) { + + int n=6; //size of the pattern + + for(int i=n;i>=1;i--){ //i for horizontal stars + + for(int j=n-1;j>=i;j--) //j for vertical and for print & handling the spaces + System.out.print(" "); + + for(int k=1;k<=i;k++) //k variable for print the values + System.out.print("* "); //printing the stars + + System.out.println(); + } + } + +} diff --git a/patternsByloops/Pattern5.java b/patternsByloops/Pattern5.java new file mode 100644 index 0000000..a93e554 --- /dev/null +++ b/patternsByloops/Pattern5.java @@ -0,0 +1,30 @@ +package patternsByloops; + +public class Pattern5 { + + public static void main(String[] args) { + + int min_stars =0; /*change value to set minimum no. of stars in pyramid + take odd no.for odd no. of stars in each row 1-3-5 + etc + take even no. for even no. stars in each row,2-4-6 etc */ + + int p_height=6; //change value to increase or decrease the size of pyramid + + int p_space = p_height - 1; + + for(int i= 1;ii;j--) + System.out.print(" "); + + for(int k=0;k<=min_stars;k++) + System.out.print("* "); + + min_stars+=2; + System.out.println(); + } + + }//end of main +}//end of class diff --git a/patternsByloops/Pattern5_2.java b/patternsByloops/Pattern5_2.java new file mode 100644 index 0000000..3728cec --- /dev/null +++ b/patternsByloops/Pattern5_2.java @@ -0,0 +1,24 @@ +package patternsByloops; + +public class Pattern5_2 { + + public static void main(String[] args) { + + int n = 5; //size of pattern + + int px =n; //left print control + int py =n; //right print control + + for(int i=1;i<=n;i++) //i for horizontal stars + { + for(int j=1;j=px && j<=py) System.out.print("* "); //if condition is true then print the stars + else System.out.print(" "); //else print the spaces + + px--; //post decrementing the left print control + py++; //post incrementing the right print control + System.out.println(); + } + + } +} diff --git a/patternsByloops/Pattern6.java b/patternsByloops/Pattern6.java new file mode 100644 index 0000000..efb017c --- /dev/null +++ b/patternsByloops/Pattern6.java @@ -0,0 +1,21 @@ +package patternsByloops; + +public class Pattern6 { + + public static void main(String[] args) + { + int n=5; + //size of pattern + for(int i= n;i>=1;i--) + { + for(int j=n-1;j>=i;j--) // loop for print the spaces + System.out.print(" "); //printing the spaces + + for(int k =1;k<=i;k++) // loop for print the stars + System.out.print("*"); //printing the stars + + System.out.println(); //for new line + }//end of loop + + }//end of main +}//end of class diff --git a/patternsByloops/Pattern7.java b/patternsByloops/Pattern7.java new file mode 100644 index 0000000..4365cb5 --- /dev/null +++ b/patternsByloops/Pattern7.java @@ -0,0 +1,18 @@ +package patternsByloops; + +public class Pattern7 +{ + public static void main(String[] args) + { + + int size = 3; //size of the pattern + + for(int i=size;i>=-size;i--) //loop to print the stars + { + for( int j=size;j>=Math.abs(i);j--) //inner or nested loop for print the stars + System.out.print("* "); + + System.out.println(); //for new line + }//end of loop + }//end of main +}//end of class diff --git a/patternsByloops/Pattern8.java b/patternsByloops/Pattern8.java new file mode 100644 index 0000000..8ef97cb --- /dev/null +++ b/patternsByloops/Pattern8.java @@ -0,0 +1,21 @@ +package patternsByloops; + +public class Pattern8 +{ + public static void main(String[] args) + { + int size = 3; //size of pattern + + for(int i=size;i>=-size;i--) { //loop for handling spaces & stars + + for(int j=1;j<=Math.abs(i);j++) //inner or nested loop to handle or print spaces + System.out.print(" "); //printing the spaces + + for(int k=size;k>=Math.abs(i);k--) //inner or nested loop to handle or print stars + System.out.print("*"); //printing the stars + + System.out.println(); //for new line + }//end of loop + + }//end of main +}//end of class diff --git a/patternsByloops/Pattern9.java b/patternsByloops/Pattern9.java new file mode 100644 index 0000000..d9ea106 --- /dev/null +++ b/patternsByloops/Pattern9.java @@ -0,0 +1,19 @@ +package patternsByloops; + +public class Pattern9 { + + public static void main(String[] args) { + + int size = 3; //size of pattern + for(int i=size;i>=-size;i--) //loop for handle stars & spaces + { + for(int j=1;j<=Math.abs(i);j++) //inner or nested loop for spaces + System.out.print(" "); //printing the spaces + + for(int k=size;k>=Math.abs(i);k--) //inner or nested loop for stars + System.out.print("* "); //printing the stars + + System.out.println(); //new line + } + }//end of main +}//end of class diff --git a/sdeProblems/FindDuplicate.java b/sdeProblems/FindDuplicate.java new file mode 100644 index 0000000..df101dc --- /dev/null +++ b/sdeProblems/FindDuplicate.java @@ -0,0 +1,32 @@ +package sdeProblems; + +public class FindDuplicate { + + // Implementing an array as a HashMap for finding duplicate elements + void printRepeating(int m[],int size) { + + int i; + System.out.print("The repeating elements are:"); + + for(i=0;i=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; + else System.out.print(Math.abs(m[i]) + " "); + } + } + + //Driver Method + public static void main(String[] args) { + + FindDuplicate duplicate = new FindDuplicate(); // Making an object of FindDuplicate class + + int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array + int m_size = m.length; // variables which store size or length of array + + duplicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements + + } + + } diff --git a/sdeProblems/RotateArray.java b/sdeProblems/RotateArray.java new file mode 100644 index 0000000..69b6ef1 --- /dev/null +++ b/sdeProblems/RotateArray.java @@ -0,0 +1,38 @@ +package sdeProblems; + +public class RotateArray { + + //function to rotate the elements of an array + + void rotateL(int a[], int d, int n) { + for(int i= 0;i=0; i--) + { + /* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater + element is not found */ + int j, last = arr1[m-1]; + + for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--) + arr1[j+1] = arr1[j]; + + // If there was a greater element + if (j != m-2 || last > arr2[i]) { + arr1[j+1] = arr2[i]; + arr2[i] = last; } + } + } + + // Driver method to test the above function + public static void main1(String[] args) + { + merge(arr1.length,arr2.length); + System.out.print("After Merging nFirst Array: "); + System.out.println(Arrays.toString(arr1)); + System.out.print("Second Array: "); + System.out.println(Arrays.toString(arr2)); + } +} diff --git a/sortingAlgorithms/BubbleSort.java b/sortingAlgorithms/BubbleSort.java new file mode 100644 index 0000000..4bd2323 --- /dev/null +++ b/sortingAlgorithms/BubbleSort.java @@ -0,0 +1,30 @@ +package sortingAlgorithms; + +public class BubbleSort { + + public static void main(String[] args) { + + int a[] = {7,9,-4,6,8,5}; + int n = a.length; + + for(int i=0;i<=n-1-i;i++) { + for(int j=0;j<=n-1-i;j++) { + + boolean sorted = true; + + if(a[j+1] < a[j]){ + int temp = a[j+1]; + a[j+1] = a[j]; + a[j]= temp; + sorted = false; + } + + if(sorted) break; + } + } + + for(int item:a) + System.out.print(item+" "); + + }//end of main +}//end of class diff --git a/sortingAlgorithms/InsertionSort.java b/sortingAlgorithms/InsertionSort.java new file mode 100644 index 0000000..2efb58c --- /dev/null +++ b/sortingAlgorithms/InsertionSort.java @@ -0,0 +1,42 @@ +package sortingAlgorithms; + +public class InsertionSort { + + /*Function to sort array using insertion sort*/ + void sort(int arr[]) + { + int n = arr.length; + for (int i = 1; i < n; ++i) { + int key = arr[i]; + int j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + /* A utility function to print array of size n*/ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + + System.out.println(); + } + + // Driver method + public static void main(String args[]) + { + int arr[] = { 12, 11, 13, 5, 6 }; + + InsertionSort ob = new InsertionSort(); + ob.sort(arr); + + printArray(arr); + } +} + diff --git a/sortingAlgorithms/SelectionSort.java b/sortingAlgorithms/SelectionSort.java new file mode 100644 index 0000000..a517475 --- /dev/null +++ b/sortingAlgorithms/SelectionSort.java @@ -0,0 +1,42 @@ +package sortingAlgorithms; + +public class SelectionSort { + + void sort(int arr[]) + { + int n = arr.length; + + // One by one move boundary of unsorted sub-array + for (int i = 0; i < n-1; i++) { + // Find the minimum element in unsorted array + int min_idx = i; + for (int j = i+1; j < n; j++) + if (arr[j] < arr[min_idx]) + min_idx = j; + + // Swap the found minimum element with the first + // element + int temp = arr[min_idx]; + arr[min_idx] = arr[i]; + arr[i] = temp; + } + } + + // Prints the array + void printArray(int arr[]) { + int n = arr.length; + for (int i=0; i Date: Thu, 20 Aug 2020 11:38:27 +0530 Subject: [PATCH 02/10] Create README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a93624f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Java-Programs +This is a repository where, I will upload and keep up to date my Java, DataStructure and algorithms code and question regarding to them,which i practiced on eclipse with help of Books. +The books i refer and use for learning java and algorithms are 1. Java for Dummies. 2. Java The Complete Reference by Herberth Schildt. 3. Algorithms by Sedwig. 4. Algorithms by Cormen. From 224cca7c273ee520c43b46a9a78c31695668c8c8 Mon Sep 17 00:00:00 2001 From: Aman Soni <65482419+gitaman8481@users.noreply.github.com> Date: Thu, 20 Aug 2020 11:41:51 +0530 Subject: [PATCH 03/10] Create SECURITY.md --- SECURITY.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..034e848 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,21 @@ +# Security Policy + +## Supported Versions + +Use this section to tell people about which versions of your project are +currently being supported with security updates. + +| Version | Supported | +| ------- | ------------------ | +| 5.1.x | :white_check_mark: | +| 5.0.x | :x: | +| 4.0.x | :white_check_mark: | +| < 4.0 | :x: | + +## Reporting a Vulnerability + +Use this section to tell people how to report a vulnerability. + +Tell them where to go, how often they can expect to get an update on a +reported vulnerability, what to expect if the vulnerability is accepted or +declined, etc. From 0d652f315bb1fa2be2f02691de1d6b9f80e48c2a Mon Sep 17 00:00:00 2001 From: Aman Soni <65482419+gitaman8481@users.noreply.github.com> Date: Sun, 23 Aug 2020 11:41:45 +0530 Subject: [PATCH 04/10] Add files via upload --- arrays/BasicArrays.java | 66 ++++++------- arrays/FIndDublicate.java | 64 ++++++------- basics/ControlStatements.java | 62 ++++++------ basics/DoWhileLoop.java | 30 +++--- basics/Operators.java | 34 +++---- basics/Treedatastructure.java | 130 +++++++++++++------------- basics/VariablesandDataTypes.java | 30 +++--- basics/WhileLoop.java | 32 +++---- dataStructure/ArrayDequeDemo.java | 48 +++++----- dataStructure/ArrayListDemo.java | 66 ++++++------- dataStructure/HashMapIntro.java | 56 +++++------ dataStructure/LinkedListDemo.java | 70 +++++++------- dataStructure/List.java | 14 +++ dataStructure/MYStackByList.java | 121 ++++++++++++++++++++++++ dataStructure/MyArrayList.java | 33 +++++++ dataStructure/MyHashMap.java | 28 ++++++ dataStructure/MyHashSet.java | 31 ++++-- dataStructure/MyLinkedList.java | 35 +++++++ dataStructure/MyQueue.java | 82 ++++++++++++++++ dataStructure/MyStack.java | 58 ++++++++++++ dataStructure/SetExample.java | 34 +++++++ dataStructure/Tree.java | 52 +++++++++++ oops/A.java | 20 ++-- oops/MYStaticKeyword.java | 22 +++++ oops/Mor.java | 26 +++--- oops/MyConstructor.java | 25 ++--- oops/Person.java | 14 +-- oops/RepairShop.java | 40 ++++---- oops/Sonata.java | 24 ++--- oops/StaticKeyword.java | 28 +++--- oops/Vehicle.java | 38 ++++---- oops/Watches.java | 16 ++-- oopsEncapsulation/EncapIntro.java | 34 +++---- oopsEncapsulation/S.java | 60 ++++++------ oopsabstraction/Audi.java | 14 +++ oopsabstraction/Car.java | 8 ++ oopsabstraction/RepairShop.java | 21 +++++ oopsabstraction/WagonR.java | 20 ++++ oopsinheritance/MainClass.java | 40 ++++---- oopsinheritance/Person.java | 30 +++--- oopsinheritance/Singer.java | 18 ++-- oopsinheritance/Teacher.java | 18 ++-- patternsByloops/Pattern1.java | 36 +++---- patternsByloops/Pattern2.java | 34 +++---- patternsByloops/Pattern3.java | 36 +++---- patternsByloops/Pattern4.java | 40 ++++---- patternsByloops/Pattern5.java | 60 ++++++------ patternsByloops/Pattern5_2.java | 48 +++++----- patternsByloops/Pattern6.java | 42 ++++----- patternsByloops/Pattern7.java | 36 +++---- patternsByloops/Pattern8.java | 42 ++++----- patternsByloops/Pattern9.java | 38 ++++---- queues/ArrayDequeDemo.java | 24 +++++ sdeProblems/FindDuplicate.java | 64 ++++++------- sdeProblems/RotateArray.java | 76 +++++++-------- sdeProblems/Test.java | 74 +++++++-------- sortingAlgorithms/BubbleSort.java | 60 ++++++------ sortingAlgorithms/InsertionSort.java | 84 ++++++++--------- sortingAlgorithms/SelectionSort.java | 84 ++++++++--------- stack/MYStackByList.java | 121 ++++++++++++++++++++++++ trees/FindFullNodesInABinaryTree.java | 47 ++++++++++ trees/Insertion_In_BinaryTree.java | 78 ++++++++++++++++ 62 files changed, 1819 insertions(+), 997 deletions(-) create mode 100644 dataStructure/List.java create mode 100644 dataStructure/MYStackByList.java create mode 100644 dataStructure/MyArrayList.java create mode 100644 dataStructure/MyHashMap.java create mode 100644 dataStructure/MyLinkedList.java create mode 100644 dataStructure/MyQueue.java create mode 100644 dataStructure/MyStack.java create mode 100644 dataStructure/SetExample.java create mode 100644 dataStructure/Tree.java create mode 100644 oops/MYStaticKeyword.java create mode 100644 oopsabstraction/Audi.java create mode 100644 oopsabstraction/Car.java create mode 100644 oopsabstraction/RepairShop.java create mode 100644 oopsabstraction/WagonR.java create mode 100644 queues/ArrayDequeDemo.java create mode 100644 stack/MYStackByList.java create mode 100644 trees/FindFullNodesInABinaryTree.java create mode 100644 trees/Insertion_In_BinaryTree.java diff --git a/arrays/BasicArrays.java b/arrays/BasicArrays.java index 2421967..3677a77 100644 --- a/arrays/BasicArrays.java +++ b/arrays/BasicArrays.java @@ -1,33 +1,33 @@ -package arrays; - -import java.io.*; - -public class BasicArrays { - - public static void main(String[] args) { - - int a[]; - a = new int[5]; - - try { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - for(int i=0;i<5;i++) { - int j=i+1; - System.out.println(" Enter element no." +j); - String s = br.readLine(); - a[i] = Integer.parseInt(s); - } - for(int i=0;i<5;i++) { - System.out.println("arr["+i+"]="+a[i]); - } - } - - catch(Exception e) { - System.out.println(""+e); - } - } -} - - - - +package arrays; + +import java.io.*; + +public class BasicArrays { + + public static void main(String[] args) { + + int a[]; + a = new int[5]; + + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + for(int i=0;i<5;i++) { + int j=i+1; + System.out.println(" Enter element no." +j); + String s = br.readLine(); + a[i] = Integer.parseInt(s); + } + for(int i=0;i<5;i++) { + System.out.println("arr["+i+"]="+a[i]); + } + } + + catch(Exception e) { + System.out.println(""+e); + } + } +} + + + + diff --git a/arrays/FIndDublicate.java b/arrays/FIndDublicate.java index a96cce0..41c697d 100644 --- a/arrays/FIndDublicate.java +++ b/arrays/FIndDublicate.java @@ -1,32 +1,32 @@ -package arrays; - -public class FIndDublicate { - // Implementing an array as a HashMap for finding duplicate elements - void printRepeating(int m[],int size) { - - int i; - System.out.print("The repeating elements are:"); - - for(i=0;i=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; - else System.out.print(Math.abs(m[i]) + " "); - } - } - - //Driver Method - public static void main(String[] args) { - - FIndDublicate dublicate= new FIndDublicate(); // Making an object of FindDuplicate class - - int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array - int m_size = m.length; // variables which store size or length of array - - dublicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements - - } - -} - +package arrays; + +public class FIndDublicate { + // Implementing an array as a HashMap for finding duplicate elements + void printRepeating(int m[],int size) { + + int i; + System.out.print("The repeating elements are:"); + + for(i=0;i=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; + else System.out.print(Math.abs(m[i]) + " "); + } + } + + //Driver Method + public static void main(String[] args) { + + FIndDublicate dublicate= new FIndDublicate(); // Making an object of FindDuplicate class + + int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array + int m_size = m.length; // variables which store size or length of array + + dublicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements + + } + +} + diff --git a/basics/ControlStatements.java b/basics/ControlStatements.java index 2962c5f..6b51848 100644 --- a/basics/ControlStatements.java +++ b/basics/ControlStatements.java @@ -1,31 +1,31 @@ -package basics; -// public class to implement or for run the code -public class ControlStatements { - - public static void main(String[] args) { - // for loop to implement switch case statement - for(int i =0;i<9;i++){ - // switch controle statement - switch(i) { - // case1 - case 1: - System.out.println(" i is zero."); - break; //break statement - //case2 - case 2: - System.out.println(" i is one."); - break; - //case3 - case 3: - System.out.println(" i is zero."); - break; - default: //default statement - System.out.println(" i is greater than 3."); - - } - - } - } -} - - +package basics; +// public class to implement or for run the code +public class ControlStatements { + + public static void main(String[] args) { + // for loop to implement switch case statement + for(int i =0;i<9;i++){ + // switch controle statement + switch(i) { + // case1 + case 1: + System.out.println(" i is zero."); + break; //break statement + //case2 + case 2: + System.out.println(" i is one."); + break; + //case3 + case 3: + System.out.println(" i is zero."); + break; + default: //default statement + System.out.println(" i is greater than 3."); + + } + + } + } +} + + diff --git a/basics/DoWhileLoop.java b/basics/DoWhileLoop.java index a13f4c4..ec6d430 100644 --- a/basics/DoWhileLoop.java +++ b/basics/DoWhileLoop.java @@ -1,15 +1,15 @@ -package basics; - -public class DoWhileLoop { - //method inside which do while loop will occur - public static void main(String[] args) { - - int n = 10; - //do while loop's do block - // must executes atleast once - do { - System.out.println(" tick " +n); - n--; - }while(n>0);// do while loop's while block - } // end of main method -}// end of class +package basics; + +public class DoWhileLoop { + //method inside which do while loop will occur + public static void main(String[] args) { + + int n = 10; + //do while loop's do block + // must executes atleast once + do { + System.out.println(" tick " +n); + n--; + }while(n>0);// do while loop's while block + } // end of main method +}// end of class diff --git a/basics/Operators.java b/basics/Operators.java index 1d06969..7ecd5a8 100644 --- a/basics/Operators.java +++ b/basics/Operators.java @@ -1,17 +1,17 @@ -package basics; - -public class Operators { - - public static void main(String[] args) { - int a = 7,b= 6; - int c = a*b; //* (multiplication) operator - int d = a+b; // + addition operator - int e = a/b; // - substration operator - int f = a%b; // % modulus operator - int g = a^b; // ^ power operator - long h = b^a; - System.out.println(c + " " + d + " " + e + " " + f + " " + g + " " + h); - - } - -} +package basics; + +public class Operators { + + public static void main(String[] args) { + int a = 7,b= 6; + int c = a*b; //* (multiplication) operator + int d = a+b; // + addition operator + int e = a/b; // - substration operator + int f = a%b; // % modulus operator + int g = a^b; // ^ power operator + long h = b^a; + System.out.println(c + " " + d + " " + e + " " + f + " " + g + " " + h); + + } + +} diff --git a/basics/Treedatastructure.java b/basics/Treedatastructure.java index cbdbe60..c03db55 100644 --- a/basics/Treedatastructure.java +++ b/basics/Treedatastructure.java @@ -1,65 +1,65 @@ -package basics; - -// a node class -class Node{ - int key ; - Node left,right; - public Node(int item) { - key = item; - left = right = null; - } -} - -class Treedatastructure{ - // Root of Binary Tree - Node root; - - // Constructors - Treedatastructure(int key) - { - root = new Node(key); - } - - Treedatastructure() - { - root = null; - } - - public static void main(String[] args) - { - Treedatastructure tree = new Treedatastructure(); - - /*create root*/ - tree.root = new Node(1); - - /* following is the tree after above statement - - 1 - / \ - null null */ - - tree.root.left = new Node(2); - tree.root.right = new Node(3); - - /* 2 and 3 become left and right children of 1 - 1 - / \ - 2 3 - / \ / \ - null null null null */ - - - tree.root.left.left = new Node(4); - /* 4 becomes left child of 2 - 1 - / \ - 2 3 - / \ / \ - 4 null null null - / \ - null null - */ - - }} - - +package basics; + +// a node class +class Node{ + int key ; + Node left,right; + public Node(int item) { + key = item; + left = right = null; + } +} + +class Treedatastructure{ + // Root of Binary Tree + Node root; + + // Constructors + Treedatastructure(int key) + { + root = new Node(key); + } + + Treedatastructure() + { + root = null; + } + + public static void main(String[] args) + { + Treedatastructure tree = new Treedatastructure(); + + /*create root*/ + tree.root = new Node(1); + + /* following is the tree after above statement + + 1 + / \ + null null */ + + tree.root.left = new Node(2); + tree.root.right = new Node(3); + + /* 2 and 3 become left and right children of 1 + 1 + / \ + 2 3 + / \ / \ + null null null null */ + + + tree.root.left.left = new Node(4); + /* 4 becomes left child of 2 + 1 + / \ + 2 3 + / \ / \ + 4 null null null + / \ + null null + */ + + }} + + diff --git a/basics/VariablesandDataTypes.java b/basics/VariablesandDataTypes.java index 111d443..08132bd 100644 --- a/basics/VariablesandDataTypes.java +++ b/basics/VariablesandDataTypes.java @@ -1,15 +1,15 @@ -package basics; - -public class VariablesandDataTypes { - - public static void main(String[] args) { - - int age = 18; // integer data type - String name = "Aman"; // String data type - long number = 7023; // long data type - String address ="Behind Fort"; - System.out.println(age + " " + name + " " + number + " " + address); - - } - -} +package basics; + +public class VariablesandDataTypes { + + public static void main(String[] args) { + + int age = 18; // integer data type + String name = "Aman"; // String data type + long number = 7023; // long data type + String address ="Behind Fort"; + System.out.println(age + " " + name + " " + number + " " + address); + + } + +} diff --git a/basics/WhileLoop.java b/basics/WhileLoop.java index 6c0195a..cb755b3 100644 --- a/basics/WhileLoop.java +++ b/basics/WhileLoop.java @@ -1,16 +1,16 @@ -package basics; - -public class WhileLoop { - - public static void main(String[] args) { - - int n = 5; - // while loop block - while(n>0) { - System.out.println(" tick " + n); - n--; // post substraction operator - } - - } - - } +package basics; + +public class WhileLoop { + + public static void main(String[] args) { + + int n = 5; + // while loop block + while(n>0) { + System.out.println(" tick " + n); + n--; // post substraction operator + } + + } + + } diff --git a/dataStructure/ArrayDequeDemo.java b/dataStructure/ArrayDequeDemo.java index 15aaa23..35dadd2 100644 --- a/dataStructure/ArrayDequeDemo.java +++ b/dataStructure/ArrayDequeDemo.java @@ -1,24 +1,24 @@ -package dataStructure; -import java.util.*; -public class ArrayDequeDemo { - - public static void main(String[] args) { - - ArrayDeque adq = new ArrayDeque(); - - adq.push("A"); - adq.push("B"); - adq.push("C"); - adq.push("D"); - adq.push("E"); - - System.out.print("Popping the stack:"); - - while(adq.peek()!=null) - System.out.print(adq.pop()+" "); - - System.out.println(); - - } - -} +package dataStructure; +import java.util.*; +public class ArrayDequeDemo { + + public static void main(String[] args) { + + ArrayDeque adq = new ArrayDeque(); + + adq.push("A"); + adq.push("B"); + adq.push("C"); + adq.push("D"); + adq.push("E"); + + System.out.print("Popping the stack:"); + + while(adq.peek()!=null) + System.out.print(adq.pop()+" "); + + System.out.println(); + + } + +} diff --git a/dataStructure/ArrayListDemo.java b/dataStructure/ArrayListDemo.java index f0aae7e..24e2663 100644 --- a/dataStructure/ArrayListDemo.java +++ b/dataStructure/ArrayListDemo.java @@ -1,33 +1,33 @@ -package dataStructure; -import java.util.ArrayList; - -public class ArrayListDemo { - - public static void main(String[] args) { - - //Create an array list - ArrayList al = new ArrayList(); - System.out.println("Initial size of al:" + al.size()); - - //Add elements to array list - al.add("A"); - al.add("M"); - al.add("A"); - al.add("N"); - al.add("S"); - al.add("s1"); - System.out.println("Size of al after additions:" + al.size()); - - //Display the array list - System.out.println("Contents of al:" + al); - - //Remove elements from the array - al.remove("s1"); - - System.out.println("Size of al after deletions:"+ al.size()); - System.out.println("Contents of al:" + al); - - - } - -} +package dataStructure; +import java.util.ArrayList; + +public class ArrayListDemo { + + public static void main(String[] args) { + + //Create an array list + ArrayList al = new ArrayList(); + System.out.println("Initial size of al:" + al.size()); + + //Add elements to array list + al.add("A"); + al.add("M"); + al.add("A"); + al.add("N"); + al.add("S"); + al.add("s1"); + System.out.println("Size of al after additions:" + al.size()); + + //Display the array list + System.out.println("Contents of al:" + al); + + //Remove elements from the array + al.remove("s1"); + + System.out.println("Size of al after deletions:"+ al.size()); + System.out.println("Contents of al:" + al); + + + } + +} diff --git a/dataStructure/HashMapIntro.java b/dataStructure/HashMapIntro.java index bebd6c3..c2d8ae6 100644 --- a/dataStructure/HashMapIntro.java +++ b/dataStructure/HashMapIntro.java @@ -1,28 +1,28 @@ -package dataStructure; -import java.util.HashMap; -public class HashMapIntro -{ - - public static void main(String[] args) - { - // Create an empty hash map - HashMap map = new HashMap<>(); - - // Add elements to the map - map.put("vishal", 10); - map.put("sachin", 30); - map.put("vaibhav", 20); - - // Print size and content - System.out.println("Size of map is:- " + map.size()); - System.out.println(map); - - // Check if a key is present and if present, print value - if ( map.containsKey("vishal")) - { - Integer a = map.get("vishal"); - System.out.println("value for key vishal is:- "+ a); - } - } - -} +package dataStructure; +import java.util.HashMap; +public class HashMapIntro +{ + + public static void main(String[] args) + { + // Create an empty hash map + HashMap map = new HashMap<>(); + + // Add elements to the map + map.put("vishal", 10); + map.put("sachin", 30); + map.put("vaibhav", 20); + + // Print size and content + System.out.println("Size of map is:- " + map.size()); + System.out.println(map); + + // Check if a key is present and if present, print value + if ( map.containsKey("vishal")) + { + Integer a = map.get("vishal"); + System.out.println("value for key vishal is:- "+ a); + } + } + +} diff --git a/dataStructure/LinkedListDemo.java b/dataStructure/LinkedListDemo.java index 994ff70..3999230 100644 --- a/dataStructure/LinkedListDemo.java +++ b/dataStructure/LinkedListDemo.java @@ -1,35 +1,35 @@ -package dataStructure; -import java.util.LinkedList; -public class LinkedListDemo { - - public static void main(String[] args) { - // Create a linked list - LinkedList ll = new LinkedList<>(); - - // Add elements to the linked list - ll.add("A"); - ll.add("M"); - ll.add("M"); - ll.add("N"); - ll.add("S"); - ll.addFirst("S1"); - ll.addLast("S2"); - ll.add(1,"A1"); - System.out.println("Original contents of ll:"+ll); - - //Remove elements from the linked list - ll.remove("A1"); - System.out.println("Content of ll after deletion:"+ll); - - //Remove first & last elements - ll.removeFirst(); - ll.removeLast(); - System.out.println("ll after deleting first & last:" + ll); - - //Get & set value - String val = ll.get(2); - ll.set(2, val + "Changed"); - System.out.println("ll after change:"+ll); - } - -} +package dataStructure; +import java.util.LinkedList; +public class LinkedListDemo { + + public static void main(String[] args) { + // Create a linked list + LinkedList ll = new LinkedList<>(); + + // Add elements to the linked list + ll.add("A"); + ll.add("M"); + ll.add("M"); + ll.add("N"); + ll.add("S"); + ll.addFirst("S1"); + ll.addLast("S2"); + ll.add(1,"A1"); + System.out.println("Original contents of ll:"+ll); + + //Remove elements from the linked list + ll.remove("A1"); + System.out.println("Content of ll after deletion:"+ll); + + //Remove first & last elements + ll.removeFirst(); + ll.removeLast(); + System.out.println("ll after deleting first & last:" + ll); + + //Get & set value + String val = ll.get(2); + ll.set(2, val + "Changed"); + System.out.println("ll after change:"+ll); + } + +} diff --git a/dataStructure/List.java b/dataStructure/List.java new file mode 100644 index 0000000..7d36733 --- /dev/null +++ b/dataStructure/List.java @@ -0,0 +1,14 @@ +package dataStructure; + +import java.util.LinkedList; +public class List{ + public static void main(String[] args) { + + LinkedList ll = new LinkedList<>(); + + ll.add("Aman"); + ll.add("Soni"); + ll.add(1, "Kumar"); + System.out.println(ll); + } +} diff --git a/dataStructure/MYStackByList.java b/dataStructure/MYStackByList.java new file mode 100644 index 0000000..3e78f56 --- /dev/null +++ b/dataStructure/MYStackByList.java @@ -0,0 +1,121 @@ +package dataStructure; + +//Java program to Implement a stack +//using singly linked list +//import package +import static java.lang.System.exit; + +//Create Stack Using Linked list +class StackUsingLinkedlist { + + // A linked list node + private class Node { + + int data; // integer data + Node link; // reference variable Node type + } + // create global top reference variable global + Node top; + // Constructor + StackUsingLinkedlist() + { + this.top = null; + } + + // Utility function to add an element x in the stack + public void push(int x) // insert at the beginning + { + // create new node temp and allocate memory + Node temp = new Node(); + + // initialize data into data field + temp.data = x; + + // put top reference into link + temp.link = top; + + // update top reference + top = temp; + } + + // Utility function to check if the stack is empty or not + public boolean isEmpty() + { + return top == null; + } + + // Utility function to return top element in a stack + public int peek() + { + // check for empty stack + if (!isEmpty()) { + return top.data; + } + else { + System.out.println("Stack is empty"); + return -1; + } + } + + // Utility function to pop top element from the stack + public void pop() // remove at the beginning + { + // check for stack underflow + if (top == null) { + System.out.print("\nStack Underflow"); + return; + } + + // update the top pointer to point to the next node + top = (top).link; + } + + public void display() + { + // check for stack underflow + if (top == null) { + System.out.printf("\nStack Underflow"); + exit(1); + } + else { + Node temp = top; + while (temp != null) { + + // print node data + System.out.printf("%d->", temp.data); + + // assign temp link to temp + temp = temp.link; + } + } + } +} +//main class +public class MYStackByList { + public static void main(String[] args) + { + // create Object of Implementing class + StackUsingLinkedlist obj = new StackUsingLinkedlist(); + // insert Stack value + obj.push(11); + obj.push(22); + obj.push(33); + obj.push(44); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + + // Delete top element of Stack + obj.pop(); + obj.pop(); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + } +} diff --git a/dataStructure/MyArrayList.java b/dataStructure/MyArrayList.java new file mode 100644 index 0000000..e356e85 --- /dev/null +++ b/dataStructure/MyArrayList.java @@ -0,0 +1,33 @@ +package dataStructure; +import java.util.ArrayList; + +public class MyArrayList { + + public static void main(String[] args) { + + //Create an array list + ArrayList al = new ArrayList(); + System.out.println("Initial size of al:" + al.size()); + + //Add elements to array list + al.add("A"); + al.add("M"); + al.add("A"); + al.add("N"); + al.add("S"); + al.add("s1"); + System.out.println("Size of al after additions:" + al.size()); + + //Display the array list + System.out.println("Contents of al:" + al); + + //Remove elements from the array + al.remove("s1"); + + System.out.println("Size of al after deletions:"+ al.size()); + System.out.println("Contents of al:" + al); + + + } + +} diff --git a/dataStructure/MyHashMap.java b/dataStructure/MyHashMap.java new file mode 100644 index 0000000..18bd450 --- /dev/null +++ b/dataStructure/MyHashMap.java @@ -0,0 +1,28 @@ +package dataStructure; +import java.util.HashMap; +public class MyHashMap +{ + + public static void main(String[] args) + { + // Create an empty hash map + HashMap map = new HashMap<>(); + + // Add elements to the map + map.put("vishal", 10); + map.put("sachin", 30); + map.put("vaibhav", 20); + + // Print size and content + System.out.println("Size of map is:- " + map.size()); + System.out.println(map); + + // Check if a key is present and if present, print value + if ( map.containsKey("vishal")) + { + Integer a = map.get("vishal"); + System.out.println("value for key vishal is:- "+ a); + } + } + +} diff --git a/dataStructure/MyHashSet.java b/dataStructure/MyHashSet.java index e0b82f4..e9b3b38 100644 --- a/dataStructure/MyHashSet.java +++ b/dataStructure/MyHashSet.java @@ -1,10 +1,21 @@ -package dataStructure; - -public class MyHashSet { - - public static void main(String[] args) { - // TODO Auto-generated method stub - - } - -} +package dataStructure; + +import java.util.HashSet; + +public class MyHashSet { + + public static void main(String[] args) { + + HashSet hs = new HashSet<>(); + + hs.add("Beta"); + hs.add("Alpha"); + hs.add("Gama"); + hs.add("Eplision"); + hs.add("Omega"); + + System.out.println(hs); + + } + +} diff --git a/dataStructure/MyLinkedList.java b/dataStructure/MyLinkedList.java new file mode 100644 index 0000000..d7a578e --- /dev/null +++ b/dataStructure/MyLinkedList.java @@ -0,0 +1,35 @@ +package dataStructure; +import java.util.LinkedList; +public class MyLinkedList { + + public static void main(String[] args) { + // Create a linked list + LinkedList ll = new LinkedList<>(); + + // Add elements to the linked list + ll.add("A"); + ll.add("M"); + ll.add("M"); + ll.add("N"); + ll.add("S"); + ll.addFirst("S1"); + ll.addLast("S2"); + ll.add(1,"A1"); + System.out.println("Original contents of ll:"+ll); + + //Remove elements from the linked list + ll.remove("A1"); + System.out.println("Content of ll after deletion:"+ll); + + //Remove first & last elements + ll.removeFirst(); + ll.removeLast(); + System.out.println("ll after deleting first & last:" + ll); + + //Get & set value + String val = ll.get(2); + ll.set(2, val + "Changed"); + System.out.println("ll after change:"+ll); + } + +} diff --git a/dataStructure/MyQueue.java b/dataStructure/MyQueue.java new file mode 100644 index 0000000..fb64284 --- /dev/null +++ b/dataStructure/MyQueue.java @@ -0,0 +1,82 @@ +//Java program for array implementation of queue +package dataStructure; +//A class to represent a queue +class Queue{ + int front, rear, size; + int capacity; + int array[]; + + public Queue(int capacity) { + this.capacity = capacity; + front = this.size = 0; + rear = capacity - 1; + array = new int[this.capacity]; + } + + //Queue is full when size becomes equal to capacity + boolean isFull(Queue queue) { + return(queue.size == queue.capacity); + } + + //Queue is Empty when size is 0 + boolean isEmpty(Queue queue){ + return(queue.size == 0); + } + + //Method to add an item to the queue + //It changes rear and size + void enqueue(int item) { + if(isFull(this)) + return; + this.rear = (this.rear+1)%this.capacity; + this.array[this.rear] = item; + this.size = this.size+1; + System.out.println(item + " enqueud to queue"); + } + + //Method to remove an item from queue + //It changes front and size + int dequeue() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + int item = this.array[this.front]; + this.front = (this.front + 1) % this.capacity; + this.size = this.size-1; + return item; + + } + + //Method to get front of queue + int front() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.front]; + } + + //Method to get rear of queue + int rear() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.rear]; + } +} + +//Driver Class +public class MyQueue{ + public static void main(String[] args){ + + Queue queue = new Queue(1000); + + queue.enqueue(10); + queue.enqueue(20); + queue.enqueue(30); + queue.enqueue(40); + + System.out.println(queue.dequeue()+(" dequeued from queue")); + System.out.println("Front item is " + queue.front()); + System.out.println("Rear item is "+ queue.rear()); + } +} diff --git a/dataStructure/MyStack.java b/dataStructure/MyStack.java new file mode 100644 index 0000000..b6c1039 --- /dev/null +++ b/dataStructure/MyStack.java @@ -0,0 +1,58 @@ +package dataStructure; + + class Stack { + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; + + boolean isEmpty() { + return (top<0); + } + + Stack(){ + top = -1; + } + + boolean push(int x) { + if(top>= (MAX-1)) { + System.out.println(x+"pushed into stack"); + return false; + } + else { + a[++top] = x; + System.out.println(x+"pushed into stack"); + return true; + } + } + + int pop() { + if(top<0) { + System.out.println("Stack underflow"); + return 0; + } + else { + int x = a[top--]; + return x; + } + } + + int peek() { + if(top<0) { + System.out.println("Stack underflow"); + return 0; + } + else { + int x = a[top]; return x; + } + } + + class MyStack{ + public void main (String[] args) { + Stack s = new Stack(); + s.push(10); + s.push(20); + s.push(30); + System.out.println(s.pop() + " Popped from stack"); + } + } +} diff --git a/dataStructure/SetExample.java b/dataStructure/SetExample.java new file mode 100644 index 0000000..be90ac9 --- /dev/null +++ b/dataStructure/SetExample.java @@ -0,0 +1,34 @@ +package dataStructure; +// Java program to demonstrate the +// union, intersection and difference +// operations on sets +import java.util.*; +public class SetExample +{ + public static void main(String args[]) + { + Set a = new HashSet(); + a.addAll(Arrays.asList(new Integer[] {1, 3, 2, 4, 8, 9, 0})); + Set b = new HashSet(); + b.addAll(Arrays.asList(new Integer[] {1, 3, 7, 5, 4, 0, 7, 5})); + + // To find union + Set union = new HashSet(a); + union.addAll(b); + System.out.print("Union of the two Set"); + System.out.println(union); + + // To find intersection + Set intersection = new HashSet(a); + + intersection.retainAll(b); + System.out.print("Intersection of the two Set"); + System.out.println(intersection); + + // To find the symmetric difference + Set difference = new HashSet(a); + difference.removeAll(b); + System.out.print("Difference of the two Set"); + System.out.println(difference); + } +} diff --git a/dataStructure/Tree.java b/dataStructure/Tree.java new file mode 100644 index 0000000..c7cbfb9 --- /dev/null +++ b/dataStructure/Tree.java @@ -0,0 +1,52 @@ +package dataStructure; + +//class containing left & right child of current node & key value +class Node{ + int key; + Node left,right; + public Node(int item) { + key = item; + left = right = null; + } +} + +// A java program to introduce Binary Tree +public class Tree { + + //Root node of binary tree + Node root; + + //Constructors + Tree(int key){ + root = new Node(key); + } + + Tree(){ + root = null; + } + public static void main(String[] args) { + Tree t = new Tree(); + //create root + t.root = new Node(1); + + /* following is the tree after above statement + 1 + / \ + null null*/ + + t.root.left = new Node(2); + t.root.right = new Node(3); + /* 2 & 3 become left & right children of 1 + * 1 + * / \ + * 2 3 + * / \ | \ + * null null null null + */ + t.root.left.left = new Node(4); + t.root.right.right = new Node(5); + t.root.right.left = new Node(6); + t.root.left.right = new Node(7); + + } +} diff --git a/oops/A.java b/oops/A.java index face387..cd46f29 100644 --- a/oops/A.java +++ b/oops/A.java @@ -1,10 +1,10 @@ -package oops; - -public class A { -class B{ - int age; -} -static class C{ - String name; -} -} +package oops; + +public class A { + class B{ + int age; + } + static class C{ + String name; + } +} diff --git a/oops/MYStaticKeyword.java b/oops/MYStaticKeyword.java new file mode 100644 index 0000000..1bd6b92 --- /dev/null +++ b/oops/MYStaticKeyword.java @@ -0,0 +1,22 @@ +package oops; +import oops.A.C; +public class MYStaticKeyword { + + static { + System.out.println("in block 1"); + } + static { + System.out.println("in block 2"); + } + + public static void main(String[] args) { + + A objA = new A(); + @SuppressWarnings("unused") + A.B objB = objA.new B(); + @SuppressWarnings("unused") + C objC = new A.C(); + System.out.println("inside main"); + } + +} diff --git a/oops/Mor.java b/oops/Mor.java index 263c1c7..ee5d5db 100644 --- a/oops/Mor.java +++ b/oops/Mor.java @@ -1,13 +1,13 @@ -package oops; - -public class Mor extends Watches { - - @Override - public void Digital() { - System.out.println("Method_overriding is digital"); - } - public void Royal() { - System.out.println("Method_overriding is royal"); - } - -} +package oops; + +public class Mor extends Watches { + + @Override + public void Digital() { + System.out.println("Method_overriding is digital"); + } + public void Royal() { + System.out.println("Method_overriding is royal"); + } + +} diff --git a/oops/MyConstructor.java b/oops/MyConstructor.java index eadde6c..742700d 100644 --- a/oops/MyConstructor.java +++ b/oops/MyConstructor.java @@ -1,12 +1,13 @@ -package oops; - -public class MyConstructor { - - MyConstructor(){ - System.out.println("Obj is now created"); - } - public static void main(String[] args) { - MyConstructor obj = new MyConstructor(); - } - -} +package oops; + +public class MyConstructor { + + MyConstructor(){ + System.out.println("Obj is now created"); + } + public static void main(String[] args) { + @SuppressWarnings("unused") + MyConstructor obj = new MyConstructor(); + } + +} diff --git a/oops/Person.java b/oops/Person.java index d17564a..053e8c3 100644 --- a/oops/Person.java +++ b/oops/Person.java @@ -1,7 +1,7 @@ -package oops; - -public class Person { -int age; -String name; -final static String breed = "HomoSapiens"; -} +package oops; + +public class Person { +int age; +String name; +final static String breed = "HomoSapiens"; +} diff --git a/oops/RepairShop.java b/oops/RepairShop.java index f119ee3..8946250 100644 --- a/oops/RepairShop.java +++ b/oops/RepairShop.java @@ -1,20 +1,20 @@ -package oops; - -public class RepairShop { - - - public static void repair (Watches ws) { - System.out.println("watch is repaired"); - } - public static void repair(Sonata sn) { - System.out.println("watch is repaired"); - } - public static void main(String[] args) { - Mor Mo = new Mor(); - Sonata sn = new Sonata(); - - repair(Mo); - repair(sn); - } - -} +package oops; + +public class RepairShop { + + + public static void repair (Watches ws) { + System.out.println("watch is repaired"); + } + public static void repair(Sonata sn) { + System.out.println("watch is repaired"); + } + public static void main(String[] args) { + Mor Mo = new Mor(); + Sonata sn = new Sonata(); + + repair(Mo); + repair(sn); + } + +} diff --git a/oops/Sonata.java b/oops/Sonata.java index 7d8972d..73aefbc 100644 --- a/oops/Sonata.java +++ b/oops/Sonata.java @@ -1,12 +1,12 @@ -package oops; - -public class Sonata extends Watches{ - - @Override - public void Digital() { - System.out.println("Sonata is digital"); - } - public void Royal() { - System.out.println("Sonata is royal"); - } -} +package oops; + +public class Sonata extends Watches{ + + @Override + public void Digital() { + System.out.println("Sonata is digital"); + } + public void Royal() { + System.out.println("Sonata is royal"); + } +} diff --git a/oops/StaticKeyword.java b/oops/StaticKeyword.java index ce2e6d9..d0052c4 100644 --- a/oops/StaticKeyword.java +++ b/oops/StaticKeyword.java @@ -1,15 +1,13 @@ -package oops; - -public class StaticKeyword { - public static void main(String[] args) { - Person obj = new Person(); - obj.name ="Aman"; - obj.age = 19; - System.out.println(obj.name); - System.out.println(obj.age + " age"); - System.out.println(obj.breed); - - - } - -} +package oops; + +public class StaticKeyword { + public static void main(String[] args) { + Person obj = new Person(); + obj.name ="Aman"; + obj.age = 19; + System.out.println(obj.name); + System.out.println(obj.age + " age"); + System.out.println(Person.breed); + } + +} diff --git a/oops/Vehicle.java b/oops/Vehicle.java index b6058cb..46e9d09 100644 --- a/oops/Vehicle.java +++ b/oops/Vehicle.java @@ -1,19 +1,19 @@ -package oops; - -class transport{ - int wheels; - transport(){ - wheels = 4; - } -} - -public class Vehicle { - - public static void main(String[] args) { - - transport car = new transport(); - System.out.println(car.wheels + " wheels"); - - } - -} +package oops; + +class transport{ + int wheels; + transport(){ + wheels = 4; + } +} + +public class Vehicle { + + public static void main(String[] args) { + + transport car = new transport(); + System.out.println(car.wheels + " wheels"); + + } + +} diff --git a/oops/Watches.java b/oops/Watches.java index 863a6d9..54a7fb5 100644 --- a/oops/Watches.java +++ b/oops/Watches.java @@ -1,8 +1,8 @@ -package oops; - -public abstract class Watches { - - public abstract void Digital(); - public abstract void Royal(); - -} +package oops; + +public abstract class Watches { + + public abstract void Digital(); + public abstract void Royal(); + +} diff --git a/oopsEncapsulation/EncapIntro.java b/oopsEncapsulation/EncapIntro.java index e069086..e0ac979 100644 --- a/oopsEncapsulation/EncapIntro.java +++ b/oopsEncapsulation/EncapIntro.java @@ -1,17 +1,17 @@ -package oopsEncapsulation; - -public class EncapIntro { - - public static void main(String[] args) { - - S obj = new S(); - - obj.setName("Milan"); - obj.setAge(16); - System.out.println(obj.getName()); - System.out.println(obj.getAge()); - - - } - -} +package oopsEncapsulation; + +public class EncapIntro { + + public static void main(String[] args) { + + S obj = new S(); + + obj.setName("Milan"); + obj.setAge(16); + System.out.println(obj.getName()); + System.out.println(obj.getAge()); + + + } + +} diff --git a/oopsEncapsulation/S.java b/oopsEncapsulation/S.java index f8de03c..92fdfb6 100644 --- a/oopsEncapsulation/S.java +++ b/oopsEncapsulation/S.java @@ -1,30 +1,30 @@ -package oopsEncapsulation; - -public class S { - - /*we have to put the variables private to achieve encapsulation*/ - private int age; - - private String name; - - //setter methods - public void setAge(int age) { - if(age>20) System.out.println(" You are to old:"); - else this.age = age; - } - - public void setName(String name) { - this.name = name; - } - - //getter methods - public int getAge() { - return age; - } - - public String getName() { - return name; - } - - -} +package oopsEncapsulation; + +public class S { + + /*we have to put the variables private to achieve encapsulation*/ + private int age; + + private String name; + + //setter methods + public void setAge(int age) { + if(age>20) System.out.println(" You are to old:"); + else this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + //getter methods + public int getAge() { + return age; + } + + public String getName() { + return name; + } + + +} diff --git a/oopsabstraction/Audi.java b/oopsabstraction/Audi.java new file mode 100644 index 0000000..da5de77 --- /dev/null +++ b/oopsabstraction/Audi.java @@ -0,0 +1,14 @@ +package oopsabstraction; + +public class Audi extends Car{ + + public void accelerate() { + System.out.println("Audi is accelerating"); + } + @Override + public void apply_break() { + System.out.println("Audi is breaking"); + + } + +} diff --git a/oopsabstraction/Car.java b/oopsabstraction/Car.java new file mode 100644 index 0000000..d6f8f09 --- /dev/null +++ b/oopsabstraction/Car.java @@ -0,0 +1,8 @@ +package oopsabstraction; + +public abstract class Car { + + public abstract void accelerate(); + public abstract void apply_break(); + +} diff --git a/oopsabstraction/RepairShop.java b/oopsabstraction/RepairShop.java new file mode 100644 index 0000000..e8a1bdd --- /dev/null +++ b/oopsabstraction/RepairShop.java @@ -0,0 +1,21 @@ +package oopsabstraction; + +public class RepairShop { + + public static void repairCar(Car car) { + System.out.println("car is repaired"); + } + + public static void repairCar(Car...Audi) { + System.out.println("car is repaired"); + } + + public static void main(String[] args) { + WagonR wagonR = new WagonR(); + Audi audi = new Audi(); + + repairCar(wagonR); + repairCar(audi); + } + +} diff --git a/oopsabstraction/WagonR.java b/oopsabstraction/WagonR.java new file mode 100644 index 0000000..61d96f1 --- /dev/null +++ b/oopsabstraction/WagonR.java @@ -0,0 +1,20 @@ +package oopsabstraction; + +public class WagonR extends Car{ + + public static void main(String[] args) { + + } + @Override + public void accelerate() { + + System.out.println("WagonR is accelerating"); + } + + @Override + public void apply_break() { + + System.out.println("break is applied in WagonR"); + } + +} diff --git a/oopsinheritance/MainClass.java b/oopsinheritance/MainClass.java index c4c5dd6..21e8ede 100644 --- a/oopsinheritance/MainClass.java +++ b/oopsinheritance/MainClass.java @@ -1,20 +1,20 @@ -package oopsinheritance; - -public class MainClass { - - public static void main(String[] args) { - - Teacher T = new Teacher(); - T.name = "Mr Harry "; - T.eat(); - T.walk(); - T.teach(); - - Singer s = new Singer(); - s.name = "Justin B "; - s.sing(); - s.eat(); - - } - -} +package oopsinheritance; + +public class MainClass { + + public static void main(String[] args) { + + Teacher T = new Teacher(); + T.name = "Mr Harry "; + T.eat(); + T.walk(); + T.teach(); + + Singer s = new Singer(); + s.name = "Justin B "; + s.sing(); + s.eat(); + + } + +} diff --git a/oopsinheritance/Person.java b/oopsinheritance/Person.java index d4c4805..86f1b66 100644 --- a/oopsinheritance/Person.java +++ b/oopsinheritance/Person.java @@ -1,15 +1,15 @@ -package oopsinheritance; - -public class Person { - - protected String name; - - public void walk() { - System.out.println(name + "is walking"); - } - - public void eat() { - System.out.println(name + "is eating"); - } - -} +package oopsinheritance; + +public class Person { + + protected String name; + + public void walk() { + System.out.println(name + "is walking"); + } + + public void eat() { + System.out.println(name + "is eating"); + } + +} diff --git a/oopsinheritance/Singer.java b/oopsinheritance/Singer.java index 0d39ff4..3e33f78 100644 --- a/oopsinheritance/Singer.java +++ b/oopsinheritance/Singer.java @@ -1,9 +1,9 @@ -package oopsinheritance; - -public class Singer extends Person { - - public void sing() { - System.out.println(name + "is singing"); - } - -} +package oopsinheritance; + +public class Singer extends Person { + + public void sing() { + System.out.println(name + "is singing"); + } + +} diff --git a/oopsinheritance/Teacher.java b/oopsinheritance/Teacher.java index 71e38d8..378aa0a 100644 --- a/oopsinheritance/Teacher.java +++ b/oopsinheritance/Teacher.java @@ -1,9 +1,9 @@ -package oopsinheritance; - -public class Teacher extends Person { - - public void teach() { - System.out.println(name + "is teaching"); - } - -} +package oopsinheritance; + +public class Teacher extends Person { + + public void teach() { + System.out.println(name + "is teaching"); + } + +} diff --git a/patternsByloops/Pattern1.java b/patternsByloops/Pattern1.java index 4546ec7..9035d26 100644 --- a/patternsByloops/Pattern1.java +++ b/patternsByloops/Pattern1.java @@ -1,18 +1,18 @@ -package patternsByloops; - -public class Pattern1 { - - public static void main(String[] args) { - - int n = 6; - - for(int i= 0;i=i;j--) //j for horizontal stars - System.out.print("* "); //printing the stars - - System.out.println(); - } - } - -} +package patternsByloops; +public class Pattern2 { + + public static void main(String[] args) { + + int w = 5; //size of the pattern + + for(int i=1;i<=w;i++){ //i for vertical Stars + + for(int j=w;j>=i;j--) //j for horizontal stars + System.out.print("* "); //printing the stars + + System.out.println(); + } + } + +} diff --git a/patternsByloops/Pattern3.java b/patternsByloops/Pattern3.java index d2a20ca..bb0efe3 100644 --- a/patternsByloops/Pattern3.java +++ b/patternsByloops/Pattern3.java @@ -1,18 +1,18 @@ -package patternsByloops; - -public class Pattern3 { - - public static void main(String[] args) { - - int n = 6; //size of the pattern - - for(int i=1;i<=n;i++) { //i for horizontal stars operation - - for(int j=n-1;j>=i;j--) //j for vertical stars operation - System.out.print("* "); //printing the stars - - System.out.println(); // for new line - } - } - -} +package patternsByloops; + +public class Pattern3 { + + public static void main(String[] args) { + + int n = 6; //size of the pattern + + for(int i=1;i<=n;i++) { //i for horizontal stars operation + + for(int j=n-1;j>=i;j--) //j for vertical stars operation + System.out.print("* "); //printing the stars + + System.out.println(); // for new line + } + } + +} diff --git a/patternsByloops/Pattern4.java b/patternsByloops/Pattern4.java index 3635f7e..052976f 100644 --- a/patternsByloops/Pattern4.java +++ b/patternsByloops/Pattern4.java @@ -1,20 +1,20 @@ -package patternsByloops; - -public class Pattern4 { - public static void main(String[] args) { - - int n=6; //size of the pattern - - for(int i=n;i>=1;i--){ //i for horizontal stars - - for(int j=n-1;j>=i;j--) //j for vertical and for print & handling the spaces - System.out.print(" "); - - for(int k=1;k<=i;k++) //k variable for print the values - System.out.print("* "); //printing the stars - - System.out.println(); - } - } - -} +package patternsByloops; + +public class Pattern4 { + public static void main(String[] args) { + + int n=6; //size of the pattern + + for(int i=n;i>=1;i--){ //i for horizontal stars + + for(int j=n-1;j>=i;j--) //j for vertical and for print & handling the spaces + System.out.print(" "); + + for(int k=1;k<=i;k++) //k variable for print the values + System.out.print("* "); //printing the stars + + System.out.println(); + } + } + +} diff --git a/patternsByloops/Pattern5.java b/patternsByloops/Pattern5.java index a93e554..832905f 100644 --- a/patternsByloops/Pattern5.java +++ b/patternsByloops/Pattern5.java @@ -1,30 +1,30 @@ -package patternsByloops; - -public class Pattern5 { - - public static void main(String[] args) { - - int min_stars =0; /*change value to set minimum no. of stars in pyramid - take odd no.for odd no. of stars in each row 1-3-5 - etc - take even no. for even no. stars in each row,2-4-6 etc */ - - int p_height=6; //change value to increase or decrease the size of pyramid - - int p_space = p_height - 1; - - for(int i= 1;ii;j--) - System.out.print(" "); - - for(int k=0;k<=min_stars;k++) - System.out.print("* "); - - min_stars+=2; - System.out.println(); - } - - }//end of main -}//end of class +package patternsByloops; + +public class Pattern5 { + + public static void main(String[] args) { + + int min_stars =0; /*change value to set minimum no. of stars in pyramid + take odd no.for odd no. of stars in each row 1-3-5 + etc + take even no. for even no. stars in each row,2-4-6 etc */ + + int p_height=6; //change value to increase or decrease the size of pyramid + + int p_space = p_height - 1; + + for(int i= 1;ii;j--) + System.out.print(" "); + + for(int k=0;k<=min_stars;k++) + System.out.print("* "); + + min_stars+=2; + System.out.println(); + } + + }//end of main +}//end of class diff --git a/patternsByloops/Pattern5_2.java b/patternsByloops/Pattern5_2.java index 3728cec..9b56a9c 100644 --- a/patternsByloops/Pattern5_2.java +++ b/patternsByloops/Pattern5_2.java @@ -1,24 +1,24 @@ -package patternsByloops; - -public class Pattern5_2 { - - public static void main(String[] args) { - - int n = 5; //size of pattern - - int px =n; //left print control - int py =n; //right print control - - for(int i=1;i<=n;i++) //i for horizontal stars - { - for(int j=1;j=px && j<=py) System.out.print("* "); //if condition is true then print the stars - else System.out.print(" "); //else print the spaces - - px--; //post decrementing the left print control - py++; //post incrementing the right print control - System.out.println(); - } - - } -} +package patternsByloops; + +public class Pattern5_2 { + + public static void main(String[] args) { + + int n = 5; //size of pattern + + int px =n; //left print control + int py =n; //right print control + + for(int i=1;i<=n;i++) //i for horizontal stars + { + for(int j=1;j=px && j<=py) System.out.print("* "); //if condition is true then print the stars + else System.out.print(" "); //else print the spaces + + px--; //post decrementing the left print control + py++; //post incrementing the right print control + System.out.println(); + } + + } +} diff --git a/patternsByloops/Pattern6.java b/patternsByloops/Pattern6.java index efb017c..ff01e91 100644 --- a/patternsByloops/Pattern6.java +++ b/patternsByloops/Pattern6.java @@ -1,21 +1,21 @@ -package patternsByloops; - -public class Pattern6 { - - public static void main(String[] args) - { - int n=5; - //size of pattern - for(int i= n;i>=1;i--) - { - for(int j=n-1;j>=i;j--) // loop for print the spaces - System.out.print(" "); //printing the spaces - - for(int k =1;k<=i;k++) // loop for print the stars - System.out.print("*"); //printing the stars - - System.out.println(); //for new line - }//end of loop - - }//end of main -}//end of class +package patternsByloops; + +public class Pattern6 { + + public static void main(String[] args) + { + int n=5; + //size of pattern + for(int i= n;i>=1;i--) + { + for(int j=n-1;j>=i;j--) // loop for print the spaces + System.out.print(" "); //printing the spaces + + for(int k =1;k<=i;k++) // loop for print the stars + System.out.print("*"); //printing the stars + + System.out.println(); //for new line + }//end of loop + + }//end of main +}//end of class diff --git a/patternsByloops/Pattern7.java b/patternsByloops/Pattern7.java index 4365cb5..ef6fd0e 100644 --- a/patternsByloops/Pattern7.java +++ b/patternsByloops/Pattern7.java @@ -1,18 +1,18 @@ -package patternsByloops; - -public class Pattern7 -{ - public static void main(String[] args) - { - - int size = 3; //size of the pattern - - for(int i=size;i>=-size;i--) //loop to print the stars - { - for( int j=size;j>=Math.abs(i);j--) //inner or nested loop for print the stars - System.out.print("* "); - - System.out.println(); //for new line - }//end of loop - }//end of main -}//end of class +package patternsByloops; + +public class Pattern7 +{ + public static void main(String[] args) + { + + int size = 3; //size of the pattern + + for(int i=size;i>=-size;i--) //loop to print the stars + { + for( int j=size;j>=Math.abs(i);j--) //inner or nested loop for print the stars + System.out.print("* "); + + System.out.println(); //for new line + }//end of loop + }//end of main +}//end of class diff --git a/patternsByloops/Pattern8.java b/patternsByloops/Pattern8.java index 8ef97cb..b8478b0 100644 --- a/patternsByloops/Pattern8.java +++ b/patternsByloops/Pattern8.java @@ -1,21 +1,21 @@ -package patternsByloops; - -public class Pattern8 -{ - public static void main(String[] args) - { - int size = 3; //size of pattern - - for(int i=size;i>=-size;i--) { //loop for handling spaces & stars - - for(int j=1;j<=Math.abs(i);j++) //inner or nested loop to handle or print spaces - System.out.print(" "); //printing the spaces - - for(int k=size;k>=Math.abs(i);k--) //inner or nested loop to handle or print stars - System.out.print("*"); //printing the stars - - System.out.println(); //for new line - }//end of loop - - }//end of main -}//end of class +package patternsByloops; + +public class Pattern8 +{ + public static void main(String[] args) + { + int size = 3; //size of pattern + + for(int i=size;i>=-size;i--) { //loop for handling spaces & stars + + for(int j=1;j<=Math.abs(i);j++) //inner or nested loop to handle or print spaces + System.out.print(" "); //printing the spaces + + for(int k=size;k>=Math.abs(i);k--) //inner or nested loop to handle or print stars + System.out.print("*"); //printing the stars + + System.out.println(); //for new line + }//end of loop + + }//end of main +}//end of class diff --git a/patternsByloops/Pattern9.java b/patternsByloops/Pattern9.java index d9ea106..eff73d4 100644 --- a/patternsByloops/Pattern9.java +++ b/patternsByloops/Pattern9.java @@ -1,19 +1,19 @@ -package patternsByloops; - -public class Pattern9 { - - public static void main(String[] args) { - - int size = 3; //size of pattern - for(int i=size;i>=-size;i--) //loop for handle stars & spaces - { - for(int j=1;j<=Math.abs(i);j++) //inner or nested loop for spaces - System.out.print(" "); //printing the spaces - - for(int k=size;k>=Math.abs(i);k--) //inner or nested loop for stars - System.out.print("* "); //printing the stars - - System.out.println(); //new line - } - }//end of main -}//end of class +package patternsByloops; + +public class Pattern9 { + + public static void main(String[] args) { + + int size = 3; //size of pattern + for(int i=size;i>=-size;i--) //loop for handle stars & spaces + { + for(int j=1;j<=Math.abs(i);j++) //inner or nested loop for spaces + System.out.print(" "); //printing the spaces + + for(int k=size;k>=Math.abs(i);k--) //inner or nested loop for stars + System.out.print("* "); //printing the stars + + System.out.println(); //new line + } + }//end of main +}//end of class diff --git a/queues/ArrayDequeDemo.java b/queues/ArrayDequeDemo.java new file mode 100644 index 0000000..1a2485c --- /dev/null +++ b/queues/ArrayDequeDemo.java @@ -0,0 +1,24 @@ +package queues; +import java.util.*; +public class ArrayDequeDemo { + + public static void main(String[] args) { + + ArrayDeque adq = new ArrayDeque(); + + adq.push("A"); + adq.push("B"); + adq.push("C"); + adq.push("D"); + adq.push("E"); + + System.out.print("Popping the stack:"); + + while(adq.peek()!=null) + System.out.print(adq.pop()+" "); + + System.out.println(); + + } + +} diff --git a/sdeProblems/FindDuplicate.java b/sdeProblems/FindDuplicate.java index df101dc..ddead95 100644 --- a/sdeProblems/FindDuplicate.java +++ b/sdeProblems/FindDuplicate.java @@ -1,32 +1,32 @@ -package sdeProblems; - -public class FindDuplicate { - - // Implementing an array as a HashMap for finding duplicate elements - void printRepeating(int m[],int size) { - - int i; - System.out.print("The repeating elements are:"); - - for(i=0;i=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; - else System.out.print(Math.abs(m[i]) + " "); - } - } - - //Driver Method - public static void main(String[] args) { - - FindDuplicate duplicate = new FindDuplicate(); // Making an object of FindDuplicate class - - int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array - int m_size = m.length; // variables which store size or length of array - - duplicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements - - } - - } +package sdeProblems; + +public class FindDuplicate { + + // Implementing an array as a HashMap for finding duplicate elements + void printRepeating(int m[],int size) { + + int i; + System.out.print("The repeating elements are:"); + + for(i=0;i=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; + else System.out.print(Math.abs(m[i]) + " "); + } + } + + //Driver Method + public static void main(String[] args) { + + FindDuplicate duplicate = new FindDuplicate(); // Making an object of FindDuplicate class + + int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array + int m_size = m.length; // variables which store size or length of array + + duplicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements + + } + + } diff --git a/sdeProblems/RotateArray.java b/sdeProblems/RotateArray.java index 69b6ef1..9a47dc3 100644 --- a/sdeProblems/RotateArray.java +++ b/sdeProblems/RotateArray.java @@ -1,38 +1,38 @@ -package sdeProblems; - -public class RotateArray { - - //function to rotate the elements of an array - - void rotateL(int a[], int d, int n) { - for(int i= 0;i=0; i--) - { - /* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater - element is not found */ - int j, last = arr1[m-1]; - - for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--) - arr1[j+1] = arr1[j]; - - // If there was a greater element - if (j != m-2 || last > arr2[i]) { - arr1[j+1] = arr2[i]; - arr2[i] = last; } - } - } - - // Driver method to test the above function - public static void main1(String[] args) - { - merge(arr1.length,arr2.length); - System.out.print("After Merging nFirst Array: "); - System.out.println(Arrays.toString(arr1)); - System.out.print("Second Array: "); - System.out.println(Arrays.toString(arr2)); - } -} +package sdeProblems; +import java.util.Arrays; + +public class Test +{ + static int arr1[] = new int[]{1, 5, 9, 10, 15, 20}; + static int arr2[] = new int[]{2, 3, 8, 13}; + + static void merge(int m, int n) + { + // Iterate through all elements of ar2[] starting from to the last element + for (int i=n-1; i>=0; i--) + { + /* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater + element is not found */ + int j, last = arr1[m-1]; + + for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--) + arr1[j+1] = arr1[j]; + + // If there was a greater element + if (j != m-2 || last > arr2[i]) { + arr1[j+1] = arr2[i]; + arr2[i] = last; } + } + } + + // Driver method to test the above function + public static void main1(String[] args) + { + merge(arr1.length,arr2.length); + System.out.print("After Merging nFirst Array: "); + System.out.println(Arrays.toString(arr1)); + System.out.print("Second Array: "); + System.out.println(Arrays.toString(arr2)); + } +} diff --git a/sortingAlgorithms/BubbleSort.java b/sortingAlgorithms/BubbleSort.java index 4bd2323..29d2fe0 100644 --- a/sortingAlgorithms/BubbleSort.java +++ b/sortingAlgorithms/BubbleSort.java @@ -1,30 +1,30 @@ -package sortingAlgorithms; - -public class BubbleSort { - - public static void main(String[] args) { - - int a[] = {7,9,-4,6,8,5}; - int n = a.length; - - for(int i=0;i<=n-1-i;i++) { - for(int j=0;j<=n-1-i;j++) { - - boolean sorted = true; - - if(a[j+1] < a[j]){ - int temp = a[j+1]; - a[j+1] = a[j]; - a[j]= temp; - sorted = false; - } - - if(sorted) break; - } - } - - for(int item:a) - System.out.print(item+" "); - - }//end of main -}//end of class +package sortingAlgorithms; + +public class BubbleSort { + + public static void main(String[] args) { + + int a[] = {7,9,-4,6,8,5}; + int n = a.length; + + for(int i=0;i<=n-1-i;i++) { + for(int j=0;j<=n-1-i;j++) { + + boolean sorted = true; + + if(a[j+1] < a[j]){ + int temp = a[j+1]; + a[j+1] = a[j]; + a[j]= temp; + sorted = false; + } + + if(sorted) break; + } + } + + for(int item:a) + System.out.print(item+" "); + + }//end of main +}//end of class diff --git a/sortingAlgorithms/InsertionSort.java b/sortingAlgorithms/InsertionSort.java index 2efb58c..486988f 100644 --- a/sortingAlgorithms/InsertionSort.java +++ b/sortingAlgorithms/InsertionSort.java @@ -1,42 +1,42 @@ -package sortingAlgorithms; - -public class InsertionSort { - - /*Function to sort array using insertion sort*/ - void sort(int arr[]) - { - int n = arr.length; - for (int i = 1; i < n; ++i) { - int key = arr[i]; - int j = i - 1; - - while (j >= 0 && arr[j] > key) { - arr[j + 1] = arr[j]; - j = j - 1; - } - arr[j + 1] = key; - } - } - - /* A utility function to print array of size n*/ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i = 0; i < n; ++i) - System.out.print(arr[i] + " "); - - System.out.println(); - } - - // Driver method - public static void main(String args[]) - { - int arr[] = { 12, 11, 13, 5, 6 }; - - InsertionSort ob = new InsertionSort(); - ob.sort(arr); - - printArray(arr); - } -} - +package sortingAlgorithms; + +public class InsertionSort { + + /*Function to sort array using insertion sort*/ + void sort(int arr[]) + { + int n = arr.length; + for (int i = 1; i < n; ++i) { + int key = arr[i]; + int j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + /* A utility function to print array of size n*/ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + + System.out.println(); + } + + // Driver method + public static void main(String args[]) + { + int arr[] = { 12, 11, 13, 5, 6 }; + + InsertionSort ob = new InsertionSort(); + ob.sort(arr); + + printArray(arr); + } +} + diff --git a/sortingAlgorithms/SelectionSort.java b/sortingAlgorithms/SelectionSort.java index a517475..4add59c 100644 --- a/sortingAlgorithms/SelectionSort.java +++ b/sortingAlgorithms/SelectionSort.java @@ -1,42 +1,42 @@ -package sortingAlgorithms; - -public class SelectionSort { - - void sort(int arr[]) - { - int n = arr.length; - - // One by one move boundary of unsorted sub-array - for (int i = 0; i < n-1; i++) { - // Find the minimum element in unsorted array - int min_idx = i; - for (int j = i+1; j < n; j++) - if (arr[j] < arr[min_idx]) - min_idx = j; - - // Swap the found minimum element with the first - // element - int temp = arr[min_idx]; - arr[min_idx] = arr[i]; - arr[i] = temp; - } - } - - // Prints the array - void printArray(int arr[]) { - int n = arr.length; - for (int i=0; i", temp.data); + + // assign temp link to temp + temp = temp.link; + } + } + } +} +//main class +public class MYStackByList { + public static void main(String[] args) + { + // create Object of Implementing class + StackUsingLinkedlist obj = new StackUsingLinkedlist(); + // insert Stack value + obj.push(11); + obj.push(22); + obj.push(33); + obj.push(44); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + + // Delete top element of Stack + obj.pop(); + obj.pop(); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + } +} diff --git a/trees/FindFullNodesInABinaryTree.java b/trees/FindFullNodesInABinaryTree.java new file mode 100644 index 0000000..71ffeb3 --- /dev/null +++ b/trees/FindFullNodesInABinaryTree.java @@ -0,0 +1,47 @@ +package trees; + +//A Binary Tree Node +class Node{ + int data; + Node left , right; + Node(int data) { + this.data = data; + left = right = null ; + + } +} + +public class FindFullNodesInABinaryTree { + + //Traverse given tree in In-order fashion & prints all nodes that have both children as non-empty + public static void findFullNode(Node root) { + if (root!=null) { + findFullNode(root.left); + if(root.left != null && root.right != null) + System.out.println(root.data +" "); + findFullNode(root.right); + } + } + + //Driver method + public static void main(String[] args) { + Node root = new Node(1); + root.left = new Node(2); + root.right = new Node(3); + root.left.left = new Node(4); + root.left.right = new Node(5); + root.right.left = new Node(6); + root.right.right = new Node(7); + root.right.right.right = new Node(8); + root.right.left.right = new Node(9); + root.right.left.left = new Node(10); + root.left.left.left= new Node(11); + root.left.left.right = new Node(12); + root.left.right.left = new Node(13); + root.left.right.right = new Node(14); + + findFullNode(root); + + } + +} diff --git a/trees/Insertion_In_BinaryTree.java b/trees/Insertion_In_BinaryTree.java new file mode 100644 index 0000000..8cd7c6d --- /dev/null +++ b/trees/Insertion_In_BinaryTree.java @@ -0,0 +1,78 @@ +package trees; + + // Java program to insert element in binary tree +import java.util.LinkedList; +import java.util.Queue; +public class Insertion_In_BinaryTree { + /* A binary tree node has key, pointer to + left child and a pointer to right child */ + static class Node { + int key; + Node left, right; + + // constructor + Node(int key){ + this.key = key; + left = right = null; + } + } + static Node root; + static Node temp = root; + + //Inorder traversal of a binary tree + static void inorder(Node temp) + { + if (temp == null) + return; + + inorder(temp.left); + System.out.print(temp.key+" "); + inorder(temp.right); + } + + //function to insert element in binary tree + static void insert(Node temp, int key) + { + Queue q = new LinkedList(); + q.add(temp); + + // Do level order traversal until we find + // an empty place. + while (!q.isEmpty()) { + temp = q.peek(); + q.remove(); + + if (temp.left == null) { + temp.left = new Node(key); + break; + } else + q.add(temp.left); + + if (temp.right == null) { + temp.right = new Node(key); + break; + } else + q.add(temp.right); + } + } + + // Driver code + public static void main(String args[]) + { + root = new Node(10); + root.left = new Node(11); + root.left.left = new Node(7); + root.right = new Node(9); + root.right.left = new Node(15); + root.right.right = new Node(8); + + System.out.print( "Inorder traversal before insertion:"); + inorder(root); + + int key = 12; + insert(root, key); + + System.out.print("\nInorder traversal after insertion:"); + inorder(root); + } +} From 877cdc3a6a199f2da1cf0bad6c565f51f310f6c1 Mon Sep 17 00:00:00 2001 From: gitaman8481 <65482419+gitaman8481@users.noreply.github.com> Date: Tue, 25 Aug 2020 11:56:53 +0530 Subject: [PATCH 05/10] JAva program update --- dataStructure/MYStackByList.java | 121 +++++++++++++++++++++++++++++++ dataStructure/MyQueue.java | 82 +++++++++++++++++++++ dataStructure/MyStack.java | 58 +++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 dataStructure/MYStackByList.java create mode 100644 dataStructure/MyQueue.java create mode 100644 dataStructure/MyStack.java diff --git a/dataStructure/MYStackByList.java b/dataStructure/MYStackByList.java new file mode 100644 index 0000000..01fea0f --- /dev/null +++ b/dataStructure/MYStackByList.java @@ -0,0 +1,121 @@ +package dataStructure; + +//Java program to Implement a stack +//using singly linked list +//import package +import static java.lang.System.exit; + +//Create Stack Using Linked list +class StackUsingLinkedlist { + + // A linked list node + private class Node { + + int data; // integer data + Node link; // reference variable Node type + } + // create global top reference variable global + Node top; + // Constructor + StackUsingLinkedlist() + { + this.top = null; + } + + // Utility function to add an element x in the stack + public void push(int x) // insert at the beginning + { + // create new node temp and allocate memory + Node temp = new Node(); + + // initialize data into data field + temp.data = x; + + // put top reference into link + temp.link = top; + + // update top reference + top = temp; + } + + // Utility function to check if the stack is empty or not + public boolean isEmpty() + { + return top == null; + } + + // Utility function to return top element in a stack + public int peek() + { + // check for empty stack + if (!isEmpty()) { + return top.data; + } + else { + System.out.println("Stack is empty"); + return -1; + } + } + + // Utility function to pop top element from the stack + public void pop() // remove at the beginning + { + // check for stack underflow + if (top == null) { + System.out.print("\nStack Underflow"); + return; + } + + // update the top pointer to point to the next node + top = (top).link; + } + + public void display() + { + // check for stack underflow + if (top == null) { + System.out.printf("\nStack Underflow"); + exit(1); + } + else { + Node temp = top; + while (temp != null) { + + // print node data + System.out.printf("%d->", temp.data); + + // assign temp link to temp + temp = temp.link; + } + } + } +} +//main class +public class MYStackByList { + public static void main(String[] args) + { + // create Object of Implementing class + StackUsingLinkedlist obj = new StackUsingLinkedlist(); + // insert Stack value + obj.push(11); + obj.push(22); + obj.push(33); + obj.push(44); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + + // Delete top element of Stack + obj.pop(); + obj.pop(); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + } +} diff --git a/dataStructure/MyQueue.java b/dataStructure/MyQueue.java new file mode 100644 index 0000000..0cb291e --- /dev/null +++ b/dataStructure/MyQueue.java @@ -0,0 +1,82 @@ +//Java program for array implementation of queue +package dataStructure; +//A class to represent a queue +class Queue{ + int front, rear, size; + int capacity; + int array[]; + + public Queue(int capacity) { + this.capacity = capacity; + front = this.size = 0; + rear = capacity - 1; + array = new int[this.capacity]; + } + + //Queue is full when size becomes equal to capacity + boolean isFull(Queue queue) { + return(queue.size == queue.capacity); + } + + //Queue is Empty when size is 0 + boolean isEmpty(Queue queue){ + return(queue.size == 0); + } + + //Method to add an item to the queue + //It changes rear and size + void enqueue(int item) { + if(isFull(this)) + return; + this.rear = (this.rear+1)%this.capacity; + this.array[this.rear] = item; + this.size = this.size+1; + System.out.println(item + " enqueud to queue"); + } + + //Method to remove an item from queue + //It changes front and size + int dequeue() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + int item = this.array[this.front]; + this.front = (this.front + 1) % this.capacity; + this.size = this.size-1; + return item; + + } + + //Method to get front of queue + int front() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.front]; + } + + //Method to get rear of queue + int rear() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.rear]; + } +} + +//Driver Class +public class MyQueue{ + public static void main(String[] args){ + + Queue queue = new Queue(1000); + + queue.enqueue(10); + queue.enqueue(20); + queue.enqueue(30); + queue.enqueue(40); + + System.out.println(queue.dequeue()+(" dequeued from queue")); + System.out.println("Front item is " + queue.front()); + System.out.println("Rear item is "+ queue.rear()); + } +} diff --git a/dataStructure/MyStack.java b/dataStructure/MyStack.java new file mode 100644 index 0000000..0c45b42 --- /dev/null +++ b/dataStructure/MyStack.java @@ -0,0 +1,58 @@ +package dataStructure; + + class Stack { + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; + + boolean isEmpty() { + return (top<0); + } + + Stack(){ + top = -1; + } + + boolean push(int x) { + if(top>= (MAX-1)) { + System.out.println(x+"pushed into stack"); + return false; + } + else { + a[++top] = x; + System.out.println(x+"pushed into stack"); + return true; + } + } + + int pop() { + if(top<0) { + System.out.println("Stack underflow"); + return 0; + } + else { + int x = a[top--]; + return x; + } + } + + int peek() { + if(top<0) { + System.out.println("Stack underflow"); + return 0; + } + else { + int x = a[top]; return x; + } + } + + class MyStack{ + public void main (String[] args) { + Stack s = new Stack(); + s.push(10); + s.push(20); + s.push(30); + System.out.println(s.pop() + " Popped from stack"); + } + } +} From 3d95301ebe81e74d07806e81e62aa0e828f90bad Mon Sep 17 00:00:00 2001 From: Aman Soni <65482419+gitaman8481@users.noreply.github.com> Date: Thu, 27 Aug 2020 11:00:39 +0530 Subject: [PATCH 06/10] Add files via upload --- arrays/Maximum_subarray_problem.java | 44 +++++++++ arrays/RotateArray.java | 38 +++++++ ...out_using_extra_space_or_sorting_algo.java | 38 +++++++ basicProblems/Factorial.java | 19 ++++ basicProblems/Fibonacci_Series.java | 24 +++++ .../Multiplicative_Table_till_20.java | 20 ++++ basicProblems/Palindrome_Number.java | 13 +++ basicProblems/Prime_Number_Or_Not.java | 21 ++++ basicProblems/Series_Sum_1.java | 20 ++++ basicProblems/Series_Sum_2.java | 32 ++++++ basicProblems/Swap_two_numbers.java | 22 +++++ basicProblems/X_raisedTo_power_Y.java | 18 ++++ oopspolymorphism/Animal.java | 5 + oopspolymorphism/Dog.java | 10 ++ oopspolymorphism/MainClass.java | 29 ++++++ oopspolymorphism/Pet.java | 9 ++ recursion/Factorial_using_Recursion.java | 21 ++++ searchingAlgorithms/BinarySearch.java | 46 +++++++++ searchingAlgorithms/LinearSearch.java | 28 ++++++ searchingAlgorithms/package-info.java | 1 + sortingAlgorithms/BubbleSort.java | 58 ++++++----- sortingAlgorithms/HeapSort.java | 77 +++++++++++++++ sortingAlgorithms/MergeSort.java | 98 +++++++++++++++++++ sortingAlgorithms/QuickSort.java | 80 +++++++++++++++ sortingAlgorithms/RadixSort.java | 73 ++++++++++++++ 25 files changed, 821 insertions(+), 23 deletions(-) create mode 100644 arrays/Maximum_subarray_problem.java create mode 100644 arrays/RotateArray.java create mode 100644 arrays/Sort_an_array_of_0s_1s_2s_without_using_extra_space_or_sorting_algo.java create mode 100644 basicProblems/Factorial.java create mode 100644 basicProblems/Fibonacci_Series.java create mode 100644 basicProblems/Multiplicative_Table_till_20.java create mode 100644 basicProblems/Palindrome_Number.java create mode 100644 basicProblems/Prime_Number_Or_Not.java create mode 100644 basicProblems/Series_Sum_1.java create mode 100644 basicProblems/Series_Sum_2.java create mode 100644 basicProblems/Swap_two_numbers.java create mode 100644 basicProblems/X_raisedTo_power_Y.java create mode 100644 oopspolymorphism/Animal.java create mode 100644 oopspolymorphism/Dog.java create mode 100644 oopspolymorphism/MainClass.java create mode 100644 oopspolymorphism/Pet.java create mode 100644 recursion/Factorial_using_Recursion.java create mode 100644 searchingAlgorithms/BinarySearch.java create mode 100644 searchingAlgorithms/LinearSearch.java create mode 100644 searchingAlgorithms/package-info.java create mode 100644 sortingAlgorithms/HeapSort.java create mode 100644 sortingAlgorithms/MergeSort.java create mode 100644 sortingAlgorithms/QuickSort.java create mode 100644 sortingAlgorithms/RadixSort.java diff --git a/arrays/Maximum_subarray_problem.java b/arrays/Maximum_subarray_problem.java new file mode 100644 index 0000000..8e1fed5 --- /dev/null +++ b/arrays/Maximum_subarray_problem.java @@ -0,0 +1,44 @@ +package arrays; + +public class Maximum_subarray_problem { + // Java program to print largest + // contiguous array sum + + static void maxSubArraySum(int a[], int size) + { + int max_so_far = Integer.MIN_VALUE, + max_ending_here = 0,start = 0, + end = 0, s = 0; + + for (int i = 0; i < size; i++) + { + max_ending_here += a[i]; + + if (max_so_far < max_ending_here) + { + max_so_far = max_ending_here; + start = s; + end = i; + } + + if (max_ending_here < 0) + { + max_ending_here = 0; + s = i + 1; + } + } + System.out.println("Maximum contiguous sum is " + + max_so_far); + System.out.println("Starting index " + start); + System.out.println("Ending index " + end); + } + + // Driver code + public static void main(String[] args) + { + int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 }; + int n = a.length; + maxSubArraySum(a, n); + } + } + diff --git a/arrays/RotateArray.java b/arrays/RotateArray.java new file mode 100644 index 0000000..4610918 --- /dev/null +++ b/arrays/RotateArray.java @@ -0,0 +1,38 @@ +package arrays; + +public class RotateArray { + + //function to rotate the elements of an array + + void rotateL(int a[], int d, int n) { + for(int i= 0;i=0; i--) + { + /* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater + element is not found */ + int j, last = arr1[m-1]; + + for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--) + arr1[j+1] = arr1[j]; + + // If there was a greater element + if (j != m-2 || last > arr2[i]) { + arr1[j+1] = arr2[i]; + arr2[i] = last; + + } + } + } + + // Driver method to test the above function + public static void main1(String[] args) + { + merge(arr1.length,arr2.length); + System.out.print("After Merging nFirst Array: "); + System.out.println(Arrays.toString(arr1)); + System.out.print("Second Array: "); + System.out.println(Arrays.toString(arr2)); + } +} diff --git a/basicProblems/Factorial.java b/basicProblems/Factorial.java new file mode 100644 index 0000000..607f2b3 --- /dev/null +++ b/basicProblems/Factorial.java @@ -0,0 +1,19 @@ +package basicProblems; +import java.util.*; +public class Factorial { + + public static void main(String[] args) { + int fact=1; + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + for(int i = n ; i>=1;i--) { + fact = fact*i; + } + + System.out.println("fact of " + n + " is " + fact ); + } +} + + diff --git a/basicProblems/Fibonacci_Series.java b/basicProblems/Fibonacci_Series.java new file mode 100644 index 0000000..e73fd61 --- /dev/null +++ b/basicProblems/Fibonacci_Series.java @@ -0,0 +1,24 @@ +package basicProblems; +import java.util.*; +public class Fibonacci_Series { + + public static void main(String[] args) { + + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int a=0,b=1; + + System.out.println(a + " "); + System.out.println(b + " "); + + for(int i=0;i= 1) + return num * multiplyNumbers(num - 1); + else + return 1; + } + public static void main(String[] args) { + + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + long factorial = multiplyNumbers(num); + System.out.println("Factorial of " + num + " = " + factorial); + } + +} diff --git a/searchingAlgorithms/BinarySearch.java b/searchingAlgorithms/BinarySearch.java new file mode 100644 index 0000000..4de771c --- /dev/null +++ b/searchingAlgorithms/BinarySearch.java @@ -0,0 +1,46 @@ +package searchingAlgorithms; + +public class BinarySearch { + + // Returns index of x if it is present in arr[l.. + // r], else return -1 + int binarySearch(int arr[], int l, int r, int x) + { + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the + // middle itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not present + // in array + return -1; + } + + // Driver method to test above + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40, 50, 90, 130, 230, 740}; + int n = arr.length; + int x = 130; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at index " + result); + } +} + diff --git a/searchingAlgorithms/LinearSearch.java b/searchingAlgorithms/LinearSearch.java new file mode 100644 index 0000000..4bdf529 --- /dev/null +++ b/searchingAlgorithms/LinearSearch.java @@ -0,0 +1,28 @@ +package searchingAlgorithms; + +public class LinearSearch { + + public static int search(int arr[], int x) + { + int n = arr.length; + for(int i = 0; i < n; i++) + { + if(arr[i] == x) + return i; + } + return -1; + } + + public static void main(String args[]) + { + int arr[] = { 1, 2, 3, 4, 10, 40, 50, 60, 90, 80, 70}; + int x = 90; + + int result = search(arr, x); + if(result == -1) + System.out.print("Element is not present in array"); + else + System.out.print("Element is present at index " + result); + } + +} diff --git a/searchingAlgorithms/package-info.java b/searchingAlgorithms/package-info.java new file mode 100644 index 0000000..65ec78b --- /dev/null +++ b/searchingAlgorithms/package-info.java @@ -0,0 +1 @@ +package searchingAlgorithms; diff --git a/sortingAlgorithms/BubbleSort.java b/sortingAlgorithms/BubbleSort.java index 29d2fe0..035055e 100644 --- a/sortingAlgorithms/BubbleSort.java +++ b/sortingAlgorithms/BubbleSort.java @@ -1,30 +1,42 @@ package sortingAlgorithms; public class BubbleSort { - - public static void main(String[] args) { - - int a[] = {7,9,-4,6,8,5}; + + void bubbleSort(int a[]) { + int n = a.length; - - for(int i=0;i<=n-1-i;i++) { - for(int j=0;j<=n-1-i;j++) { - - boolean sorted = true; - - if(a[j+1] < a[j]){ - int temp = a[j+1]; - a[j+1] = a[j]; - a[j]= temp; - sorted = false; + for(int i=0;ia[j+1]) { + //swap a[j+1] and a[i] + int temp = a[j]; + a[j] = a[j+1]; + a[j+1] = temp; } - - if(sorted) break; - } + } } + } + + //function to print the array + void printArray(int a[]) { + + int n = a.length; + for(int i=0;i= 0; i--) + heapify(A, n, i); + + // One by one extract an element from heap + for (int i=n-1; i>0; i--) + { + // Move current root to end + int temp = A[0]; + A[0] = A[i]; + A[i] = temp; + + // call max heapify on the reduced heap + heapify(A, i, 0); + } + } + + // To heapify a subtree rooted with node i which is + // an index in arr[]. n is size of heap + void heapify(int A[], int n, int i) + { + int largest = i; // Initialize largest as root + int l = 2*i + 1; // left = 2*i + 1 + int r = 2*i + 2; // right = 2*i + 2 + + // If left child is larger than root + if (l < n && A[l] > A[largest]) + largest = l; + + // If right child is larger than largest so far + if (r < n && A[r] > A[largest]) + largest = r; + + // If largest is not root + if (largest != i) + { + int swap = A[i]; + A[i] = A[largest]; + A[largest] = swap; + + // Recursively heapify the affected sub-tree + heapify(A, n, largest); + } + } + + /* A utility function to print array of size n */ + static void printArray(int A[]) + { + int n = A.length; + for (int i=0; i Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + if (low < high) + { + /* pi is partitioning index, arr[pi] is + now at right place */ + int pi = partition(arr, low, high); + + // Recursively sort elements before + // partition and after partition + sort(arr, low, pi-1); + sort(arr, pi+1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i mx) + mx = arr[i]; + return mx; + } + + // A function to do counting sort of arr[] according to + // the digit represented by exp. + static void countSort(int arr[], int n, int exp) { + int output[] = new int[n]; // output array + int i; + int count[] = new int[10]; + Arrays.fill(count,0); + + // Store count of occurrences in count[] + for (i = 0; i < n; i++) + count[ (arr[i]/exp)%10 ]++; + + // Change count[i] so that count[i] now contains + // actual position of this digit in output[] + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + + // Build the output array + for (i = n - 1; i >= 0; i--) + { + output[count[ (arr[i]/exp)%10 ] - 1] = arr[i]; + count[ (arr[i]/exp)%10 ]--; + } + + // Copy the output array to arr[], so that arr[] now + // contains sorted numbers according to curent digit + for (i = 0; i < n; i++) + arr[i] = output[i]; + } + + // The main function to that sorts arr[] of size n using + // Radix Sort + static void radixsort(int arr[], int n) { + // Find the maximum number to know number of digits + int m = getMax(arr, n); + + // Do counting sort for every digit. Note that instead + // of passing digit number, exp is passed. exp is 10^i + // where i is current digit number + for (int exp = 1; m/exp > 0; exp *= 10) + countSort(arr, n, exp); + } + + // A utility function to print an array + static void print(int arr[], int n) { + for (int i=0; i Date: Sat, 12 Sep 2020 14:30:30 +0530 Subject: [PATCH 07/10] this is repo to be commited --- arrays/Maximum_subarray_problem.java | 44 +++++++ arrays/RotateArray.java | 38 ++++++ ...out_using_extra_space_or_sorting_algo.java | 38 ++++++ basicProblems/Factorial.java | 19 +++ basicProblems/Fibonacci_Series.java | 24 ++++ .../Multiplicative_Table_till_20.java | 20 +++ basicProblems/Palindrome_Number.java | 13 ++ basicProblems/Prime_Number_Or_Not.java | 21 +++ basicProblems/Series_Sum_1.java | 20 +++ basicProblems/Series_Sum_2.java | 32 +++++ basicProblems/Swap_two_numbers.java | 22 ++++ basicProblems/X_raisedTo_power_Y.java | 18 +++ .../List.java | 2 +- dataStructure/MyArrayList.java | 33 +++++ dataStructure/MyHashMap.java | 28 ++++ dataStructure/MyHashSet.java | 15 ++- dataStructure/MyLinkedList.java | 35 +++++ dataStructure/SetExample.java | 34 +++++ dataStructure/Tree.java | 52 ++++++++ oops/A.java | 12 +- oops/MYStaticKeyword.java | 22 ++++ oops/MyConstructor.java | 1 + oops/StaticKeyword.java | 4 +- oopsabstraction/Audi.java | 14 ++ oopsabstraction/Car.java | 8 ++ oopsabstraction/RepairShop.java | 21 +++ oopsabstraction/WagonR.java | 20 +++ oopspolymorphism/Animal.java | 5 + oopspolymorphism/Dog.java | 10 ++ oopspolymorphism/MainClass.java | 29 +++++ oopspolymorphism/Pet.java | 9 ++ queues/ArrayDequeDemo.java | 24 ++++ recursion/Factorial_using_Recursion.java | 21 +++ searchingAlgorithms/BinarySearch.java | 46 +++++++ searchingAlgorithms/LinearSearch.java | 28 ++++ searchingAlgorithms/package-info.java | 1 + sortingAlgorithms/BubbleSort.java | 58 +++++---- sortingAlgorithms/HeapSort.java | 77 +++++++++++ sortingAlgorithms/MergeSort.java | 98 ++++++++++++++ sortingAlgorithms/QuickSort.java | 80 ++++++++++++ sortingAlgorithms/RadixSort.java | 73 +++++++++++ stack/MYStackByList.java | 121 ++++++++++++++++++ trees/FindFullNodesInABinaryTree.java | 47 +++++++ trees/Insertion_In_BinaryTree.java | 78 +++++++++++ 44 files changed, 1380 insertions(+), 35 deletions(-) create mode 100644 arrays/Maximum_subarray_problem.java create mode 100644 arrays/RotateArray.java create mode 100644 arrays/Sort_an_array_of_0s_1s_2s_without_using_extra_space_or_sorting_algo.java create mode 100644 basicProblems/Factorial.java create mode 100644 basicProblems/Fibonacci_Series.java create mode 100644 basicProblems/Multiplicative_Table_till_20.java create mode 100644 basicProblems/Palindrome_Number.java create mode 100644 basicProblems/Prime_Number_Or_Not.java create mode 100644 basicProblems/Series_Sum_1.java create mode 100644 basicProblems/Series_Sum_2.java create mode 100644 basicProblems/Swap_two_numbers.java create mode 100644 basicProblems/X_raisedTo_power_Y.java rename {CollectionFramework => dataStructure}/List.java (89%) create mode 100644 dataStructure/MyArrayList.java create mode 100644 dataStructure/MyHashMap.java create mode 100644 dataStructure/MyLinkedList.java create mode 100644 dataStructure/SetExample.java create mode 100644 dataStructure/Tree.java create mode 100644 oops/MYStaticKeyword.java create mode 100644 oopsabstraction/Audi.java create mode 100644 oopsabstraction/Car.java create mode 100644 oopsabstraction/RepairShop.java create mode 100644 oopsabstraction/WagonR.java create mode 100644 oopspolymorphism/Animal.java create mode 100644 oopspolymorphism/Dog.java create mode 100644 oopspolymorphism/MainClass.java create mode 100644 oopspolymorphism/Pet.java create mode 100644 queues/ArrayDequeDemo.java create mode 100644 recursion/Factorial_using_Recursion.java create mode 100644 searchingAlgorithms/BinarySearch.java create mode 100644 searchingAlgorithms/LinearSearch.java create mode 100644 searchingAlgorithms/package-info.java create mode 100644 sortingAlgorithms/HeapSort.java create mode 100644 sortingAlgorithms/MergeSort.java create mode 100644 sortingAlgorithms/QuickSort.java create mode 100644 sortingAlgorithms/RadixSort.java create mode 100644 stack/MYStackByList.java create mode 100644 trees/FindFullNodesInABinaryTree.java create mode 100644 trees/Insertion_In_BinaryTree.java diff --git a/arrays/Maximum_subarray_problem.java b/arrays/Maximum_subarray_problem.java new file mode 100644 index 0000000..724dcf7 --- /dev/null +++ b/arrays/Maximum_subarray_problem.java @@ -0,0 +1,44 @@ +package arrays; + +public class Maximum_subarray_problem { + // Java program to print largest + // contiguous array sum + + static void maxSubArraySum(int a[], int size) + { + int max_so_far = Integer.MIN_VALUE, + max_ending_here = 0,start = 0, + end = 0, s = 0; + + for (int i = 0; i < size; i++) + { + max_ending_here += a[i]; + + if (max_so_far < max_ending_here) + { + max_so_far = max_ending_here; + start = s; + end = i; + } + + if (max_ending_here < 0) + { + max_ending_here = 0; + s = i + 1; + } + } + System.out.println("Maximum contiguous sum is " + + max_so_far); + System.out.println("Starting index " + start); + System.out.println("Ending index " + end); + } + + // Driver code + public static void main(String[] args) + { + int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 }; + int n = a.length; + maxSubArraySum(a, n); + } + } + diff --git a/arrays/RotateArray.java b/arrays/RotateArray.java new file mode 100644 index 0000000..00691e4 --- /dev/null +++ b/arrays/RotateArray.java @@ -0,0 +1,38 @@ +package arrays; + +public class RotateArray { + + //function to rotate the elements of an array + + void rotateL(int a[], int d, int n) { + for(int i= 0;i=0; i--) + { + /* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater + element is not found */ + int j, last = arr1[m-1]; + + for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--) + arr1[j+1] = arr1[j]; + + // If there was a greater element + if (j != m-2 || last > arr2[i]) { + arr1[j+1] = arr2[i]; + arr2[i] = last; + + } + } + } + + // Driver method to test the above function + public static void main1(String[] args) + { + merge(arr1.length,arr2.length); + System.out.print("After Merging nFirst Array: "); + System.out.println(Arrays.toString(arr1)); + System.out.print("Second Array: "); + System.out.println(Arrays.toString(arr2)); + } +} diff --git a/basicProblems/Factorial.java b/basicProblems/Factorial.java new file mode 100644 index 0000000..f4a4355 --- /dev/null +++ b/basicProblems/Factorial.java @@ -0,0 +1,19 @@ +package basicProblems; +import java.util.*; +public class Factorial { + + public static void main(String[] args) { + int fact=1; + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + for(int i = n ; i>=1;i--) { + fact = fact*i; + } + + System.out.println("fact of " + n + " is " + fact ); + } +} + + diff --git a/basicProblems/Fibonacci_Series.java b/basicProblems/Fibonacci_Series.java new file mode 100644 index 0000000..2772bc1 --- /dev/null +++ b/basicProblems/Fibonacci_Series.java @@ -0,0 +1,24 @@ +package basicProblems; +import java.util.*; +public class Fibonacci_Series { + + public static void main(String[] args) { + + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int a=0,b=1; + + System.out.println(a + " "); + System.out.println(b + " "); + + for(int i=0;i al = new ArrayList(); + System.out.println("Initial size of al:" + al.size()); + + //Add elements to array list + al.add("A"); + al.add("M"); + al.add("A"); + al.add("N"); + al.add("S"); + al.add("s1"); + System.out.println("Size of al after additions:" + al.size()); + + //Display the array list + System.out.println("Contents of al:" + al); + + //Remove elements from the array + al.remove("s1"); + + System.out.println("Size of al after deletions:"+ al.size()); + System.out.println("Contents of al:" + al); + + + } + +} diff --git a/dataStructure/MyHashMap.java b/dataStructure/MyHashMap.java new file mode 100644 index 0000000..3d7cc2f --- /dev/null +++ b/dataStructure/MyHashMap.java @@ -0,0 +1,28 @@ +package dataStructure; +import java.util.HashMap; +public class MyHashMap +{ + + public static void main(String[] args) + { + // Create an empty hash map + HashMap map = new HashMap<>(); + + // Add elements to the map + map.put("vishal", 10); + map.put("sachin", 30); + map.put("vaibhav", 20); + + // Print size and content + System.out.println("Size of map is:- " + map.size()); + System.out.println(map); + + // Check if a key is present and if present, print value + if ( map.containsKey("vishal")) + { + Integer a = map.get("vishal"); + System.out.println("value for key vishal is:- "+ a); + } + } + +} diff --git a/dataStructure/MyHashSet.java b/dataStructure/MyHashSet.java index e0b82f4..7537ba5 100644 --- a/dataStructure/MyHashSet.java +++ b/dataStructure/MyHashSet.java @@ -1,10 +1,21 @@ package dataStructure; +import java.util.HashSet; + public class MyHashSet { public static void main(String[] args) { - // TODO Auto-generated method stub - + + HashSet hs = new HashSet<>(); + + hs.add("Beta"); + hs.add("Alpha"); + hs.add("Gama"); + hs.add("Eplision"); + hs.add("Omega"); + + System.out.println(hs); + } } diff --git a/dataStructure/MyLinkedList.java b/dataStructure/MyLinkedList.java new file mode 100644 index 0000000..977d3e9 --- /dev/null +++ b/dataStructure/MyLinkedList.java @@ -0,0 +1,35 @@ +package dataStructure; +import java.util.LinkedList; +public class MyLinkedList { + + public static void main(String[] args) { + // Create a linked list + LinkedList ll = new LinkedList<>(); + + // Add elements to the linked list + ll.add("A"); + ll.add("M"); + ll.add("M"); + ll.add("N"); + ll.add("S"); + ll.addFirst("S1"); + ll.addLast("S2"); + ll.add(1,"A1"); + System.out.println("Original contents of ll:"+ll); + + //Remove elements from the linked list + ll.remove("A1"); + System.out.println("Content of ll after deletion:"+ll); + + //Remove first & last elements + ll.removeFirst(); + ll.removeLast(); + System.out.println("ll after deleting first & last:" + ll); + + //Get & set value + String val = ll.get(2); + ll.set(2, val + "Changed"); + System.out.println("ll after change:"+ll); + } + +} diff --git a/dataStructure/SetExample.java b/dataStructure/SetExample.java new file mode 100644 index 0000000..ad4bb39 --- /dev/null +++ b/dataStructure/SetExample.java @@ -0,0 +1,34 @@ +package dataStructure; +// Java program to demonstrate the +// union, intersection and difference +// operations on sets +import java.util.*; +public class SetExample +{ + public static void main(String args[]) + { + Set a = new HashSet(); + a.addAll(Arrays.asList(new Integer[] {1, 3, 2, 4, 8, 9, 0})); + Set b = new HashSet(); + b.addAll(Arrays.asList(new Integer[] {1, 3, 7, 5, 4, 0, 7, 5})); + + // To find union + Set union = new HashSet(a); + union.addAll(b); + System.out.print("Union of the two Set"); + System.out.println(union); + + // To find intersection + Set intersection = new HashSet(a); + + intersection.retainAll(b); + System.out.print("Intersection of the two Set"); + System.out.println(intersection); + + // To find the symmetric difference + Set difference = new HashSet(a); + difference.removeAll(b); + System.out.print("Difference of the two Set"); + System.out.println(difference); + } +} diff --git a/dataStructure/Tree.java b/dataStructure/Tree.java new file mode 100644 index 0000000..8581c0a --- /dev/null +++ b/dataStructure/Tree.java @@ -0,0 +1,52 @@ +package dataStructure; + +//class containing left & right child of current node & key value +class Node{ + int key; + Node left,right; + public Node(int item) { + key = item; + left = right = null; + } +} + +// A java program to introduce Binary Tree +public class Tree { + + //Root node of binary tree + Node root; + + //Constructors + Tree(int key){ + root = new Node(key); + } + + Tree(){ + root = null; + } + public static void main(String[] args) { + Tree t = new Tree(); + //create root + t.root = new Node(1); + + /* following is the tree after above statement + 1 + / \ + null null*/ + + t.root.left = new Node(2); + t.root.right = new Node(3); + /* 2 & 3 become left & right children of 1 + * 1 + * / \ + * 2 3 + * / \ | \ + * null null null null + */ + t.root.left.left = new Node(4); + t.root.right.right = new Node(5); + t.root.right.left = new Node(6); + t.root.left.right = new Node(7); + + } +} diff --git a/oops/A.java b/oops/A.java index face387..aec9a69 100644 --- a/oops/A.java +++ b/oops/A.java @@ -1,10 +1,10 @@ package oops; public class A { -class B{ - int age; -} -static class C{ - String name; -} + class B{ + int age; + } + static class C{ + String name; + } } diff --git a/oops/MYStaticKeyword.java b/oops/MYStaticKeyword.java new file mode 100644 index 0000000..181746e --- /dev/null +++ b/oops/MYStaticKeyword.java @@ -0,0 +1,22 @@ +package oops; +import oops.A.C; +public class MYStaticKeyword { + + static { + System.out.println("in block 1"); + } + static { + System.out.println("in block 2"); + } + + public static void main(String[] args) { + + A objA = new A(); + @SuppressWarnings("unused") + A.B objB = objA.new B(); + @SuppressWarnings("unused") + C objC = new A.C(); + System.out.println("inside main"); + } + +} diff --git a/oops/MyConstructor.java b/oops/MyConstructor.java index eadde6c..d5ebf6b 100644 --- a/oops/MyConstructor.java +++ b/oops/MyConstructor.java @@ -6,6 +6,7 @@ public class MyConstructor { System.out.println("Obj is now created"); } public static void main(String[] args) { + @SuppressWarnings("unused") MyConstructor obj = new MyConstructor(); } diff --git a/oops/StaticKeyword.java b/oops/StaticKeyword.java index ce2e6d9..7ae5504 100644 --- a/oops/StaticKeyword.java +++ b/oops/StaticKeyword.java @@ -7,9 +7,7 @@ public static void main(String[] args) { obj.age = 19; System.out.println(obj.name); System.out.println(obj.age + " age"); - System.out.println(obj.breed); - - + System.out.println(Person.breed); } } diff --git a/oopsabstraction/Audi.java b/oopsabstraction/Audi.java new file mode 100644 index 0000000..26d5ed8 --- /dev/null +++ b/oopsabstraction/Audi.java @@ -0,0 +1,14 @@ +package oopsabstraction; + +public class Audi extends Car{ + + public void accelerate() { + System.out.println("Audi is accelerating"); + } + @Override + public void apply_break() { + System.out.println("Audi is breaking"); + + } + +} diff --git a/oopsabstraction/Car.java b/oopsabstraction/Car.java new file mode 100644 index 0000000..dceb3b5 --- /dev/null +++ b/oopsabstraction/Car.java @@ -0,0 +1,8 @@ +package oopsabstraction; + +public abstract class Car { + + public abstract void accelerate(); + public abstract void apply_break(); + +} diff --git a/oopsabstraction/RepairShop.java b/oopsabstraction/RepairShop.java new file mode 100644 index 0000000..6a09ac2 --- /dev/null +++ b/oopsabstraction/RepairShop.java @@ -0,0 +1,21 @@ +package oopsabstraction; + +public class RepairShop { + + public static void repairCar(Car car) { + System.out.println("car is repaired"); + } + + public static void repairCar(Car...Audi) { + System.out.println("car is repaired"); + } + + public static void main(String[] args) { + WagonR wagonR = new WagonR(); + Audi audi = new Audi(); + + repairCar(wagonR); + repairCar(audi); + } + +} diff --git a/oopsabstraction/WagonR.java b/oopsabstraction/WagonR.java new file mode 100644 index 0000000..8f8425b --- /dev/null +++ b/oopsabstraction/WagonR.java @@ -0,0 +1,20 @@ +package oopsabstraction; + +public class WagonR extends Car{ + + public static void main(String[] args) { + + } + @Override + public void accelerate() { + + System.out.println("WagonR is accelerating"); + } + + @Override + public void apply_break() { + + System.out.println("break is applied in WagonR"); + } + +} diff --git a/oopspolymorphism/Animal.java b/oopspolymorphism/Animal.java new file mode 100644 index 0000000..d46eeb4 --- /dev/null +++ b/oopspolymorphism/Animal.java @@ -0,0 +1,5 @@ +package oopspolymorphism; + +public class Animal { + +} diff --git a/oopspolymorphism/Dog.java b/oopspolymorphism/Dog.java new file mode 100644 index 0000000..995b0d6 --- /dev/null +++ b/oopspolymorphism/Dog.java @@ -0,0 +1,10 @@ +package oopspolymorphism; + +public class Dog extends Pet { + + public void walk() { + System.out.println("dog is walking"); + } + + +} diff --git a/oopspolymorphism/MainClass.java b/oopspolymorphism/MainClass.java new file mode 100644 index 0000000..112fc11 --- /dev/null +++ b/oopspolymorphism/MainClass.java @@ -0,0 +1,29 @@ +package oopspolymorphism; + +public class MainClass { + + + public static void greetings() { + System.out.println("Hi,there"); + } + public static void greetings(String s) { + System.out.println(s); + } + public static void greetings(String s, int count) { + for(int i=0;i adq = new ArrayDeque(); + + adq.push("A"); + adq.push("B"); + adq.push("C"); + adq.push("D"); + adq.push("E"); + + System.out.print("Popping the stack:"); + + while(adq.peek()!=null) + System.out.print(adq.pop()+" "); + + System.out.println(); + + } + +} diff --git a/recursion/Factorial_using_Recursion.java b/recursion/Factorial_using_Recursion.java new file mode 100644 index 0000000..f31d5d1 --- /dev/null +++ b/recursion/Factorial_using_Recursion.java @@ -0,0 +1,21 @@ +package recursion; +import java.util.Scanner; + +public class Factorial_using_Recursion { + public static long multiplyNumbers(int num) + { + if (num >= 1) + return num * multiplyNumbers(num - 1); + else + return 1; + } + public static void main(String[] args) { + + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + long factorial = multiplyNumbers(num); + System.out.println("Factorial of " + num + " = " + factorial); + } + +} diff --git a/searchingAlgorithms/BinarySearch.java b/searchingAlgorithms/BinarySearch.java new file mode 100644 index 0000000..d331720 --- /dev/null +++ b/searchingAlgorithms/BinarySearch.java @@ -0,0 +1,46 @@ +package searchingAlgorithms; + +public class BinarySearch { + + // Returns index of x if it is present in arr[l.. + // r], else return -1 + int binarySearch(int arr[], int l, int r, int x) + { + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the + // middle itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not present + // in array + return -1; + } + + // Driver method to test above + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40, 50, 90, 130, 230, 740}; + int n = arr.length; + int x = 130; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at index " + result); + } +} + diff --git a/searchingAlgorithms/LinearSearch.java b/searchingAlgorithms/LinearSearch.java new file mode 100644 index 0000000..ff4718c --- /dev/null +++ b/searchingAlgorithms/LinearSearch.java @@ -0,0 +1,28 @@ +package searchingAlgorithms; + +public class LinearSearch { + + public static int search(int arr[], int x) + { + int n = arr.length; + for(int i = 0; i < n; i++) + { + if(arr[i] == x) + return i; + } + return -1; + } + + public static void main(String args[]) + { + int arr[] = { 1, 2, 3, 4, 10, 40, 50, 60, 90, 80, 70}; + int x = 90; + + int result = search(arr, x); + if(result == -1) + System.out.print("Element is not present in array"); + else + System.out.print("Element is present at index " + result); + } + +} diff --git a/searchingAlgorithms/package-info.java b/searchingAlgorithms/package-info.java new file mode 100644 index 0000000..9d8805d --- /dev/null +++ b/searchingAlgorithms/package-info.java @@ -0,0 +1 @@ +package searchingAlgorithms; diff --git a/sortingAlgorithms/BubbleSort.java b/sortingAlgorithms/BubbleSort.java index 4bd2323..56ae59c 100644 --- a/sortingAlgorithms/BubbleSort.java +++ b/sortingAlgorithms/BubbleSort.java @@ -1,30 +1,42 @@ package sortingAlgorithms; public class BubbleSort { - - public static void main(String[] args) { - - int a[] = {7,9,-4,6,8,5}; + + void bubbleSort(int a[]) { + int n = a.length; - - for(int i=0;i<=n-1-i;i++) { - for(int j=0;j<=n-1-i;j++) { - - boolean sorted = true; - - if(a[j+1] < a[j]){ - int temp = a[j+1]; - a[j+1] = a[j]; - a[j]= temp; - sorted = false; + for(int i=0;ia[j+1]) { + //swap a[j+1] and a[i] + int temp = a[j]; + a[j] = a[j+1]; + a[j+1] = temp; } - - if(sorted) break; - } + } } + } + + //function to print the array + void printArray(int a[]) { + + int n = a.length; + for(int i=0;i= 0; i--) + heapify(A, n, i); + + // One by one extract an element from heap + for (int i=n-1; i>0; i--) + { + // Move current root to end + int temp = A[0]; + A[0] = A[i]; + A[i] = temp; + + // call max heapify on the reduced heap + heapify(A, i, 0); + } + } + + // To heapify a subtree rooted with node i which is + // an index in arr[]. n is size of heap + void heapify(int A[], int n, int i) + { + int largest = i; // Initialize largest as root + int l = 2*i + 1; // left = 2*i + 1 + int r = 2*i + 2; // right = 2*i + 2 + + // If left child is larger than root + if (l < n && A[l] > A[largest]) + largest = l; + + // If right child is larger than largest so far + if (r < n && A[r] > A[largest]) + largest = r; + + // If largest is not root + if (largest != i) + { + int swap = A[i]; + A[i] = A[largest]; + A[largest] = swap; + + // Recursively heapify the affected sub-tree + heapify(A, n, largest); + } + } + + /* A utility function to print array of size n */ + static void printArray(int A[]) + { + int n = A.length; + for (int i=0; i Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + if (low < high) + { + /* pi is partitioning index, arr[pi] is + now at right place */ + int pi = partition(arr, low, high); + + // Recursively sort elements before + // partition and after partition + sort(arr, low, pi-1); + sort(arr, pi+1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i mx) + mx = arr[i]; + return mx; + } + + // A function to do counting sort of arr[] according to + // the digit represented by exp. + static void countSort(int arr[], int n, int exp) { + int output[] = new int[n]; // output array + int i; + int count[] = new int[10]; + Arrays.fill(count,0); + + // Store count of occurrences in count[] + for (i = 0; i < n; i++) + count[ (arr[i]/exp)%10 ]++; + + // Change count[i] so that count[i] now contains + // actual position of this digit in output[] + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + + // Build the output array + for (i = n - 1; i >= 0; i--) + { + output[count[ (arr[i]/exp)%10 ] - 1] = arr[i]; + count[ (arr[i]/exp)%10 ]--; + } + + // Copy the output array to arr[], so that arr[] now + // contains sorted numbers according to curent digit + for (i = 0; i < n; i++) + arr[i] = output[i]; + } + + // The main function to that sorts arr[] of size n using + // Radix Sort + static void radixsort(int arr[], int n) { + // Find the maximum number to know number of digits + int m = getMax(arr, n); + + // Do counting sort for every digit. Note that instead + // of passing digit number, exp is passed. exp is 10^i + // where i is current digit number + for (int exp = 1; m/exp > 0; exp *= 10) + countSort(arr, n, exp); + } + + // A utility function to print an array + static void print(int arr[], int n) { + for (int i=0; i", temp.data); + + // assign temp link to temp + temp = temp.link; + } + } + } +} +//main class +public class MYStackByList { + public static void main(String[] args) + { + // create Object of Implementing class + StackUsingLinkedlist obj = new StackUsingLinkedlist(); + // insert Stack value + obj.push(11); + obj.push(22); + obj.push(33); + obj.push(44); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + + // Delete top element of Stack + obj.pop(); + obj.pop(); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + } +} diff --git a/trees/FindFullNodesInABinaryTree.java b/trees/FindFullNodesInABinaryTree.java new file mode 100644 index 0000000..9ef37e1 --- /dev/null +++ b/trees/FindFullNodesInABinaryTree.java @@ -0,0 +1,47 @@ +package trees; + +//A Binary Tree Node +class Node{ + int data; + Node left , right; + Node(int data) { + this.data = data; + left = right = null ; + + } +} + +public class FindFullNodesInABinaryTree { + + //Traverse given tree in In-order fashion & prints all nodes that have both children as non-empty + public static void findFullNode(Node root) { + if (root!=null) { + findFullNode(root.left); + if(root.left != null && root.right != null) + System.out.println(root.data +" "); + findFullNode(root.right); + } + } + + //Driver method + public static void main(String[] args) { + Node root = new Node(1); + root.left = new Node(2); + root.right = new Node(3); + root.left.left = new Node(4); + root.left.right = new Node(5); + root.right.left = new Node(6); + root.right.right = new Node(7); + root.right.right.right = new Node(8); + root.right.left.right = new Node(9); + root.right.left.left = new Node(10); + root.left.left.left= new Node(11); + root.left.left.right = new Node(12); + root.left.right.left = new Node(13); + root.left.right.right = new Node(14); + + findFullNode(root); + + } + +} diff --git a/trees/Insertion_In_BinaryTree.java b/trees/Insertion_In_BinaryTree.java new file mode 100644 index 0000000..98e45ad --- /dev/null +++ b/trees/Insertion_In_BinaryTree.java @@ -0,0 +1,78 @@ +package trees; + + // Java program to insert element in binary tree +import java.util.LinkedList; +import java.util.Queue; +public class Insertion_In_BinaryTree { + /* A binary tree node has key, pointer to + left child and a pointer to right child */ + static class Node { + int key; + Node left, right; + + // constructor + Node(int key){ + this.key = key; + left = right = null; + } + } + static Node root; + static Node temp = root; + + //Inorder traversal of a binary tree + static void inorder(Node temp) + { + if (temp == null) + return; + + inorder(temp.left); + System.out.print(temp.key+" "); + inorder(temp.right); + } + + //function to insert element in binary tree + static void insert(Node temp, int key) + { + Queue q = new LinkedList(); + q.add(temp); + + // Do level order traversal until we find + // an empty place. + while (!q.isEmpty()) { + temp = q.peek(); + q.remove(); + + if (temp.left == null) { + temp.left = new Node(key); + break; + } else + q.add(temp.left); + + if (temp.right == null) { + temp.right = new Node(key); + break; + } else + q.add(temp.right); + } + } + + // Driver code + public static void main(String args[]) + { + root = new Node(10); + root.left = new Node(11); + root.left.left = new Node(7); + root.right = new Node(9); + root.right.left = new Node(15); + root.right.right = new Node(8); + + System.out.print( "Inorder traversal before insertion:"); + inorder(root); + + int key = 12; + insert(root, key); + + System.out.print("\nInorder traversal after insertion:"); + inorder(root); + } +} From fabcffa24ca8806382e6f259e194f15ffcf84c40 Mon Sep 17 00:00:00 2001 From: Aman Soni <65482419+gitaman8481@users.noreply.github.com> Date: Tue, 20 Oct 2020 23:00:25 +0530 Subject: [PATCH 08/10] Modification no 2 --- basicProblems/Factorial.class | Bin 0 -> 1124 bytes src/algorithm_fundamentals/Stack.java | 10 ++ src/exercise_Problems/Exercise1.java | 33 ++++++ src/exercise_Problems/Exercise2.java | 24 +++++ src/exercise_Problems/Exercise3.java | 34 ++++++ src/graphs/Graph.java | 41 +++++++ src/list/Deletion_in_Linked_List.java | 100 ++++++++++++++++++ src/list/Insertion_in_Linked_List.java | 99 +++++++++++++++++ src/list/MainList.java | 18 ++++ src/list/MyLL.java | 53 ++++++++++ .../Cylinder.java | 26 +++++ .../Setter_Getters.java | 44 ++++++++ src/src | 1 + trees/FindFullNodesInABinaryTree.class | Bin 0 -> 1436 bytes trees/Node.class | Bin 0 -> 339 bytes 15 files changed, 483 insertions(+) create mode 100644 basicProblems/Factorial.class create mode 100644 src/algorithm_fundamentals/Stack.java create mode 100644 src/exercise_Problems/Exercise1.java create mode 100644 src/exercise_Problems/Exercise2.java create mode 100644 src/exercise_Problems/Exercise3.java create mode 100644 src/graphs/Graph.java create mode 100644 src/list/Deletion_in_Linked_List.java create mode 100644 src/list/Insertion_in_Linked_List.java create mode 100644 src/list/MainList.java create mode 100644 src/list/MyLL.java create mode 100644 src/practice_Set_Problems_Problem1/Cylinder.java create mode 100644 src/practice_Set_Problems_Problem1/Setter_Getters.java create mode 160000 src/src create mode 100644 trees/FindFullNodesInABinaryTree.class create mode 100644 trees/Node.class diff --git a/basicProblems/Factorial.class b/basicProblems/Factorial.class new file mode 100644 index 0000000000000000000000000000000000000000..89634d6bd6e83a204a8e7d37834186fbd03e9f1a GIT binary patch literal 1124 zcmaJ=O-~b16g_V`?UZr&wtQH@f`A2)svxMXENnt-0!mV%F|J;R2MnFgG&8S6*7_sd zx^gWGNHFmS_;Xyk5bv8-DQV*@?tLHko_o%{Z+`qf{|;ac>n2R342<+(6k{edj4N|P zZPP;(a?3;pQwF9D+-67=9M6&K46*F|YX-gO?+S+gO~(^28?}lEc6i056TQYAk6|SH zZu5{I@tn)O{ajfFj<=sziM?gX?dle7Vct^Vc@y%%TNZqz9r;=zZ>=^a4(4^3pw03 z@W8@DEHjK&c<9*MflsV!VeVg50=VS;s&Xi7$uQhS%$30MaVSNNK~vgOP34ZCD|z*X zB=LgR@(g3yt{18+h9}<3l1HVey(O}KgVvWo+tk0mKqgl3h#Iv=jmr3Y8J2Ov3?oG~ zUKK^(v$=fZ$OBr1l6x{#!zh*JJI6t~BB?~Q4WG!=RW(;BX)(S!L<6g;nNJyJE?ebz zM}AebCf#Jw?CG(A0zIEi->)|63|2`^ujukH6h!S)dxaK8b8auVx9f^qXY{30 z`a$8Ne(~kv=Lr%M>2nysDUu3Vi;$<0jPNz`y@WqRWYR<@Lw=5`EmOTgibz4bAQLw* k#0+Tky6}sxVi=B~uHy-Llc=pBkH;9tP1+}EuM|!G0c+YBD*ylh literal 0 HcmV?d00001 diff --git a/src/algorithm_fundamentals/Stack.java b/src/algorithm_fundamentals/Stack.java new file mode 100644 index 0000000..e156557 --- /dev/null +++ b/src/algorithm_fundamentals/Stack.java @@ -0,0 +1,10 @@ +package algorithm_fundamentals; + +public class Stack { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/src/exercise_Problems/Exercise1.java b/src/exercise_Problems/Exercise1.java new file mode 100644 index 0000000..afe8283 --- /dev/null +++ b/src/exercise_Problems/Exercise1.java @@ -0,0 +1,33 @@ +package exercise_Problems; +import java.util.*; + +/* To calculate percentage of a given student in CBSE Board exam. + * His marks from 5 subjects must be taken as input from the keyboard + * (Marks are out of 100) + * */ + +public class Exercise1 { + + static Scanner sc = new Scanner(System.in); + + static int subject_1_marks = sc.nextInt(); + static int subject_2_marks = sc.nextInt(); + static int subject_3_marks = sc.nextInt(); + static int subject_4_marks = sc.nextInt(); + static int subject_5_marks = sc.nextInt(); + + public static void main(String[] args) { + + int total = (subject_1_marks + subject_2_marks + subject_3_marks + subject_4_marks + subject_5_marks); + float percentage = (total)/5; + + System.out.println("The marks of the subject's are: \n " + "\n" + "subject_1_marks:- " + subject_1_marks + "\n" + + "subject_2_marks:- " + subject_2_marks + "\n" + "subject_3_marks:- " + subject_3_marks + "\n" + + "subject_4_marks:- " + subject_4_marks + "\n" + "subject_5_marks:- " + subject_5_marks + "\n"); + + System.out.println("The total no. of marks are: " + total); + System.out.println("Total percentage is: " + percentage); + + } + +} diff --git a/src/exercise_Problems/Exercise2.java b/src/exercise_Problems/Exercise2.java new file mode 100644 index 0000000..e66bec3 --- /dev/null +++ b/src/exercise_Problems/Exercise2.java @@ -0,0 +1,24 @@ +package exercise_Problems; + +/* + * swap two given numbers without using third variable + */ + +public class Exercise2 { + + public static void main(String[] args) { + + int a = 20; + int b = 40; + + System.out.println("Number's before swapping: " + "a = " + a + "," + " b =" + " " + b); + + a = a + b; + b = a - b; + a = a - b; + + System.out.println("Number's after swapping: " + "a = " + a + "," + " b =" + " " + b); + + } + +} diff --git a/src/exercise_Problems/Exercise3.java b/src/exercise_Problems/Exercise3.java new file mode 100644 index 0000000..79b5808 --- /dev/null +++ b/src/exercise_Problems/Exercise3.java @@ -0,0 +1,34 @@ +package exercise_Problems; +import java.util.Scanner; + +/* + * Take an array as input & print all of its content at particular index wise + */ + +public class Exercise3 { + + public static void main(String[] args) { + try (Scanner sc = new Scanner(System.in)) { + int n,sum =0; + + System.out.println("Enter no. of elements you want in array:"); + n = sc.nextInt(); + + int A[] = new int[n]; + System.out.println("Enter all the elements: "); + + for(int i=0;i adj[]; + + @SuppressWarnings("unchecked") + public Graph(int v) { + //array of Linked List + adj = new LinkedList[v]; + + for(int i=0;i(); + } + } + + public void addEdge(int source, int destination) { + adj[source].add(destination); + adj[destination].add(source); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Enter number of vertices and edges"); + int v = sc.nextInt(); + int e = sc.nextInt(); + + Graph graph = new Graph(v); + System.out.println("Enter " + e + " edges"); + + for(int i=0;inext is the node to be deleted + // Store pointer to the next of node to be deleted + Node next = temp.next.next; + + temp.next = next; // Unlink the deleted node from list + } + + /* This function prints contents of linked list starting from + the given node */ + public void printList() + { + Node tnode = head; + while (tnode != null) + { + System.out.print(tnode.data+" "); + tnode = tnode.next; + } + } + + /* Driver program to test above functions. Ideally this function + should be in a separate user class. It is kept here to keep + code compact */ + public static void main(String[] args) + { + /* Start with the empty list */ + LinkedList llist = new LinkedList(); + + llist.push(7); + llist.push(1); + llist.push(3); + llist.push(2); + llist.push(8); + + System.out.println("\nCreated Linked list is: "); + llist.printList(); + + llist.deleteNode(4); // Delete node at position 4 + + System.out.println("\nLinked List after Deletion at position 4: "); + llist.printList(); + } +} \ No newline at end of file diff --git a/src/list/Insertion_in_Linked_List.java b/src/list/Insertion_in_Linked_List.java new file mode 100644 index 0000000..f395e1a --- /dev/null +++ b/src/list/Insertion_in_Linked_List.java @@ -0,0 +1,99 @@ +package list; + +public class Insertion_in_Linked_List { + + class Node{ + int data; + Node next; + + Node(int d){ + data = d; + next = null; + } + } + + Node head; + + public void push(int new_data ) { + Node new_node = new Node(new_data); + new_node.next = head; + head = new_node; + } + + public void insertAfter(Node prev_node, int new_data) + { + /* 1. Check if the given Node is null */ + if (prev_node == null) + { + System.out.println("The given previous node cannot be null"); + return; + } + + /* 2 & 3: Allocate the Node & Put in the data*/ + Node new_node = new Node(new_data); + + /* 4. Make next of new Node as next of prev_node */ + new_node.next = prev_node.next; + + /* 5. make next of prev_node as new_node */ + prev_node.next = new_node; + } + + public void append(int new_data) { + + //make an object of above mentioned Node class + Node new_node = new Node(new_data); + + if(head == null) { + head = new_node; + return; + } + + new_node.next = null; + + Node last = head; + while (last.next != null) + last = last.next; + + last.next = new_node; + return; + } + + public void printList() + { + Node tnode = head; + while (tnode != null) + { + System.out.print(tnode.data+" "); + tnode = tnode.next; + } + } + + public static void main(String[] args) { + Insertion_in_Linked_List llist = new Insertion_in_Linked_List(); + /* Start with the empty list */ + + // Insert 6. So linked list becomes 6->NUllist + llist.append(6); + + // Insert 7 at the beginning. So linked list becomes + // 7->6->NUllist + llist.push(7); + + // Insert 1 at the beginning. So linked list becomes + // 1->7->6->NUllist + llist.push(1); + + // Insert 4 at the end. So linked list becomes + // 1->7->6->4->NUllist + llist.append(4); + + // Insert 8, after 7. So linked list becomes + // 1->7->8->6->4->NUllist + llist.insertAfter(llist.head.next, 8); + + System.out.println("\nCreated Linked list is: "); + llist.printList(); + } + +} diff --git a/src/list/MainList.java b/src/list/MainList.java new file mode 100644 index 0000000..62e39df --- /dev/null +++ b/src/list/MainList.java @@ -0,0 +1,18 @@ +package list; +public class MainList { + + public static void main(String[] args) { + + MyLL myll = new MyLL(); + + + myll.add(0); + myll.add(1); + myll.add(2); + myll.add(3); + myll.add(4); + + myll.print(); + } + +} diff --git a/src/list/MyLL.java b/src/list/MyLL.java new file mode 100644 index 0000000..478b1da --- /dev/null +++ b/src/list/MyLL.java @@ -0,0 +1,53 @@ +package list; + +public class MyLL { + + static class Node{ + int data; + Node next; + + public Node(int data) { + this.data = data; + next = null; + } + } + + Node head; + Node position; + + void add(int data) { + Node toAdd = new Node(data); + + if(isEmpty()) { + head = toAdd; + return; + } + + Node temp = head; + while(temp.next != null) { + temp = temp.next; + } + temp.next = toAdd; + } + + void print() { + Node temp = head; + while(temp != null) { + System.out.println(temp.data + " "); + temp = temp.next; + } + + } + + boolean isEmpty() { + return head == null; + } + + void remove(int data) { + head = null; + } + + boolean isValue() { + return head == position; + } +} diff --git a/src/practice_Set_Problems_Problem1/Cylinder.java b/src/practice_Set_Problems_Problem1/Cylinder.java new file mode 100644 index 0000000..72324b7 --- /dev/null +++ b/src/practice_Set_Problems_Problem1/Cylinder.java @@ -0,0 +1,26 @@ +package practice_Set_Problems_Problem1; + +public class Cylinder { + + public static void main(String[] args) { + + //The below line represent's the , Object of the Setter_Getters class + Setter_Getters sg = new Setter_Getters(); + + //setting values of pie, height & radius by using setter method with use of other class & by calling it + sg.setHeight(35); + sg.setRadius(3.0); + sg.setPie(3.14); + + //getting values of pie, radius & height + System.out.println("The value of the Pie is: " + sg.getPie()); + System.out.println("The height of the cylinder is: " + sg.getHeight() + " cm"); + System.out.println("The radius of the cylinder is: " + sg.getRadius() + " cm"); + + // volume of cylinder => (V = 3.14*(r)^2*h) + double V = sg.getPie()*(sg.getRadius()*sg.getRadius())*sg.getHeight(); + System.out.println("The volume of the cylinder is: " + V + " cm^3"); + + } + +} diff --git a/src/practice_Set_Problems_Problem1/Setter_Getters.java b/src/practice_Set_Problems_Problem1/Setter_Getters.java new file mode 100644 index 0000000..0716daf --- /dev/null +++ b/src/practice_Set_Problems_Problem1/Setter_Getters.java @@ -0,0 +1,44 @@ +package practice_Set_Problems_Problem1; + +//a class to implement getter & setter methods for achieving encapsulation +public class Setter_Getters { + + /*we have to put the variables private to achieve encapsulation*/ + private int height; + private double radius; + private double pie; + + //setter method to set height of cylinder + public void setHeight(int height) { + if(height > 40) System.out.println(" The height of cylender is to high to to find vol & surface area: "); + else this.height = height; + } + + //setter method to set radius of cylinder + public void setRadius(double radius) { + this.radius = radius; + } + + //setter method to set the value of pie + public void setPie(double pie) { + this.pie = pie; + } + + //getter method to get the height of cylinder + public int getHeight() { + return height; + } + + //getter method to get the radius of cylinder + public double getRadius() { + return radius; + } + + //getter method to get the value of pie + public double getPie() { + return pie; + } + + + +} diff --git a/src/src b/src/src new file mode 160000 index 0000000..26d2d57 --- /dev/null +++ b/src/src @@ -0,0 +1 @@ +Subproject commit 26d2d577f3d9fe69db155171c374a6391232703f diff --git a/trees/FindFullNodesInABinaryTree.class b/trees/FindFullNodesInABinaryTree.class new file mode 100644 index 0000000000000000000000000000000000000000..8057a21b920cdaafea6cdc518f9760100d5f9e64 GIT binary patch literal 1436 zcmaJ>-A)rh6#k}^E?t%%`3LzIv=pWIi&_+=36Z3LNeRYyVTN{qh3#&$J0;-(dHp5b52PxwB=1(tX9jL!d>T^ z79qx#X_uC&mbDr>Rzz|2CQp(a30QOdo@NAcIZ>{S!XeO4pChFxkp=3o##^O`l;VeT3K-G^pVW z{WQeS(D9LIj*vKk27E@7Ldi$5#(dTR;sx==(MjcWpyp@(ioA0Z$-mP%00F2ens z?1{o?<=0mvU&oK3=ckp`NY*FBd^_RW@oB0=EotFWIBmLEjjPr6z&l3X!8~wR(eZOA z5)zWMCu=O{kY3Ak4mH<9#zkQ4oW3RM6%^4LdWB*IEm-s=8zeb~NRDCZ;0R_hiUqRP zFpjtMM!J~5J51sb*YO@Va7uE1r?=+^y*fYX#c3dMmH54QN~2Dq&f^)LknkfkZ=(4q HePe$BDE?Co literal 0 HcmV?d00001 diff --git a/trees/Node.class b/trees/Node.class new file mode 100644 index 0000000000000000000000000000000000000000..cfb87813e431f5268359b464553f61cd2420ef26 GIT binary patch literal 339 zcmYLEO-sW-6r63-#KhHVTfgw2$2N!NsGtZc1OlZVEIoMJHf!AyQ%DoRpXDJ49{d6R zC~>wIS>BtsZ|BX<`}zI)25^Cs04_Q{TIkx;v+2OYp@$;@Csm~q5DCjnKbHb>R2EvV zqwy-$R|2iVEMC}luT5^s8`Ap8U`n{>yFhznay_nIXS$fES;kKAxT=b|j*Y#X#U@YV zD$A_eI>~SEOs Date: Wed, 28 Oct 2020 18:49:50 +0530 Subject: [PATCH 09/10] Modification 1 --- arrays/ArrayLevel1.java | 17 ++ arrays/ArrayLevel2.java | 34 ++++ arrays/Array_Problem_1.java | 41 +++++ arrays/Array_Problem_11.java | 33 ++++ arrays/Array_Problem_18.java | 29 ++++ arrays/Array_Problem_2.java | 55 ++++++ arrays/Array_Problem_23.java | 45 +++++ arrays/Array_Problem_3.java | 122 ++++++++++++++ arrays/Array_Problem_32.java | 35 ++++ arrays/Array_Problem_4_Approach1.java | 63 +++++++ arrays/Array_Problem_4_Approach2.java | 47 ++++++ arrays/Array_Problem_5.java | 41 +++++ arrays/Array_Problem_6.java | 118 +++++++++++++ arrays/Array_Problem_7.java | 42 +++++ arrays/Array_Problem_8.java | 32 ++++ arrays/Array_Problem_9.java | 10 ++ arrays/Array_of_objects.java | 44 +++++ arrays/MultiDArray.java | 16 ++ basicProblems/Armstrong_Number.java | 25 +++ basicProblems/Factorial.java | 9 +- basicProblems/Fibonacci_Series.java | 8 +- .../Multiplicative_Table_till_20.java | 15 +- basicProblems/Palindrome_Number.java | 4 - basicProblems/Prime_Number_Or_Not.java | 15 +- ...ive_decimal_number_from_right_to_left.java | 18 ++ basicProblems/Reverse_Given_number.java | 21 +++ basicProblems/Series_Sum_1.java | 6 +- basicProblems/Swap_two_numbers.java | 15 +- basicProblems/Swapping_2.java | 24 +++ basic_idea_of_DS/ArrayDequeDemo.java | 24 +++ basic_idea_of_DS/ArrayListDemo.java | 33 ++++ basic_idea_of_DS/Graph.java | 41 +++++ basic_idea_of_DS/HashMapIntro.java | 28 +++ basic_idea_of_DS/LinkedListDemo.java | 35 ++++ basic_idea_of_DS/List.java | 14 ++ basic_idea_of_DS/MYStackByList.java | 121 +++++++++++++ basic_idea_of_DS/MyArrayList.java | 33 ++++ basic_idea_of_DS/MyHashMap.java | 28 +++ basic_idea_of_DS/MyHashSet.java | 21 +++ basic_idea_of_DS/MyLinkedList.java | 35 ++++ basic_idea_of_DS/MyQueue.java | 82 +++++++++ basic_idea_of_DS/MyStack.java | 58 +++++++ basic_idea_of_DS/SetExample.java | 34 ++++ basic_idea_of_DS/Tree.java | 52 ++++++ basics/ControlStatements.java | 38 ++--- basics/WhileLoop.java | 8 +- dataStructures/Info.java | 10 ++ dataStructures/graphs/Graph.java | 41 +++++ exercise_and_practice_Problems/Cylinder.java | 26 +++ exercise_and_practice_Problems/Exercise1.java | 33 ++++ exercise_and_practice_Problems/Exercise2.java | 34 ++++ .../Getters_Setters_For_Cylinder.java | 44 +++++ graphs/Graph.java | 41 +++++ list/Deletion_in_Linked_List.java | 100 +++++++++++ list/Insertion_in_Linked_List.java | 99 +++++++++++ list/MainList.java | 18 ++ list/MyLL.java | 53 ++++++ stack/Parenthesis_Checker_Problem.java | 74 ++++++++ stack/Tower_Of_Hanoi.java | 29 ++++ trees/BST_Deletion.java | 159 ++++++++++++++++++ trees/BinarySearchTree.java | 83 +++++++++ trees/Insertion_In_BinaryTree.java | 2 +- 62 files changed, 2458 insertions(+), 57 deletions(-) create mode 100644 arrays/ArrayLevel1.java create mode 100644 arrays/ArrayLevel2.java create mode 100644 arrays/Array_Problem_1.java create mode 100644 arrays/Array_Problem_11.java create mode 100644 arrays/Array_Problem_18.java create mode 100644 arrays/Array_Problem_2.java create mode 100644 arrays/Array_Problem_23.java create mode 100644 arrays/Array_Problem_3.java create mode 100644 arrays/Array_Problem_32.java create mode 100644 arrays/Array_Problem_4_Approach1.java create mode 100644 arrays/Array_Problem_4_Approach2.java create mode 100644 arrays/Array_Problem_5.java create mode 100644 arrays/Array_Problem_6.java create mode 100644 arrays/Array_Problem_7.java create mode 100644 arrays/Array_Problem_8.java create mode 100644 arrays/Array_Problem_9.java create mode 100644 arrays/Array_of_objects.java create mode 100644 arrays/MultiDArray.java create mode 100644 basicProblems/Armstrong_Number.java create mode 100644 basicProblems/Print_all_digits_of_positive_decimal_number_from_right_to_left.java create mode 100644 basicProblems/Reverse_Given_number.java create mode 100644 basicProblems/Swapping_2.java create mode 100644 basic_idea_of_DS/ArrayDequeDemo.java create mode 100644 basic_idea_of_DS/ArrayListDemo.java create mode 100644 basic_idea_of_DS/Graph.java create mode 100644 basic_idea_of_DS/HashMapIntro.java create mode 100644 basic_idea_of_DS/LinkedListDemo.java create mode 100644 basic_idea_of_DS/List.java create mode 100644 basic_idea_of_DS/MYStackByList.java create mode 100644 basic_idea_of_DS/MyArrayList.java create mode 100644 basic_idea_of_DS/MyHashMap.java create mode 100644 basic_idea_of_DS/MyHashSet.java create mode 100644 basic_idea_of_DS/MyLinkedList.java create mode 100644 basic_idea_of_DS/MyQueue.java create mode 100644 basic_idea_of_DS/MyStack.java create mode 100644 basic_idea_of_DS/SetExample.java create mode 100644 basic_idea_of_DS/Tree.java create mode 100644 dataStructures/Info.java create mode 100644 dataStructures/graphs/Graph.java create mode 100644 exercise_and_practice_Problems/Cylinder.java create mode 100644 exercise_and_practice_Problems/Exercise1.java create mode 100644 exercise_and_practice_Problems/Exercise2.java create mode 100644 exercise_and_practice_Problems/Getters_Setters_For_Cylinder.java create mode 100644 graphs/Graph.java create mode 100644 list/Deletion_in_Linked_List.java create mode 100644 list/Insertion_in_Linked_List.java create mode 100644 list/MainList.java create mode 100644 list/MyLL.java create mode 100644 stack/Parenthesis_Checker_Problem.java create mode 100644 stack/Tower_Of_Hanoi.java create mode 100644 trees/BST_Deletion.java create mode 100644 trees/BinarySearchTree.java diff --git a/arrays/ArrayLevel1.java b/arrays/ArrayLevel1.java new file mode 100644 index 0000000..c065dd2 --- /dev/null +++ b/arrays/ArrayLevel1.java @@ -0,0 +1,17 @@ +package arrays; + +public class ArrayLevel1 { + + public static void main(String[] args) { + int[] arr; + arr = new int[5]; + + arr[0] = 10; + arr[1] = 20; + arr[2] = 30; + arr[3] = 40; + arr[4] = 50; + + for(int i = 0 ; i =0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; + else System.out.print(Math.abs(m[i]) + " "); + } + } + + //Driver Method + public static void main(String[] args) { + + Array_Problem_11 dublicate= new Array_Problem_11(); + + int m[] = {1,2,3,1,3,6,6}; + int m_size = m.length; + + dublicate.printRepeating(m,m_size); + + } + +} + diff --git a/arrays/Array_Problem_18.java b/arrays/Array_Problem_18.java new file mode 100644 index 0000000..709bf09 --- /dev/null +++ b/arrays/Array_Problem_18.java @@ -0,0 +1,29 @@ +package arrays; + +/* + * find all pairs on integer array whose sum is equal to given number + * OR + * Equal_Sum_Pairs_of_Array + */ + +public class Array_Problem_18 { + + //function to find & print the sum & elements + static void pairs_value(int iA[], int iN) { + System.out.println("Pairs of elements & their sum : "); + + //loop to iterate & find the pair of elements whose sum is equal + for(int i = 0 ; i < iA.length ; i++) { + for(int j = i + 1 ; j < iA.length ; j++) + //check if the sum of pair is equal, if equal then print the pair of elements & their sum + if(iA[i]+iA[j] == iN) System.out.println(iA[i] + " + " + iA[j] + " = " + iN); + } + } + + //Driver Method + public static void main(String[] args) { + pairs_value(new int[] {2,7,4,-5,11,5,20}, 15); + pairs_value(new int[] {14,-15,9,16,25,45,12,8},30); + } + +} diff --git a/arrays/Array_Problem_2.java b/arrays/Array_Problem_2.java new file mode 100644 index 0000000..63dc567 --- /dev/null +++ b/arrays/Array_Problem_2.java @@ -0,0 +1,55 @@ +package arrays; + +//Java program to find the maximum & minimum element in given or array as user input +public class Array_Problem_2 { +/* Class Pair is used to return two values from getMinMax() */ + static class Pair { + + int min; + int max; + } + + static Pair getMinMax(int arr[], int n) { + Pair minmax = new Pair(); + int i; + + /*If there is only one element then return it as min and max both*/ + if (n == 1) { + minmax.max = arr[0]; + minmax.min = arr[0]; + return minmax; + } + + /* If there are more than one elements, then initialize min + and max*/ + if (arr[0] > arr[1]) { + minmax.max = arr[0]; + minmax.min = arr[1]; + } else { + minmax.max = arr[1]; + minmax.min = arr[0]; + } + + for (i = 2; i < n; i++) { + if (arr[i] > minmax.max) { + minmax.max = arr[i]; + } else if (arr[i] < minmax.min) { + minmax.min = arr[i]; + } + } + + return minmax; + } + + /* Driver program to test above function */ + public static void main(String args[]) { + int arr[] = {1000, 11, 445, 1, 330, 3000}; + int arr_size = 6; + Pair minmax = getMinMax(arr, arr_size); + System.out.printf("\nMinimum element is %d", minmax.min); + System.out.printf("\nMaximum element is %d", minmax.max); + + } + +} + diff --git a/arrays/Array_Problem_23.java b/arrays/Array_Problem_23.java new file mode 100644 index 0000000..9aedc89 --- /dev/null +++ b/arrays/Array_Problem_23.java @@ -0,0 +1,45 @@ +package arrays; + +/* + * Java program for maximum product sub-array problem + */ + +public class Array_Problem_23 { + + static void maxSubArraySum(int a[], int size) + { + int max_so_far = Integer.MIN_VALUE, + max_ending_here = 0,start = 0, + end = 0, s = 0; + + for (int i = 0; i < size; i++) + { + max_ending_here += a[i]; + + if (max_so_far < max_ending_here) + { + max_so_far = max_ending_here; + start = s; + end = i; + } + + if (max_ending_here < 0) + { + max_ending_here = 0; + s = i + 1; + } + } + System.out.println("Maximum contiguous sum is " + max_so_far); + System.out.println("Starting index " + start); + System.out.println("Ending index " + end); + } + + // Driver code + public static void main(String[] args) + { + int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 }; + int n = a.length; + maxSubArraySum(a, n); + } + } + diff --git a/arrays/Array_Problem_3.java b/arrays/Array_Problem_3.java new file mode 100644 index 0000000..0a9c3ad --- /dev/null +++ b/arrays/Array_Problem_3.java @@ -0,0 +1,122 @@ +package arrays; + +import java.util.Arrays; + +/* + * (Find K^the smallest element) + * Given an array array[] and a number K where K is smaller than size of array, + * the task is to find the K^the smallest element in the given array. + * It is given that all array elements are distinct. + */ +public class Array_Problem_3 { + + // int partition(int array[], int l, int r, int k); + + // A simple function to find median of arr[]. This is called + // only for an array of size 5 in this program. + static int findMedian(int arr[], int i,int n) + { + if(i <= n) + Arrays.sort(arr, i, n); // Sort the array + else + Arrays.sort(arr, n, i); + return arr[n/2]; // Return middle element + } + + // Returns k'th smallest element + // in arr[l..r] in worst case + // linear time. ASSUMPTION: ALL + // ELEMENTS IN ARR[] ARE DISTINCT + static int kthSmallest(int arr[], int l, int r, int k) + { + // If k is smaller than + // number of elements in array + if (k > 0 && k <= r - l + 1) + { + int n = r - l + 1 ; // Number of elements in arr[l..r] + + // Divide arr[] in groups of size 5, + // calculate median of every group + // and store it in median[] array. + int i; + + // There will be floor((n+4)/5) groups; + int []median = new int[(n + 4) / 5]; + for (i = 0; i < n/5; i++) + median[i] = findMedian(arr,l + i * 5, 5); + + // For last group with less than 5 elements + if (i*5 < n) + { + median[i] = findMedian(arr,l + i * 5, n % 5); + i++; + } + + // Find median of all medians using recursive call. + // If median[] has only one element, then no need + // of recursive call + int medOfMed = (i == 1)? median[i - 1]: + kthSmallest(median, 0, i - 1, i / 2); + + // Partition the array around a random element and + // get position of pivot element in sorted array + int pos = partition(arr, l, r, medOfMed); + + // If position is same as k + if (pos-l == k - 1) + return arr[pos]; + if (pos-l > k - 1) // If position is more, recur for left + return kthSmallest(arr, l, pos - 1, k); + + // Else recur for right subarray + return kthSmallest(arr, pos + 1, r, k - pos + l - 1); + } + + // If k is more than number of elements in array + return Integer.MAX_VALUE; + } + + static int[] swap(int []arr, int i, int j) + { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + return arr; + } + + // It searches for x in arr[l..r], and + // partitions the array around x. + static int partition(int arr[], int l, + int r, int x) + { + // Search for x in arr[l..r] and move it to end + int i; + for (i = l; i < r; i++) + if (arr[i] == x) + break; + swap(arr, i, r); + + // Standard partition algorithm + i = l; + for (int j = l; j <= r - 1; j++) + { + if (arr[j] <= x) + { + swap(arr, i, j); + i++; + } + } + swap(arr, i, r); + return i; + } + + // Driver code + public static void main(String[] args) + { + int arr[] = {12, 3, 5, 7, 4, 19, 26}; + int n = arr.length, k = 3; + System.out.println("K'th smallest element is " + + kthSmallest(arr, 0, n - 1, k)); + } + +} diff --git a/arrays/Array_Problem_32.java b/arrays/Array_Problem_32.java new file mode 100644 index 0000000..7f50762 --- /dev/null +++ b/arrays/Array_Problem_32.java @@ -0,0 +1,35 @@ +package arrays; + +//Java program to find minimum number of operations to make an array palindrome +public class Array_Problem_32 { + + static int findMinOps(int[] a, int n) { + + //Initialize result + int ans = 0; + + //Start from two corners + for(int i=0,j=n-1;i<=j;) { + + if(a[i] == a[j]) { + i++; + j--; + }else if(a[i] > a[j]) { + j--; + a[j] += a[j+1]; + ans++; + }else { + i++; + a[i] += a[i-1]; + ans++; + } + } + return ans; + } + + public static void main(String[] args) { + int a[] = new int[] {1,2,3,4,5,6,7,8,9,10}; + System.out.println("Count of minimum operations is " + findMinOps(a , a.length)); + } + +} diff --git a/arrays/Array_Problem_4_Approach1.java b/arrays/Array_Problem_4_Approach1.java new file mode 100644 index 0000000..b1e4e48 --- /dev/null +++ b/arrays/Array_Problem_4_Approach1.java @@ -0,0 +1,63 @@ +package arrays; +import java.io.*; + +/* + * Java program to solve that, + * Given an array A of size N containing 0s, 1s, and 2s; + * you need to sort the array in ascending order without using any sorting algorithm + */ + +@SuppressWarnings("unused") +public class Array_Problem_4_Approach1 { + + // Sort the input array, the array is assumed to + // have values in {0, 1, 2} + static void sort012(int a[], int arr_size) + { + int lo = 0; + int hi = arr_size - 1; + int mid = 0, temp = 0; + while (mid <= hi) { + switch (a[mid]) { + case 0: { + temp = a[lo]; + a[lo] = a[mid]; + a[mid] = temp; + lo++; + mid++; + break; + } + case 1: + mid++; + break; + case 2: { + temp = a[mid]; + a[mid] = a[hi]; + a[hi] = temp; + hi--; + break; + } + } + } + } + + /* Utility function to print array arr[] */ + static void printArray(int arr[], int arr_size) + { + int i; + for (i = 0; i < arr_size; i++) + System.out.print(arr[i] + " "); + System.out.println(""); + } + + /*Driver function to check for above functions*/ + public static void main(String[] args) + { + int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; + int arr_size = arr.length; + sort012(arr, arr_size); + System.out.println("Array after seggregation "); + printArray(arr, arr_size); + } + +} diff --git a/arrays/Array_Problem_4_Approach2.java b/arrays/Array_Problem_4_Approach2.java new file mode 100644 index 0000000..1116d1d --- /dev/null +++ b/arrays/Array_Problem_4_Approach2.java @@ -0,0 +1,47 @@ +package arrays; +import java.util.Arrays; + +/* + * Sort_an_array_of_0s_1s_2s_without_using_extra_space_or_sorting_algo + */ + +public class Array_Problem_4_Approach2 { + + static int arr1[] = new int[]{1, 5, 9, 10, 15, 20}; + static int arr2[] = new int[]{2, 3, 8, 13}; + + static void merge(int m, int n) + { + // Iterate through all elements of ar2[] starting from to the last element + for (int i=n-1; i>=0; i--) + { + + /* Find the smallest element greater than ar2[i]. + * Move all elements one position ahead, + * till the smallest greater element is not found + */ + int j, last = arr1[m-1]; + + for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--) + arr1[j+1] = arr1[j]; + + // If there was a greater element + if (j != m-2 || last > arr2[i]) { + arr1[j+1] = arr2[i]; + arr2[i] = last; + } + } + } + + // Driver method to test the above function + public static void main1(String[] args) + { + merge(arr1.length,arr2.length); + + System.out.print("After Merging nFirst Array: "); + System.out.println(Arrays.toString(arr1)); + + System.out.print("Second Array: "); + System.out.println(Arrays.toString(arr2)); + } +} diff --git a/arrays/Array_Problem_5.java b/arrays/Array_Problem_5.java new file mode 100644 index 0000000..d2a4008 --- /dev/null +++ b/arrays/Array_Problem_5.java @@ -0,0 +1,41 @@ +package arrays; + +/* + * Move all the negative elements to one side of the array + */ + +public class Array_Problem_5 { + + static void rearrange(int arr[], int n) + { + int j = 0, temp; + for (int i = 0; i < n; i++) { + if (arr[i] < 0) { + if (i != j) { + temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + j++; + } + } + } + + // A utility function to print an array + static void printArray(int arr[], int n) + { + for (int i = 0; i < n; i++) + System.out.print(arr[i] + " "); + } + + //Driver Method + public static void main(String[] args) { + int arr[] = { -1, 2, -3, 4, 5, 6, -7, 8, 9 , -47}; + int n = arr.length; + + rearrange(arr, n); + printArray(arr, n); + + } + +} diff --git a/arrays/Array_Problem_6.java b/arrays/Array_Problem_6.java new file mode 100644 index 0000000..5552ee1 --- /dev/null +++ b/arrays/Array_Problem_6.java @@ -0,0 +1,118 @@ +package arrays; + +// A Java program to print union and intersection of two unsorted arrays +import java.util.Arrays; + +/* + * Find the Union and Intersection of the two sorted arrays. + */ + +public class Array_Problem_6 { + + + void printUnion(int arr1[], int arr2[], int m, int n) + { + // Before finding union, make sure arr1[0..m-1] + // is smaller + if (m > n) { + int tempp[] = arr1; + arr1 = arr2; + arr2 = tempp; + + int temp = m; + m = n; + n = temp; + } + + // Now arr1[] is smaller + // Sort the first array and print its elements + // (these two steps can be swapped as order in + // output is not important) + Arrays.sort(arr1); + for (int i = 0; i < m; i++) + System.out.print(arr1[i] + " "); + + // Search every element of bigger array in smaller + // array and print the element if not found + for (int i = 0; i < n; i++) { + if (binarySearch(arr1, 0, m - 1, arr2[i]) == -1) + System.out.print(arr2[i] + " "); + } + } + + // Prints intersection of arr1[0..m-1] and arr2[0..n-1] + void printIntersection(int arr1[], int arr2[], int m, + int n) + { + // Before finding intersection, make sure + // arr1[0..m-1] is smaller + if (m > n) { + int tempp[] = arr1; + arr1 = arr2; + arr2 = tempp; + + int temp = m; + m = n; + n = temp; + } + + // Now arr1[] is smaller + // Sort smaller array arr1[0..m-1] + Arrays.sort(arr1); + + // Search every element of bigger array in smaller + // array and print the element if found + for (int i = 0; i < n; i++) { + if (binarySearch(arr1, 0, m - 1, arr2[i]) != -1) + System.out.print(arr2[i] + " "); + } + } + + // A recursive binary search function. It returns + // location of x in given array arr[l..r] is present, + // otherwise -1 + int binarySearch(int arr[], int l, int r, int x) + { + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the middle + // itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then it can + // only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present in right + // subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not present in + // array + return -1; + } + + // Driver code + public static void main(String[] args) + { + Array_Problem_6 u_i= new Array_Problem_6(); + + int arr1[] = { 7, 1, 5, 2, 3, 6 }; + int arr2[] = { 3, 8, 6, 20, 7 }; + int m = arr1.length; + int n = arr2.length; + + // Function call + System.out.println("Union of two arrays is "); + u_i.printUnion(arr1, arr2, m, n); + System.out.println(""); + System.out.println( + "Intersection of two arrays is "); + u_i.printIntersection(arr1, arr2, m, n); + } + } + diff --git a/arrays/Array_Problem_7.java b/arrays/Array_Problem_7.java new file mode 100644 index 0000000..948018d --- /dev/null +++ b/arrays/Array_Problem_7.java @@ -0,0 +1,42 @@ +package arrays; + +/* + * Write a program to cyclically rotate an array by one. + */ + +public class Array_Problem_7 { + + //function to rotate the elements of an array + + void rotateL(int a[], int d, int n) { + for(int i= 0;i0) { + int last_digit = n % 10; + sum += Math.pow(last_digit,3); + n=n/10; + } + + if(sum == original_no) System.out.println("Armstrong number"); + else System.out.println("Not Armstrong"); + + } + +} diff --git a/basicProblems/Factorial.java b/basicProblems/Factorial.java index f4a4355..c73f54c 100644 --- a/basicProblems/Factorial.java +++ b/basicProblems/Factorial.java @@ -1,17 +1,18 @@ package basicProblems; import java.util.*; + public class Factorial { public static void main(String[] args) { + int fact=1; - @SuppressWarnings("resource") - Scanner sc = new Scanner(System.in); + @SuppressWarnings("resource") + Scanner sc = new Scanner(System.in); int n = sc.nextInt(); - for(int i = n ; i>=1;i--) { + for(int i = n ; i >= 1 ; i--) { fact = fact*i; } - System.out.println("fact of " + n + " is " + fact ); } } diff --git a/basicProblems/Fibonacci_Series.java b/basicProblems/Fibonacci_Series.java index 2772bc1..f1fb26d 100644 --- a/basicProblems/Fibonacci_Series.java +++ b/basicProblems/Fibonacci_Series.java @@ -1,5 +1,6 @@ package basicProblems; import java.util.*; + public class Fibonacci_Series { public static void main(String[] args) { @@ -9,12 +10,13 @@ public static void main(String[] args) { int n = sc.nextInt(); int a=0,b=1; - System.out.println(a + " "); - System.out.println(b + " "); + System.out.println("a = " + a + " "); + System.out.println("b = " + b + " "); + System.out.println("Fibonacci Series:"); for(int i=0;i 0) { + int rem = n % 10; + System.out.print(rem); + n = n/10; + } + + } + +} diff --git a/basicProblems/Reverse_Given_number.java b/basicProblems/Reverse_Given_number.java new file mode 100644 index 0000000..1ba8a20 --- /dev/null +++ b/basicProblems/Reverse_Given_number.java @@ -0,0 +1,21 @@ +package basicProblems; + +import java.util.*; +public class Reverse_Given_number { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + int reverse=0; + + while(n>0) { + int last_digit = n % 10; + reverse = reverse*10 + last_digit; + n = n/10; + } + + System.out.println(reverse); + } + +} diff --git a/basicProblems/Series_Sum_1.java b/basicProblems/Series_Sum_1.java index ed57857..8d0b704 100644 --- a/basicProblems/Series_Sum_1.java +++ b/basicProblems/Series_Sum_1.java @@ -1,18 +1,20 @@ package basicProblems; import java.util.*; + public class Series_Sum_1 { public static void main(String[] args) { + //taking user input @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); int n = sc.nextInt(); float result = 0; - for(float i=1;i<=n;i++) { + for(float i=1;i<=n;i++) result +=1/i; - } + System.out.println(result); } diff --git a/basicProblems/Swap_two_numbers.java b/basicProblems/Swap_two_numbers.java index 90bebb2..066a354 100644 --- a/basicProblems/Swap_two_numbers.java +++ b/basicProblems/Swap_two_numbers.java @@ -6,17 +6,12 @@ public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); - int a = sc.nextInt(); - int b = sc.nextInt(); - int swap; + int a = sc.nextInt() , b = sc.nextInt() , swap; + System.out.println( " elements before swapping " + a + " " + b ); - - swap = a; - a = b; - b= swap; - + swap = a; + a = b; + b = swap; System.out.println( " elements after swapping " + a + " " + b ); - - } } diff --git a/basicProblems/Swapping_2.java b/basicProblems/Swapping_2.java new file mode 100644 index 0000000..dd56abc --- /dev/null +++ b/basicProblems/Swapping_2.java @@ -0,0 +1,24 @@ +package basicProblems; + +/* + * swap two given numbers without using third variable + */ + +public class Swapping_2 { + + public static void main(String[] args) { + + int a = 20; + int b = 40; + + System.out.println("Number's before swapping: " + "a = " + a + "," + " b =" + " " + b); + + a = a + b; + b = a - b; + a = a - b; + + System.out.println("Number's after swapping: " + "a = " + a + "," + " b =" + " " + b); + + } + +} diff --git a/basic_idea_of_DS/ArrayDequeDemo.java b/basic_idea_of_DS/ArrayDequeDemo.java new file mode 100644 index 0000000..91718cb --- /dev/null +++ b/basic_idea_of_DS/ArrayDequeDemo.java @@ -0,0 +1,24 @@ +package basic_idea_of_DS; +import java.util.*; +public class ArrayDequeDemo { + + public static void main(String[] args) { + + ArrayDeque adq = new ArrayDeque(); + + adq.push("A"); + adq.push("B"); + adq.push("C"); + adq.push("D"); + adq.push("E"); + + System.out.print("Popping the stack:"); + + while(adq.peek()!=null) + System.out.print(adq.pop()+" "); + + System.out.println(); + + } + +} diff --git a/basic_idea_of_DS/ArrayListDemo.java b/basic_idea_of_DS/ArrayListDemo.java new file mode 100644 index 0000000..eaa79d5 --- /dev/null +++ b/basic_idea_of_DS/ArrayListDemo.java @@ -0,0 +1,33 @@ +package basic_idea_of_DS; +import java.util.ArrayList; + +public class ArrayListDemo { + + public static void main(String[] args) { + + //Create an array list + ArrayList al = new ArrayList(); + System.out.println("Initial size of al:" + al.size()); + + //Add elements to array list + al.add("A"); + al.add("M"); + al.add("A"); + al.add("N"); + al.add("S"); + al.add("s1"); + System.out.println("Size of al after additions:" + al.size()); + + //Display the array list + System.out.println("Contents of al:" + al); + + //Remove elements from the array + al.remove("s1"); + + System.out.println("Size of al after deletions:"+ al.size()); + System.out.println("Contents of al:" + al); + + + } + +} diff --git a/basic_idea_of_DS/Graph.java b/basic_idea_of_DS/Graph.java new file mode 100644 index 0000000..91bd1e8 --- /dev/null +++ b/basic_idea_of_DS/Graph.java @@ -0,0 +1,41 @@ +package basic_idea_of_DS; +import java.util.*; + +public class Graph { + + private LinkedList adj[]; + + @SuppressWarnings("unchecked") + public Graph(int v) { + //array of Linked List + adj = new LinkedList[v]; + + for(int i=0;i(); + } + } + + public void addEdge(int source, int destination) { + adj[source].add(destination); + adj[destination].add(source); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Enter number of vertices and edges"); + int v = sc.nextInt(); + int e = sc.nextInt(); + + Graph graph = new Graph(v); + System.out.println("Enter " + e + " edges"); + + for(int i=0;i map = new HashMap<>(); + + // Add elements to the map + map.put("vishal", 10); + map.put("sachin", 30); + map.put("vaibhav", 20); + + // Print size and content + System.out.println("Size of map is:- " + map.size()); + System.out.println(map); + + // Check if a key is present and if present, print value + if ( map.containsKey("vishal")) + { + Integer a = map.get("vishal"); + System.out.println("value for key vishal is:- "+ a); + } + } + +} diff --git a/basic_idea_of_DS/LinkedListDemo.java b/basic_idea_of_DS/LinkedListDemo.java new file mode 100644 index 0000000..2003b66 --- /dev/null +++ b/basic_idea_of_DS/LinkedListDemo.java @@ -0,0 +1,35 @@ +package basic_idea_of_DS; +import java.util.LinkedList; +public class LinkedListDemo { + + public static void main(String[] args) { + // Create a linked list + LinkedList ll = new LinkedList<>(); + + // Add elements to the linked list + ll.add("A"); + ll.add("M"); + ll.add("M"); + ll.add("N"); + ll.add("S"); + ll.addFirst("S1"); + ll.addLast("S2"); + ll.add(1,"A1"); + System.out.println("Original contents of ll:"+ll); + + //Remove elements from the linked list + ll.remove("A1"); + System.out.println("Content of ll after deletion:"+ll); + + //Remove first & last elements + ll.removeFirst(); + ll.removeLast(); + System.out.println("ll after deleting first & last:" + ll); + + //Get & set value + String val = ll.get(2); + ll.set(2, val + "Changed"); + System.out.println("ll after change:"+ll); + } + +} diff --git a/basic_idea_of_DS/List.java b/basic_idea_of_DS/List.java new file mode 100644 index 0000000..d52f469 --- /dev/null +++ b/basic_idea_of_DS/List.java @@ -0,0 +1,14 @@ +package basic_idea_of_DS; + +import java.util.LinkedList; +public class List{ + public static void main(String[] args) { + + LinkedList ll = new LinkedList<>(); + + ll.add("Aman"); + ll.add("Soni"); + ll.add(1, "Kumar"); + System.out.println(ll); + } +} diff --git a/basic_idea_of_DS/MYStackByList.java b/basic_idea_of_DS/MYStackByList.java new file mode 100644 index 0000000..42fd57c --- /dev/null +++ b/basic_idea_of_DS/MYStackByList.java @@ -0,0 +1,121 @@ +package basic_idea_of_DS; + +//Java program to Implement a stack +//using singly linked list +//import package +import static java.lang.System.exit; + +//Create Stack Using Linked list +class StackUsingLinkedlist { + + // A linked list node + private class Node { + + int data; // integer data + Node link; // reference variable Node type + } + // create global top reference variable global + Node top; + // Constructor + StackUsingLinkedlist() + { + this.top = null; + } + + // Utility function to add an element x in the stack + public void push(int x) // insert at the beginning + { + // create new node temp and allocate memory + Node temp = new Node(); + + // initialize data into data field + temp.data = x; + + // put top reference into link + temp.link = top; + + // update top reference + top = temp; + } + + // Utility function to check if the stack is empty or not + public boolean isEmpty() + { + return top == null; + } + + // Utility function to return top element in a stack + public int peek() + { + // check for empty stack + if (!isEmpty()) { + return top.data; + } + else { + System.out.println("Stack is empty"); + return -1; + } + } + + // Utility function to pop top element from the stack + public void pop() // remove at the beginning + { + // check for stack underflow + if (top == null) { + System.out.print("\nStack Underflow"); + return; + } + + // update the top pointer to point to the next node + top = (top).link; + } + + public void display() + { + // check for stack underflow + if (top == null) { + System.out.printf("\nStack Underflow"); + exit(1); + } + else { + Node temp = top; + while (temp != null) { + + // print node data + System.out.printf("%d->", temp.data); + + // assign temp link to temp + temp = temp.link; + } + } + } +} +//main class +public class MYStackByList { + public static void main(String[] args) + { + // create Object of Implementing class + StackUsingLinkedlist obj = new StackUsingLinkedlist(); + // insert Stack value + obj.push(11); + obj.push(22); + obj.push(33); + obj.push(44); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + + // Delete top element of Stack + obj.pop(); + obj.pop(); + + // print Stack elements + obj.display(); + + // print Top element of Stack + System.out.printf("\nTop element is %d\n", obj.peek()); + } +} diff --git a/basic_idea_of_DS/MyArrayList.java b/basic_idea_of_DS/MyArrayList.java new file mode 100644 index 0000000..bc0609a --- /dev/null +++ b/basic_idea_of_DS/MyArrayList.java @@ -0,0 +1,33 @@ +package basic_idea_of_DS; +import java.util.ArrayList; + +public class MyArrayList { + + public static void main(String[] args) { + + //Create an array list + ArrayList al = new ArrayList(); + System.out.println("Initial size of al:" + al.size()); + + //Add elements to array list + al.add("A"); + al.add("M"); + al.add("A"); + al.add("N"); + al.add("S"); + al.add("s1"); + System.out.println("Size of al after additions:" + al.size()); + + //Display the array list + System.out.println("Contents of al:" + al); + + //Remove elements from the array + al.remove("s1"); + + System.out.println("Size of al after deletions:"+ al.size()); + System.out.println("Contents of al:" + al); + + + } + +} diff --git a/basic_idea_of_DS/MyHashMap.java b/basic_idea_of_DS/MyHashMap.java new file mode 100644 index 0000000..abe8403 --- /dev/null +++ b/basic_idea_of_DS/MyHashMap.java @@ -0,0 +1,28 @@ +package basic_idea_of_DS; +import java.util.HashMap; +public class MyHashMap +{ + + public static void main(String[] args) + { + // Create an empty hash map + HashMap map = new HashMap<>(); + + // Add elements to the map + map.put("vishal", 10); + map.put("sachin", 30); + map.put("vaibhav", 20); + + // Print size and content + System.out.println("Size of map is:- " + map.size()); + System.out.println(map); + + // Check if a key is present and if present, print value + if ( map.containsKey("vishal")) + { + Integer a = map.get("vishal"); + System.out.println("value for key vishal is:- "+ a); + } + } + +} diff --git a/basic_idea_of_DS/MyHashSet.java b/basic_idea_of_DS/MyHashSet.java new file mode 100644 index 0000000..4632e34 --- /dev/null +++ b/basic_idea_of_DS/MyHashSet.java @@ -0,0 +1,21 @@ +package basic_idea_of_DS; + +import java.util.HashSet; + +public class MyHashSet { + + public static void main(String[] args) { + + HashSet hs = new HashSet<>(); + + hs.add("Beta"); + hs.add("Alpha"); + hs.add("Gama"); + hs.add("Eplision"); + hs.add("Omega"); + + System.out.println(hs); + + } + +} diff --git a/basic_idea_of_DS/MyLinkedList.java b/basic_idea_of_DS/MyLinkedList.java new file mode 100644 index 0000000..4d46031 --- /dev/null +++ b/basic_idea_of_DS/MyLinkedList.java @@ -0,0 +1,35 @@ +package basic_idea_of_DS; +import java.util.LinkedList; +public class MyLinkedList { + + public static void main(String[] args) { + // Create a linked list + LinkedList ll = new LinkedList<>(); + + // Add elements to the linked list + ll.add("A"); + ll.add("M"); + ll.add("M"); + ll.add("N"); + ll.add("S"); + ll.addFirst("S1"); + ll.addLast("S2"); + ll.add(1,"A1"); + System.out.println("Original contents of ll:"+ll); + + //Remove elements from the linked list + ll.remove("A1"); + System.out.println("Content of ll after deletion:"+ll); + + //Remove first & last elements + ll.removeFirst(); + ll.removeLast(); + System.out.println("ll after deleting first & last:" + ll); + + //Get & set value + String val = ll.get(2); + ll.set(2, val + "Changed"); + System.out.println("ll after change:"+ll); + } + +} diff --git a/basic_idea_of_DS/MyQueue.java b/basic_idea_of_DS/MyQueue.java new file mode 100644 index 0000000..ac6b1d0 --- /dev/null +++ b/basic_idea_of_DS/MyQueue.java @@ -0,0 +1,82 @@ +//Java program for array implementation of queue +package basic_idea_of_DS; +//A class to represent a queue +class Queue{ + int front, rear, size; + int capacity; + int array[]; + + public Queue(int capacity) { + this.capacity = capacity; + front = this.size = 0; + rear = capacity - 1; + array = new int[this.capacity]; + } + + //Queue is full when size becomes equal to capacity + boolean isFull(Queue queue) { + return(queue.size == queue.capacity); + } + + //Queue is Empty when size is 0 + boolean isEmpty(Queue queue){ + return(queue.size == 0); + } + + //Method to add an item to the queue + //It changes rear and size + void enqueue(int item) { + if(isFull(this)) + return; + this.rear = (this.rear+1)%this.capacity; + this.array[this.rear] = item; + this.size = this.size+1; + System.out.println(item + " enqueud to queue"); + } + + //Method to remove an item from queue + //It changes front and size + int dequeue() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + int item = this.array[this.front]; + this.front = (this.front + 1) % this.capacity; + this.size = this.size-1; + return item; + + } + + //Method to get front of queue + int front() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.front]; + } + + //Method to get rear of queue + int rear() { + if(isEmpty(this)) + return Integer.MIN_VALUE; + + return this.array[this.rear]; + } +} + +//Driver Class +public class MyQueue{ + public static void main(String[] args){ + + Queue queue = new Queue(1000); + + queue.enqueue(10); + queue.enqueue(20); + queue.enqueue(30); + queue.enqueue(40); + + System.out.println(queue.dequeue()+(" dequeued from queue")); + System.out.println("Front item is " + queue.front()); + System.out.println("Rear item is "+ queue.rear()); + } +} diff --git a/basic_idea_of_DS/MyStack.java b/basic_idea_of_DS/MyStack.java new file mode 100644 index 0000000..a3ca5f1 --- /dev/null +++ b/basic_idea_of_DS/MyStack.java @@ -0,0 +1,58 @@ +package basic_idea_of_DS; + + class Stack { + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; + + boolean isEmpty() { + return (top<0); + } + + Stack(){ + top = -1; + } + + boolean push(int x) { + if(top>= (MAX-1)) { + System.out.println(x+"pushed into stack"); + return false; + } + else { + a[++top] = x; + System.out.println(x+"pushed into stack"); + return true; + } + } + + int pop() { + if(top<0) { + System.out.println("Stack underflow"); + return 0; + } + else { + int x = a[top--]; + return x; + } + } + + int peek() { + if(top<0) { + System.out.println("Stack underflow"); + return 0; + } + else { + int x = a[top]; return x; + } + } + + class MyStack{ + public void main (String[] args) { + Stack s = new Stack(); + s.push(10); + s.push(20); + s.push(30); + System.out.println(s.pop() + " Popped from stack"); + } + } +} diff --git a/basic_idea_of_DS/SetExample.java b/basic_idea_of_DS/SetExample.java new file mode 100644 index 0000000..df7ea1d --- /dev/null +++ b/basic_idea_of_DS/SetExample.java @@ -0,0 +1,34 @@ +package basic_idea_of_DS; +// Java program to demonstrate the +// union, intersection and difference +// operations on sets +import java.util.*; +public class SetExample +{ + public static void main(String args[]) + { + Set a = new HashSet(); + a.addAll(Arrays.asList(new Integer[] {1, 3, 2, 4, 8, 9, 0})); + Set b = new HashSet(); + b.addAll(Arrays.asList(new Integer[] {1, 3, 7, 5, 4, 0, 7, 5})); + + // To find union + Set union = new HashSet(a); + union.addAll(b); + System.out.print("Union of the two Set"); + System.out.println(union); + + // To find intersection + Set intersection = new HashSet(a); + + intersection.retainAll(b); + System.out.print("Intersection of the two Set"); + System.out.println(intersection); + + // To find the symmetric difference + Set difference = new HashSet(a); + difference.removeAll(b); + System.out.print("Difference of the two Set"); + System.out.println(difference); + } +} diff --git a/basic_idea_of_DS/Tree.java b/basic_idea_of_DS/Tree.java new file mode 100644 index 0000000..0e79c30 --- /dev/null +++ b/basic_idea_of_DS/Tree.java @@ -0,0 +1,52 @@ +package basic_idea_of_DS; + +//class containing left & right child of current node & key value +class Node{ + int key; + Node left,right; + public Node(int item) { + key = item; + left = right = null; + } +} + +// A java program to introduce Binary Tree +public class Tree { + + //Root node of binary tree + Node root; + + //Constructors + Tree(int key){ + root = new Node(key); + } + + Tree(){ + root = null; + } + public static void main(String[] args) { + Tree t = new Tree(); + //create root + t.root = new Node(1); + + /* following is the tree after above statement + 1 + / \ + null null*/ + + t.root.left = new Node(2); + t.root.right = new Node(3); + /* 2 & 3 become left & right children of 1 + * 1 + * / \ + * 2 3 + * / \ | \ + * null null null null + */ + t.root.left.left = new Node(4); + t.root.right.right = new Node(5); + t.root.right.left = new Node(6); + t.root.left.right = new Node(7); + + } +} diff --git a/basics/ControlStatements.java b/basics/ControlStatements.java index 2962c5f..fd9f096 100644 --- a/basics/ControlStatements.java +++ b/basics/ControlStatements.java @@ -1,28 +1,26 @@ package basics; -// public class to implement or for run the code + public class ControlStatements { public static void main(String[] args) { + // for loop to implement switch case statement - for(int i =0;i<9;i++){ - // switch controle statement - switch(i) { - // case1 - case 1: - System.out.println(" i is zero."); - break; //break statement - //case2 - case 2: - System.out.println(" i is one."); - break; - //case3 - case 3: - System.out.println(" i is zero."); - break; - default: //default statement - System.out.println(" i is greater than 3."); - - } + for(int i = 0 ; i < 9 ; i++){ + + // switch control statement + switch(i) { + case 1: + System.out.println(" i is zero."); + break; + case 2: + System.out.println(" i is one."); + break; + case 3: + System.out.println(" i is zero."); + break; + default: + System.out.println(" i is greater than 3."); + } } } diff --git a/basics/WhileLoop.java b/basics/WhileLoop.java index 6c0195a..c54afa3 100644 --- a/basics/WhileLoop.java +++ b/basics/WhileLoop.java @@ -5,12 +5,10 @@ public class WhileLoop { public static void main(String[] args) { int n = 5; - // while loop block while(n>0) { System.out.println(" tick " + n); - n--; // post substraction operator - } - + // post subtraction operator + n--; + } } - } diff --git a/dataStructures/Info.java b/dataStructures/Info.java new file mode 100644 index 0000000..6f332ad --- /dev/null +++ b/dataStructures/Info.java @@ -0,0 +1,10 @@ +package dataStructures; + +public class Info { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/dataStructures/graphs/Graph.java b/dataStructures/graphs/Graph.java new file mode 100644 index 0000000..2ff08b8 --- /dev/null +++ b/dataStructures/graphs/Graph.java @@ -0,0 +1,41 @@ +package dataStructures.graphs; +import java.util.*; + +public class Graph { + + private LinkedList adj[]; + + @SuppressWarnings("unchecked") + public Graph(int v) { + //array of Linked List + adj = new LinkedList[v]; + + for(int i=0;i(); + } + } + + public void addEdge(int source, int destination) { + adj[source].add(destination); + adj[destination].add(source); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Enter number of vertices and edges"); + int v = sc.nextInt(); + int e = sc.nextInt(); + + Graph graph = new Graph(v); + System.out.println("Enter " + e + " edges"); + + for(int i=0;i (V = 3.14*(r)^2*h) + double V = sg.getPie()*(sg.getRadius()*sg.getRadius())*sg.getHeight(); + System.out.println("The volume of the cylinder is: " + V + " cm^3"); + + } + +} diff --git a/exercise_and_practice_Problems/Exercise1.java b/exercise_and_practice_Problems/Exercise1.java new file mode 100644 index 0000000..b2aca46 --- /dev/null +++ b/exercise_and_practice_Problems/Exercise1.java @@ -0,0 +1,33 @@ +package exercise_and_practice_Problems; +import java.util.*; + +/* To calculate percentage of a given student in CBSE Board exam. + * His marks from 5 subjects must be taken as input from the keyboard + * (Marks are out of 100) + * */ + +public class Exercise1 { + + static Scanner sc = new Scanner(System.in); + + static int subject_1_marks = sc.nextInt(); + static int subject_2_marks = sc.nextInt(); + static int subject_3_marks = sc.nextInt(); + static int subject_4_marks = sc.nextInt(); + static int subject_5_marks = sc.nextInt(); + + public static void main(String[] args) { + + int total = (subject_1_marks + subject_2_marks + subject_3_marks + subject_4_marks + subject_5_marks); + float percentage = (total)/5; + + System.out.println("The marks of the subject's are: \n " + "\n" + "subject_1_marks:- " + subject_1_marks + "\n" + + "subject_2_marks:- " + subject_2_marks + "\n" + "subject_3_marks:- " + subject_3_marks + "\n" + + "subject_4_marks:- " + subject_4_marks + "\n" + "subject_5_marks:- " + subject_5_marks + "\n"); + + System.out.println("The total no. of marks are: " + total); + System.out.println("Total percentage is: " + percentage); + + } + +} diff --git a/exercise_and_practice_Problems/Exercise2.java b/exercise_and_practice_Problems/Exercise2.java new file mode 100644 index 0000000..a395797 --- /dev/null +++ b/exercise_and_practice_Problems/Exercise2.java @@ -0,0 +1,34 @@ +package exercise_and_practice_Problems; +import java.util.Scanner; + +/* + * Take an array as input & print all of its content at particular index wise + */ + +public class Exercise2 { + + public static void main(String[] args) { + try (Scanner sc = new Scanner(System.in)) { + int n,sum =0; + + System.out.println("Enter no. of elements you want in array:"); + n = sc.nextInt(); + + int A[] = new int[n]; + System.out.println("Enter all the elements: "); + + for(int i=0;i 40) System.out.println(" The height of cylender is to high to to find vol & surface area: "); + else this.height = height; + } + + //setter method to set radius of cylinder + public void setRadius(double radius) { + this.radius = radius; + } + + //setter method to set the value of pie + public void setPie(double pie) { + this.pie = pie; + } + + //getter method to get the height of cylinder + public int getHeight() { + return height; + } + + //getter method to get the radius of cylinder + public double getRadius() { + return radius; + } + + //getter method to get the value of pie + public double getPie() { + return pie; + } + + + +} diff --git a/graphs/Graph.java b/graphs/Graph.java new file mode 100644 index 0000000..2ff08b8 --- /dev/null +++ b/graphs/Graph.java @@ -0,0 +1,41 @@ +package dataStructures.graphs; +import java.util.*; + +public class Graph { + + private LinkedList adj[]; + + @SuppressWarnings("unchecked") + public Graph(int v) { + //array of Linked List + adj = new LinkedList[v]; + + for(int i=0;i(); + } + } + + public void addEdge(int source, int destination) { + adj[source].add(destination); + adj[destination].add(source); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Enter number of vertices and edges"); + int v = sc.nextInt(); + int e = sc.nextInt(); + + Graph graph = new Graph(v); + System.out.println("Enter " + e + " edges"); + + for(int i=0;inext is the node to be deleted + // Store pointer to the next of node to be deleted + Node next = temp.next.next; + + temp.next = next; // Unlink the deleted node from list + } + + /* This function prints contents of linked list starting from + the given node */ + public void printList() + { + Node tnode = head; + while (tnode != null) + { + System.out.print(tnode.data+" "); + tnode = tnode.next; + } + } + + /* Driver program to test above functions. Ideally this function + should be in a separate user class. It is kept here to keep + code compact */ + public static void main(String[] args) + { + /* Start with the empty list */ + LinkedList llist = new LinkedList(); + + llist.push(7); + llist.push(1); + llist.push(3); + llist.push(2); + llist.push(8); + + System.out.println("\nCreated Linked list is: "); + llist.printList(); + + llist.deleteNode(4); // Delete node at position 4 + + System.out.println("\nLinked List after Deletion at position 4: "); + llist.printList(); + } +} \ No newline at end of file diff --git a/list/Insertion_in_Linked_List.java b/list/Insertion_in_Linked_List.java new file mode 100644 index 0000000..f395e1a --- /dev/null +++ b/list/Insertion_in_Linked_List.java @@ -0,0 +1,99 @@ +package list; + +public class Insertion_in_Linked_List { + + class Node{ + int data; + Node next; + + Node(int d){ + data = d; + next = null; + } + } + + Node head; + + public void push(int new_data ) { + Node new_node = new Node(new_data); + new_node.next = head; + head = new_node; + } + + public void insertAfter(Node prev_node, int new_data) + { + /* 1. Check if the given Node is null */ + if (prev_node == null) + { + System.out.println("The given previous node cannot be null"); + return; + } + + /* 2 & 3: Allocate the Node & Put in the data*/ + Node new_node = new Node(new_data); + + /* 4. Make next of new Node as next of prev_node */ + new_node.next = prev_node.next; + + /* 5. make next of prev_node as new_node */ + prev_node.next = new_node; + } + + public void append(int new_data) { + + //make an object of above mentioned Node class + Node new_node = new Node(new_data); + + if(head == null) { + head = new_node; + return; + } + + new_node.next = null; + + Node last = head; + while (last.next != null) + last = last.next; + + last.next = new_node; + return; + } + + public void printList() + { + Node tnode = head; + while (tnode != null) + { + System.out.print(tnode.data+" "); + tnode = tnode.next; + } + } + + public static void main(String[] args) { + Insertion_in_Linked_List llist = new Insertion_in_Linked_List(); + /* Start with the empty list */ + + // Insert 6. So linked list becomes 6->NUllist + llist.append(6); + + // Insert 7 at the beginning. So linked list becomes + // 7->6->NUllist + llist.push(7); + + // Insert 1 at the beginning. So linked list becomes + // 1->7->6->NUllist + llist.push(1); + + // Insert 4 at the end. So linked list becomes + // 1->7->6->4->NUllist + llist.append(4); + + // Insert 8, after 7. So linked list becomes + // 1->7->8->6->4->NUllist + llist.insertAfter(llist.head.next, 8); + + System.out.println("\nCreated Linked list is: "); + llist.printList(); + } + +} diff --git a/list/MainList.java b/list/MainList.java new file mode 100644 index 0000000..62e39df --- /dev/null +++ b/list/MainList.java @@ -0,0 +1,18 @@ +package list; +public class MainList { + + public static void main(String[] args) { + + MyLL myll = new MyLL(); + + + myll.add(0); + myll.add(1); + myll.add(2); + myll.add(3); + myll.add(4); + + myll.print(); + } + +} diff --git a/list/MyLL.java b/list/MyLL.java new file mode 100644 index 0000000..478b1da --- /dev/null +++ b/list/MyLL.java @@ -0,0 +1,53 @@ +package list; + +public class MyLL { + + static class Node{ + int data; + Node next; + + public Node(int data) { + this.data = data; + next = null; + } + } + + Node head; + Node position; + + void add(int data) { + Node toAdd = new Node(data); + + if(isEmpty()) { + head = toAdd; + return; + } + + Node temp = head; + while(temp.next != null) { + temp = temp.next; + } + temp.next = toAdd; + } + + void print() { + Node temp = head; + while(temp != null) { + System.out.println(temp.data + " "); + temp = temp.next; + } + + } + + boolean isEmpty() { + return head == null; + } + + void remove(int data) { + head = null; + } + + boolean isValue() { + return head == position; + } +} diff --git a/stack/Parenthesis_Checker_Problem.java b/stack/Parenthesis_Checker_Problem.java new file mode 100644 index 0000000..809e544 --- /dev/null +++ b/stack/Parenthesis_Checker_Problem.java @@ -0,0 +1,74 @@ +package stack; +import java.util.Scanner; +import java.util.Stack; + +public class Parenthesis_Checker_Problem { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while(t-- != 0) { + + String s = sc.nextLine(); + Stack stack = new Stack<>(); + + boolean isBalanced = true; + + for(int i = 0 ; i < s.length() ; i++) { + + char ch = s.charAt(i); + if(ch == '(' || ch == '{' || ch == '[') { + stack.push(ch); + continue; + } + + if(stack.isEmpty()) { + isBalanced = false; + break; + } + + if(ch == ')') { + if(stack.peek() == '(') { + stack.pop(); + }else { + isBalanced = false; + break; + } + } + + if(ch == '}') { + if(stack.peek() == '{') { + stack.pop(); + }else { + isBalanced = false; + break; + } + } + + if(ch == ']') { + if(stack.peek() == '[') { + stack.pop(); + }else { + isBalanced = false; + break; + } + } + + } + + if(!stack.isEmpty()) { + isBalanced = false; + } + + if(isBalanced) { + System.out.println("isBalanced"); + }else { + System.out.println("not balanced"); + } + + } + + } + +} diff --git a/stack/Tower_Of_Hanoi.java b/stack/Tower_Of_Hanoi.java new file mode 100644 index 0000000..4f2e26f --- /dev/null +++ b/stack/Tower_Of_Hanoi.java @@ -0,0 +1,29 @@ +package stack; + +/* + * Stack Application :- Tower of Hanoi Problem(Puzzle 1) + */ +public class Tower_Of_Hanoi { + + //function to solve the tower of hanoi proble + static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { + + if(n == 1) { + System.out.println("Move disk 1 from rod " + from_rod + " to_rod " + to_rod); + return; + } + + towerOfHanoi(n-1, from_rod, to_rod, aux_rod); + System.out.println("Move disk "+ n +" from_rod "+from_rod+" to rod "+to_rod); + + } + + //Driver Method + public static void main(String[] args) { + + int n =4; + towerOfHanoi(n,'A','C','B'); + + } + +} diff --git a/trees/BST_Deletion.java b/trees/BST_Deletion.java new file mode 100644 index 0000000..31bd0b8 --- /dev/null +++ b/trees/BST_Deletion.java @@ -0,0 +1,159 @@ +package trees; + +//Java program to demonstrate delete operation in binary search tree +class BST_Deletion +{ + /* Class containing left and right child of current node and key value*/ + class Node + { + int key; + Node left, right; + + public Node(int item) + { + key = item; + left = right = null; + } + } + + // Root of BST + Node root; + + // Constructor + BST_Deletion() + { + root = null; + } + + // This method mainly calls deleteRec() + void deleteKey(int key) + { + root = deleteRec(root, key); + } + + /* A recursive function to insert a new key in BST */ + Node deleteRec(Node root, int key) + { + /* Base Case: If the tree is empty */ + if (root == null) return root; + + /* Otherwise, recur down the tree */ + if (key < root.key) + root.left = deleteRec(root.left, key); + else if (key > root.key) + root.right = deleteRec(root.right, key); + + // if key is same as root's key, then This is the node + // to be deleted + else + { + // node with only one child or no child + if (root.left == null) + return root.right; + else if (root.right == null) + return root.left; + + // node with two children: Get the inorder successor (smallest + // in the right subtree) + root.key = minValue(root.right); + + // Delete the inorder successor + root.right = deleteRec(root.right, root.key); + } + + return root; + } + + int minValue(Node root) + { + int minv = root.key; + while (root.left != null) + { + minv = root.left.key; + root = root.left; + } + return minv; + } + + // This method mainly calls insertRec() + void insert(int key) + { + root = insertRec(root, key); + } + + /* A recursive function to insert a new key in BST */ + Node insertRec(Node root, int key) + { + + /* If the tree is empty, return a new node */ + if (root == null) + { + root = new Node(key); + return root; + } + + /* Otherwise, recur down the tree */ + if (key < root.key) + root.left = insertRec(root.left, key); + else if (key > root.key) + root.right = insertRec(root.right, key); + + /* return the (unchanged) node pointer */ + return root; + } + + // This method mainly calls InorderRec() + void inorder() + { + inorderRec(root); + } + + // A utility function to do inorder traversal of BST + void inorderRec(Node root) + { + if (root != null) + { + inorderRec(root.left); + System.out.print(root.key + " "); + inorderRec(root.right); + } + } + + // Driver Program to test above functions + public static void main(String[] args) + { + BST_Deletion tree = new BST_Deletion(); + + /* Let us create following BST + 50 + / \ + 30 70 + / \ / \ + 20 40 60 80 */ + tree.insert(50); + tree.insert(30); + tree.insert(20); + tree.insert(40); + tree.insert(70); + tree.insert(60); + tree.insert(80); + + System.out.println("Inorder traversal of the given tree"); + tree.inorder(); + + System.out.println("\nDelete 20"); + tree.deleteKey(20); + System.out.println("Inorder traversal of the modified tree"); + tree.inorder(); + + System.out.println("\nDelete 30"); + tree.deleteKey(30); + System.out.println("Inorder traversal of the modified tree"); + tree.inorder(); + + System.out.println("\nDelete 50"); + tree.deleteKey(50); + System.out.println("Inorder traversal of the modified tree"); + tree.inorder(); + } +} \ No newline at end of file diff --git a/trees/BinarySearchTree.java b/trees/BinarySearchTree.java new file mode 100644 index 0000000..5bf25b3 --- /dev/null +++ b/trees/BinarySearchTree.java @@ -0,0 +1,83 @@ +package trees; + +class BinarySearchTree { + + /* Class containing left and right child of current node and key value*/ + class Node { + int key; + Node left, right; + + public Node(int item) { + key = item; + left = right = null; + } + } + + // Root of BST + Node root; + + // Constructor + BinarySearchTree() { + root = null; + } + + // This method mainly calls insertRec() + void insert(int key) { + root = insertRec(root, key); + } + + /* A recursive function to insert a new key in BST */ + Node insertRec(Node root, int key) { + + /* If the tree is empty, return a new node */ + if (root == null) { + root = new Node(key); + return root; + } + + /* Otherwise, recur down the tree */ + if (key < root.key) + root.left = insertRec(root.left, key); + else if (key > root.key) + root.right = insertRec(root.right, key); + + /* return the (unchanged) node pointer */ + return root; + } + + // This method mainly calls InorderRec() + void inorder() { + inorderRec(root); + } + + // A utility function to do inorder traversal of BST + void inorderRec(Node root) { + if (root != null) { + inorderRec(root.left); + System.out.println(root.key); + inorderRec(root.right); + } + } + + // Driver Program to test above functions + public static void main(String[] args) { + BinarySearchTree tree = new BinarySearchTree(); + + /* Let us create following BST + 50 + / \ + 30 70 + / \ / \ + 20 40 60 80 */ + tree.insert(50); + tree.insert(30); + tree.insert(20); + tree.insert(40); + tree.insert(70); + tree.insert(60); + tree.insert(80); + + // print inorder traversal of the BST + tree.inorder(); + } +} diff --git a/trees/Insertion_In_BinaryTree.java b/trees/Insertion_In_BinaryTree.java index 98e45ad..f57f789 100644 --- a/trees/Insertion_In_BinaryTree.java +++ b/trees/Insertion_In_BinaryTree.java @@ -19,7 +19,7 @@ static class Node { static Node root; static Node temp = root; - //Inorder traversal of a binary tree + //In-order traversal of a binary tree static void inorder(Node temp) { if (temp == null) From 027fdeff8ab5746079735606639157a5fffe44a4 Mon Sep 17 00:00:00 2001 From: Aman Soni <65482419+gitaman8481@users.noreply.github.com> Date: Wed, 28 Oct 2020 18:56:22 +0530 Subject: [PATCH 10/10] Update README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a93624f..f2df7b4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # Java-Programs -This is a repository where, I will upload and keep up to date my Java, DataStructure and algorithms code and question regarding to them,which i practiced on eclipse with help of Books. -The books i refer and use for learning java and algorithms are 1. Java for Dummies. 2. Java The Complete Reference by Herberth Schildt. 3. Algorithms by Sedwig. 4. Algorithms by Cormen. +This is a repository where, I will upload and keep up to date my Java, DataStructure and algorithms code and question regarding to them,which i practiced on eclipse with help of Books and programming websites. +The books I prefer and use for learning java and algorithms are: +1. Java for Dummies. +2. Java The Complete Reference by Herberth Schildt. +3. Algorithms by Sedwig. +4. Algorithms by Cormen.