├── .github └── FUNDING.yml ├── Data Structure ├── All Basic Operations On Stack using Linear Array.txt ├── All Basic Operations On Stack using Linked List.txt ├── All deletion operation of Circular Singly Linked List.txt ├── Average.java ├── BFS.java ├── Circular Queue implementation using Linear Array.txt ├── Circular_Queue_Using_Array.java ├── Code Includes All Basic Operations On Circular Linked List.txt ├── Count the Extra occurance of a number.txt ├── DFS.java ├── Deque Using Circular Array.java ├── Evaluate Postfix Expression.txt ├── Evaluate Prefix Expression.txt ├── Find pairs with given sum in Doubly Linked List.txt ├── Implementation of Merge Sort Algorithm.txt ├── Implementation of Quick Sort Algorithm ( Leftmost element is considered as Pivot).java ├── Implementation of Quick Sort Algorithm ( Middle element as pivot element).java ├── Implementing Stack using Singly Linked List.txt ├── Insert operations in Circular Singly Linked List.txt ├── Insertion_Delete_Tree.java ├── Insertion_sort.java ├── Java program to implement basic stack operations.txt ├── Linear Sort using Doubly Linked list.txt ├── Main.java ├── Main12.java ├── Main122.java ├── Main333.java ├── Middle_pivit.java ├── Priority_Queue_Using_Linked_List.java ├── QuickSort_array.java ├── Reverse a Circular Singly Linked List.txt ├── Reverse of a Double Linked List.txt ├── Selection.java ├── SelectionSort.java ├── Swap_Alternate_node.java ├── TopView.java ├── a.java ├── a1.java ├── a2.java ├── alternate.java ├── append.java ├── bottomView.java ├── bubble_sort.java ├── cir_linklist.java ├── circular.java ├── count_duplicate.java ├── count_vowel.java ├── delete.java ├── delete_circular.java ├── dequeu_linklist.java ├── dequeue.txt ├── from_left.java ├── insertion.java ├── insertion_circular.java ├── k.txt ├── leftView.java ├── levelOrderTransversalOfTree.java ├── linear_sort.java ├── max_depth.java ├── middle data of list.txt ├── occurance.java ├── pair.java ├── prefix.java ├── prefix1.java ├── print.txt ├── priority.java ├── reverse.java ├── reverse_circular.java ├── reverseprint.txt ├── rightview.java └── shift_element.java └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ["https://paypal.me/ashish2030", 13 | "https://www.buymeacoffee.com/ashish2030"] 14 | # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 15 | -------------------------------------------------------------------------------- /Data Structure/All Basic Operations On Stack using Linear Array.txt: -------------------------------------------------------------------------------- 1 | 2 | class Stack 3 | { 4 | int stack[] = new int[5]; 5 | int top = 0; 6 | public void push(int data) 7 | { 8 | if(top==5) 9 | { 10 | System.out.println("Stack Is Full!!!"); 11 | } 12 | else 13 | { 14 | stack[top] = data; 15 | top++; 16 | } 17 | } 18 | public int pop() 19 | { 20 | int data=0; 21 | if(isEmpty()) 22 | { 23 | System.out.println("Stack Is Empty!!!"); 24 | } 25 | else 26 | { 27 | top--; 28 | data = stack[top]; 29 | stack[top] = 0; 30 | } 31 | return data; 32 | } 33 | public int peek() 34 | { 35 | int data; 36 | data = stack[top-1]; 37 | return data; 38 | } 39 | public void show() 40 | { 41 | System.out.print("Updated Stack Is: "); 42 | for(int n:stack) 43 | { 44 | System.out.print(n + " "); 45 | } 46 | System.out.println("\n"); 47 | } 48 | public int size() 49 | { 50 | return top; 51 | } 52 | public boolean isEmpty() 53 | { 54 | return top<=0; 55 | } 56 | public static void main(String args[]) 57 | { 58 | int temp=0; 59 | Stack s = new Stack(); 60 | 61 | System.out.println("\nDefault Size Of Stack Array Is 5\n"); 62 | 63 | System.out.println("Popping Value From Stack..."); 64 | s.pop(); 65 | s.show(); 66 | 67 | System.out.println("Pushing 8, 2, 10 Into Stack..."); 68 | s.push(8); 69 | s.push(2); 70 | s.push(10); 71 | s.show(); 72 | 73 | System.out.println("Size Of Current Stack Is: "+s.size()+"\n"); 74 | 75 | temp = s.pop(); 76 | System.out.println("Popping "+temp+" From Stack..."); 77 | s.show(); 78 | 79 | System.out.print("Peeking Value From Stack: "+s.peek()+"\n\n"); 80 | 81 | temp = s.pop(); 82 | System.out.println("Popping "+temp+" From Stack..."); 83 | s.show(); 84 | 85 | System.out.println("Size Of Current Stack Is: "+s.size()+"\n"); 86 | 87 | System.out.println("Pushing 16 Into Stack..."); 88 | s.push(16); 89 | s.show(); 90 | 91 | System.out.println("Pushing 16, 20 Into Stack..."); 92 | s.push(16); 93 | s.push(20); 94 | s.push(55); 95 | s.show(); 96 | 97 | System.out.println("Is Stack Empty: "+s.isEmpty()+"\n"); 98 | 99 | System.out.println("Pushing 16, 20 Into Stack..."); 100 | s.push(11); 101 | s.show(); 102 | 103 | System.out.print("Peeking Value From Stack: "+s.peek()+"\n\n"); 104 | 105 | System.out.println("Size Of Current Stack Is: "+s.size()+"\n"); 106 | 107 | temp = s.pop(); 108 | System.out.println("Popping "+temp+" From Stack..."); 109 | s.show(); 110 | } 111 | } -------------------------------------------------------------------------------- /Data Structure/All Basic Operations On Stack using Linked List.txt: -------------------------------------------------------------------------------- 1 | class Stack_Using_Linked_List 2 | { 3 | Node top = null; 4 | class Node 5 | { 6 | int data; 7 | Node next; 8 | Node(int d) 9 | { 10 | data = d; 11 | } 12 | } 13 | public void push(int x) 14 | { 15 | Node temp = new Node(x); 16 | temp.next = top; 17 | top = temp; 18 | } 19 | public boolean isEmpty() 20 | { 21 | return top == null; 22 | } 23 | public int peek() 24 | { 25 | if(isEmpty()) 26 | { 27 | System.out.println("Stack Is Empty!!!"); 28 | return 0; 29 | } 30 | else 31 | { 32 | return top.data; 33 | } 34 | } 35 | public int pop() 36 | { 37 | int data; 38 | if(top==null) 39 | { 40 | System.out.println("Stack Is Empty!!!"); 41 | return 0; 42 | } 43 | else 44 | { 45 | data = top.data; 46 | top = top.next; 47 | return data; 48 | } 49 | } 50 | public void display() 51 | { 52 | if(top==null) 53 | { 54 | System.out.println("Stack Is Empty!!!\n"); 55 | return; 56 | } 57 | else 58 | { 59 | Node temp = top; 60 | System.out.print("Updated Stack: "); 61 | while(temp!=null) 62 | { 63 | System.out.print(temp.data + " "); 64 | temp = temp.next; 65 | } 66 | System.out.println("\n"); 67 | } 68 | } 69 | public int size() 70 | { 71 | int count=0; 72 | Node temp = top; 73 | while(temp!=null) 74 | { 75 | count++; 76 | temp = temp.next; 77 | } 78 | return count; 79 | } 80 | public static void main(String args[]) 81 | { 82 | int temp=0; 83 | Stack_Using_Linked_List obj = new Stack_Using_Linked_List(); 84 | 85 | System.out.println("\nCurrent Size Of Stack: "+obj.size()+"\n"); 86 | 87 | System.out.println("Pushing 6, 4, 9 Into Stack..."); 88 | obj.push(6); 89 | obj.push(4); 90 | obj.push(9); 91 | obj.display(); 92 | 93 | System.out.println("Current Size Of Stack: "+obj.size()+"\n"); 94 | 95 | System.out.println("Top Element Of Stack Is: "+obj.peek()+"\n"); 96 | obj.display(); 97 | 98 | temp = obj.pop(); 99 | System.out.println("Popping "+temp+" From Stack"); 100 | obj.display(); 101 | 102 | temp = obj.pop(); 103 | System.out.println("Popping "+temp+" From Stack"); 104 | obj.display(); 105 | 106 | temp = obj.pop(); 107 | System.out.println("Popping "+temp+" From Stack"); 108 | obj.display(); 109 | 110 | System.out.println("Top Element Of Stack Is: "+obj.peek()+"\n"); 111 | 112 | System.out.println("Is Stack Empty: "+obj.isEmpty()+"\n"); 113 | 114 | System.out.println("Pushing 4, 7, 12, 14, 31 Into Stack..."); 115 | obj.push(4); 116 | obj.push(7); 117 | obj.push(12); 118 | obj.push(14); 119 | obj.push(31); 120 | obj.display(); 121 | 122 | System.out.println("Current Size Of Stack: "+obj.size()+"\n"); 123 | } 124 | } -------------------------------------------------------------------------------- /Data Structure/All deletion operation of Circular Singly Linked List.txt: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class delete_circular 3 | { 4 | 5 | static Node head=null; 6 | static Node prev1; 7 | static Node temp; 8 | class Node 9 | { 10 | int data; 11 | Node next; 12 | Node(int d) 13 | { 14 | data = d; 15 | } 16 | } 17 | 18 | public void After(int a) 19 | { 20 | Node temp; 21 | Node head1=head; 22 | int flag=0; 23 | 24 | do 25 | { 26 | if(head1.data==a) 27 | { 28 | flag=1; 29 | if(head1.next==head) 30 | { 31 | System.out.println("No data found to delete"); 32 | return; 33 | } 34 | else 35 | { 36 | temp=head1.next; 37 | temp=temp.next; 38 | if(temp!=head) 39 | { 40 | head1.next=temp; 41 | } 42 | else 43 | { 44 | delete_atlast(head); 45 | } 46 | } 47 | } 48 | head1=head1.next; 49 | } 50 | while(head1!=head); 51 | if(flag==0) 52 | { 53 | System.out.println(a+" Not found"); 54 | } 55 | } 56 | 57 | public void before(int a) 58 | { 59 | Node head1=head; 60 | int flag=0; 61 | do 62 | { 63 | if(head1.data==a) 64 | { 65 | if(head1==head) 66 | { 67 | System.out.println("No data found to delete"); 68 | return; 69 | } 70 | else 71 | { 72 | if(prev1==head) 73 | { 74 | delete_atfirst(head) ; // head=head1; 75 | } 76 | else 77 | { 78 | 79 | temp=head1.next; 80 | prev1.next=temp; 81 | } 82 | } 83 | flag=1; 84 | } 85 | prev1=head1; 86 | head1=head1.next; 87 | } 88 | while(head1!=head); 89 | if(flag==0) 90 | { 91 | System.out.println(a+" Not Found in the list"); 92 | } 93 | } 94 | 95 | 96 | public void push(int data) 97 | { 98 | Node n=new Node(data); 99 | if(head==null) 100 | { 101 | head=n; 102 | n.next=head; 103 | prev1=n; 104 | } 105 | else 106 | { 107 | 108 | prev1.next=n; 109 | n.next=head; 110 | prev1=n; 111 | } 112 | 113 | 114 | } 115 | 116 | public static void delete_atfirst(Node head1) 117 | { 118 | while(head!=head1.next) 119 | { 120 | head1=head1.next; 121 | } 122 | head1.next=head.next; 123 | head=head.next; 124 | } 125 | 126 | 127 | public static void delete_atlast(Node head1) 128 | { 129 | while(head1.next!=head) 130 | { 131 | prev1=head1; 132 | head1=head1.next; 133 | } 134 | prev1.next=head; 135 | } 136 | 137 | public void delete(int a) 138 | { 139 | int flag=0; 140 | Node head1=head; 141 | do 142 | { 143 | if(head1.data==a) 144 | { 145 | if(head1==head) 146 | { 147 | delete_atfirst(head); 148 | flag=1; 149 | } 150 | else if(head1.next==head) 151 | { 152 | delete_atlast(head); 153 | flag=1; 154 | } 155 | else 156 | { 157 | prev1.next=head1.next; 158 | flag=1; 159 | } 160 | } 161 | prev1=head1; 162 | head1=head1.next; 163 | } 164 | while(head1!=head); 165 | if(flag==0) 166 | { 167 | System.out.println("No data found"); 168 | } 169 | } 170 | 171 | public void Print(Node head) 172 | { 173 | Node head1=head; 174 | while(head!=head1.next) 175 | { 176 | System.out.println(head1.data+" "); 177 | head1=head1.next; 178 | 179 | } 180 | System.out.println(head1.data+" "); 181 | } 182 | 183 | public static void main(String arg[]) 184 | { 185 | int push_data=0; 186 | delete_circular b=new delete_circular(); 187 | Scanner sc=new Scanner(System.in); 188 | int a=sc.nextInt(); 189 | while(a>0) 190 | { 191 | push_data=sc.nextInt(); 192 | b.push(push_data); 193 | a--; 194 | } 195 | b.Print(head); 196 | 197 | delete_atfirst(head); 198 | System.out.println("\n delete at first index function"); 199 | b.Print(head); 200 | 201 | delete_atlast(head); 202 | System.out.println("\n delete at last index function"); 203 | b.Print(head); 204 | 205 | int After_node_delete=sc.nextInt(); 206 | System.out.println("\nAfter delete at node whose data is "+After_node_delete+" function"); 207 | b.After(After_node_delete); 208 | b.Print(head); 209 | 210 | int before_node=sc.nextInt(); 211 | System.out.println("\nBefore delete at node whose data is "+before_node); 212 | b.before(before_node); 213 | b.Print(head); 214 | 215 | int node=sc.nextInt(); 216 | System.out.println("\ndelete a specific node "+node); 217 | b.delete(node); 218 | b.Print(head); 219 | } 220 | } 221 | 222 | Input: 223 | 5 224 | 1 225 | 2 226 | 3 227 | 4 228 | 5 229 | 2 230 | 3 231 | 1 232 | 2 233 | 234 | Output: 235 | 1 236 | 2 237 | 3 238 | 4 239 | 5 240 | 241 | delete at first index function 242 | 2 243 | 3 244 | 4 245 | 5 246 | 247 | delete at last index function 248 | 2 249 | 3 250 | 4 251 | 252 | After delete at node whose data is 2 function 253 | 2 254 | 4 255 | 256 | Before delete at node whose data is 3 257 | 3 Not Found in the list 258 | 2 259 | 4 260 | 261 | delete a specific node 1 262 | No data found 263 | 2 264 | 4 -------------------------------------------------------------------------------- /Data Structure/Average.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int d) 7 | { 8 | data=d; 9 | } 10 | } 11 | class Average 12 | { 13 | static Node insertEnd(Node head, int data) 14 | { 15 | Node newLink = new Node(data); 16 | Node last = head; 17 | newLink.next = null; 18 | if (head == null) 19 | { 20 | head = newLink; 21 | return head; 22 | } 23 | while (last.next != null) 24 | last = last.next; 25 | last.next = newLink; 26 | return head; 27 | } 28 | public static void main(String[] args) 29 | { 30 | int t,n,m; 31 | Scanner s = new Scanner(System.in); 32 | t=Integer.parseInt(s.nextLine()); 33 | while(t>0) 34 | { 35 | Node head = null; 36 | n = Integer.parseInt(s.nextLine()); 37 | while(n > 0) 38 | { 39 | m = Integer.parseInt(s.nextLine()); 40 | head = insertEnd(head, m); 41 | n--; 42 | } 43 | 44 | System.out.print("\n"); 45 | Average(head); 46 | System.out.print("\n"); 47 | t--; 48 | } 49 | } 50 | static void Average(Node head) 51 | { 52 | int count=0; 53 | Node head1=head; 54 | Node head2=head; 55 | Node prev=null; 56 | int sum=0; 57 | while(head1!=null) 58 | { 59 | count++; 60 | sum=sum+head1.data; 61 | 62 | head1=head1.next; 63 | } 64 | System.out.println(sum/count); 65 | } 66 | } -------------------------------------------------------------------------------- /Data Structure/BFS.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node left; 6 | Node right; 7 | Node(int data) 8 | { 9 | this.data=data; 10 | } 11 | 12 | } 13 | class BFS 14 | { 15 | static Node root; 16 | public static Node insert(int arr[],Node root,int i) 17 | { 18 | if(i que=new LinkedList<>(); 31 | que.add(root); 32 | while(que.size()>0) 33 | { 34 | Node temp=que.poll(); 35 | System.out.println(temp.data); 36 | if(temp.left!=null) 37 | { 38 | que.add(temp.left); 39 | } 40 | if(temp.right!=null) 41 | { 42 | que.add(temp.right); 43 | } 44 | } 45 | 46 | 47 | } 48 | 49 | public static void main(String args[]) 50 | { 51 | Scanner sc=new Scanner(System.in); 52 | int size=sc.nextInt(); 53 | int arr[]=new int[size]; 54 | for(int i=0;i s=new Stack<>(); 31 | s.push(root); 32 | while(s.size()>0) 33 | { 34 | Node temp=s.pop(); 35 | System.out.println(temp.data); 36 | if(temp.right!=null) 37 | { 38 | s.push(temp.right) ; 39 | } 40 | if(temp.left!=null) 41 | { 42 | s.push(temp.left) ; 43 | } 44 | } 45 | } 46 | 47 | public static void main(String args[]) 48 | { 49 | Scanner sc=new Scanner(System.in); 50 | int size=sc.nextInt(); 51 | int arr[]=new int[size]; 52 | for(int i=0;i='0'&&ch<='9') a.push(Character.getNumericValue(ch)); else 28 | { 29 | int x=a.pop(); 30 | int y=a.pop(); switch(ch) 31 | { 32 | case '+':r=x+y; break; 33 | case '-':r=y-x; break; 34 | case '*':r=x*y; break; 35 | case '/':r=y/x; break; 36 | default:r=0; 37 | } 38 | a.push(r); 39 | } 40 | } 41 | r=a.pop(); return(r); 42 | } 43 | } 44 | 45 | public class Prefix 46 | { 47 | public static void main(String[] args) 48 | { 49 | String input=""; 50 | Scanner sc=new Scanner(System.in); try 51 | { 52 | input=sc.nextLine(); if(input.equals("")) System.out.println("No expression"); Stack1 e=new Stack1(); 53 | System.out.println("Result:"+e.calculate(input)); 54 | } 55 | catch(Exception e) 56 | { 57 | System.out.println("Exception occured"); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Data Structure/Evaluate Prefix Expression.txt: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.lang.String; 3 | import java.util.Scanner; 4 | class Stack1 5 | { 6 | private int[] a; 7 | private int top,m; 8 | public Stack1() 9 | { 10 | } 11 | public Stack1(int max) 12 | { 13 | m=max; 14 | a=new int[m]; 15 | top=-1; 16 | } 17 | public void push(int key) 18 | { 19 | a[++top]=key; 20 | } 21 | public int pop() 22 | { 23 | return(a[top--]); 24 | } 25 | public int peek() 26 | { 27 | return a[top]; 28 | } 29 | public int calculate(String s) 30 | { 31 | int n,r=0; 32 | n=s.length(); 33 | Stack1 a=new Stack1(n); 34 | for (int j = s.length() - 1; j >= 0; j--) 35 | { 36 | char ch=s.charAt(j); 37 | if(ch>='0'&&ch<='9') 38 | a.push(Character.getNumericValue(ch)); 39 | else 40 | { 41 | int x=a.pop(); 42 | int y=a.pop(); 43 | switch(ch) 44 | { 45 | case '+':r=x+y; 46 | break; 47 | case '-':r=x-y; 48 | break; 49 | case '*':r=x*y; 50 | break; 51 | case '/':r=x/y; 52 | break; 53 | default:r=0; 54 | } 55 | a.push(r); 56 | } 57 | } 58 | r=a.pop(); 59 | return(r); 60 | } 61 | } 62 | 63 | public class Prefix 64 | { 65 | public static void main(String[] args) 66 | { 67 | String input=""; 68 | Scanner sc=new Scanner(System.in); 69 | try 70 | { 71 | input=sc.nextLine(); 72 | if(input.equals("")) 73 | System.out.println("No expression"); 74 | Stack1 e=new Stack1(); 75 | System.out.println("Result:"+e.calculate(input)); 76 | } 77 | catch(Exception e) 78 | { 79 | System.out.println("Exception occured"); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /Data Structure/Find pairs with given sum in Doubly Linked List.txt: -------------------------------------------------------------------------------- 1 | public class pair 2 | { 3 | Node prev1; 4 | static Node head; 5 | static class Node 6 | { 7 | int data; 8 | Node next, prev; 9 | Node(int d) 10 | { 11 | data = d; 12 | next = prev = null; 13 | } 14 | } 15 | void pair(Node head,int data) 16 | { 17 | Node head1=head; 18 | int flag=0; 19 | while(head1!=null) 20 | { 21 | Node head2=head1.next; 22 | while(head2!=null) 23 | { 24 | if(data==(head1.data+head2.data)) 25 | { 26 | flag=1; 27 | System.out.println( "(" + head1.data + ", "+ head2.data + ")" ); 28 | } 29 | head2=head2.next; 30 | } 31 | head1=head1.next; 32 | } 33 | if(flag==0) 34 | { 35 | System.out.println("Pair not found"); 36 | } 37 | } 38 | void push(int data) 39 | { 40 | Node k=new Node(data); 41 | if(head==null) 42 | { 43 | prev1=k; 44 | head=k; 45 | } 46 | else 47 | { 48 | prev1.next=k; 49 | k.prev=prev1; 50 | prev1=k; 51 | } 52 | } 53 | void printList(Node node) 54 | { 55 | while (node != null) 56 | { 57 | System.out.println(node.data + " "); 58 | node = node.next; 59 | } 60 | } 61 | public static void main(String[] args) 62 | { 63 | pair list = new pair(); 64 | list.push(2); 65 | list.push(4); 66 | list.push(6); 67 | list.push(10); 68 | list.push(1); 69 | list.push(3); 70 | list.push(5); 71 | list.push(7); 72 | System.out.println("Original linked list "); 73 | list.printList(head); 74 | list.pair(head,12); 75 | } 76 | } -------------------------------------------------------------------------------- /Data Structure/Implementation of Merge Sort Algorithm.txt: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class MergeSort 3 | { 4 | 5 | void merge(int arr[], int l, int m, int r) 6 | { 7 | int n1 = m - l + 1; 8 | int n2 = r - m; 9 | int arr_Left[] = new int [n1]; 10 | int arr_Right[] = new int [n2]; 11 | for (int i=0; i pivot) 15 | { 16 | right--; 17 | } 18 | if (left <= right) 19 | { 20 | int tmp = arr[left]; 21 | arr[left] = arr[right]; 22 | arr[right] = tmp; 23 | left++; 24 | right--; 25 | } 26 | } 27 | return left; 28 | } 29 | void sort(int arr[], int left, int right) 30 | { 31 | if (left < right) 32 | { 33 | int x = divide(arr, left, right); 34 | sort(arr, left, x-1); 35 | sort(arr, x, right); 36 | } 37 | } 38 | void printArray(int arr[]) 39 | { 40 | int n = arr.length; 41 | for (int i=0; i0) 156 | { 157 | int data=sc.nextInt(); 158 | b.append_value(data); 159 | a--; 160 | } 161 | System.out.println("After append function"); 162 | b.Print(head); 163 | int push_data=sc.nextInt(); 164 | b.push(push_data); 165 | System.out.println("\nAfter push "+push_data+" data in function"); 166 | b.Print(head); 167 | 168 | int After_node=sc.nextInt(); 169 | int insert=sc.nextInt(); 170 | b.After(After_node,insert); 171 | System.out.println("\nAfter insertion at node "+After_node+" function"); 172 | b.Print(head); 173 | 174 | int before_node=sc.nextInt(); 175 | insert=sc.nextInt(); 176 | b.before(before_node,insert); 177 | System.out.println("\nBefore insertion at node "+before_node+" function"); 178 | b.Print(head); 179 | } 180 | } 181 | 182 | Input: 183 | 5 184 | 1 185 | 2 186 | 3 187 | 4 188 | 5 189 | 6 190 | 3 191 | 10 192 | 6 193 | 0 194 | 195 | Output: 196 | After append function 197 | Your list is : 1-2-3-4-5 198 | 199 | After push 6 data in function 200 | Your list is : 6-1-2-3-4-5 201 | 202 | After insertion at node 3 function 203 | Your list is : 6-1-2-3-10-4-5 204 | 205 | Before insertion at node 6 function 206 | Your list is : 0-6-1-2-3-10-4-5 -------------------------------------------------------------------------------- /Data Structure/Insertion_Delete_Tree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Insertion_Delete_Tree 3 | { 4 | static Node head; 5 | static Node temp; 6 | Node prev1; 7 | class Node 8 | { 9 | int data; 10 | Node prev; 11 | Node next; 12 | Node(int d) 13 | { 14 | data = d; 15 | } 16 | } 17 | 18 | public void push(int data) 19 | { 20 | Node n=new Node(data); 21 | if(head==null) 22 | { 23 | head=n; 24 | prev1=n; 25 | 26 | } 27 | else 28 | { 29 | n.next=head; 30 | head.prev=n; 31 | head=n; 32 | } 33 | } 34 | 35 | 36 | public void append_value(int data) 37 | { 38 | Node n=new Node(data); 39 | if(head==null) 40 | { 41 | head=n; 42 | prev1=n; 43 | 44 | } 45 | else 46 | { 47 | prev1.next=n; 48 | n.prev=prev1; 49 | prev1=n; 50 | } 51 | } 52 | 53 | // Deletion Start when address is given 54 | 55 | 56 | public static void delete_atfirst(Node head1) 57 | { 58 | head1=head1.next; 59 | head1.prev=null; 60 | head=head1; 61 | } 62 | 63 | 64 | public static void delete_atlast(Node head1) 65 | { 66 | while(head1.next!=null) 67 | { 68 | head1=head1.next; 69 | } 70 | temp= head1.prev; 71 | temp.next=null; 72 | } 73 | 74 | // Deletion Start when value is given 75 | 76 | public void delete(int a) 77 | { 78 | int flag=0; 79 | Node head1=head; 80 | while(head1!=null) 81 | { 82 | if(head1.data==a) 83 | { 84 | if(head1.prev==null) 85 | { 86 | //delete_atfirst(head); 87 | head1=head1.next; 88 | head1.prev=null; 89 | head=head1; 90 | } 91 | 92 | else if(head1.next==null) 93 | { 94 | //delete_atlast(head); 95 | temp= head1.prev; 96 | temp.next=null; 97 | } 98 | else 99 | { 100 | temp=head1.prev; 101 | temp.next=head1.next; 102 | temp=head1.next; 103 | temp.prev=head1.prev; 104 | flag=1; 105 | } 106 | } 107 | head1=head1.next; 108 | } 109 | if(flag==0) 110 | { 111 | System.out.println("No data found"); 112 | } 113 | } 114 | 115 | 116 | public void After(int a) 117 | { 118 | Node head1=head; 119 | int flag=0; 120 | while(head1!=null) 121 | { 122 | if(head1.data==a) 123 | { 124 | flag=1; 125 | if(head1.next==null) 126 | { 127 | System.out.println("No data found to delete"); 128 | return; 129 | } 130 | else 131 | { 132 | temp=head1.next; 133 | temp=temp.next; 134 | if(temp!=null) 135 | { 136 | temp.prev=head1; 137 | head1.next=temp; 138 | } 139 | else 140 | { 141 | delete_atlast(head);// head1.next=null; 142 | } 143 | } 144 | } 145 | head1=head1.next; 146 | } 147 | if(flag==0) 148 | { 149 | System.out.println(a+" Not found"); 150 | } 151 | } 152 | 153 | 154 | 155 | public void before(int a) 156 | { 157 | Node head1=head; 158 | int flag=0; 159 | while(head1!=null) 160 | { 161 | if(head1.data==a) 162 | { 163 | if(head1.prev==null) 164 | { 165 | System.out.println("No data found to delete"); 166 | return; 167 | } 168 | else 169 | { 170 | if(prev1.prev==null) 171 | { 172 | delete_atfirst(head) ; 173 | } 174 | else 175 | { 176 | 177 | temp=prev1.prev; 178 | head1.prev=temp; 179 | temp.next=head1; 180 | } 181 | } 182 | flag=1; 183 | } 184 | prev1=head1; 185 | head1=head1.next; 186 | } 187 | if(flag==0) 188 | { 189 | System.out.println(a+" Not Found in the list"); 190 | } 191 | } 192 | 193 | 194 | public void Print(Node head) 195 | { 196 | if(head==null) 197 | { 198 | return; 199 | } 200 | System.out.print(head.data+"-"); 201 | Print(head.next); 202 | } 203 | 204 | 205 | public static void main(String arg[]) 206 | { 207 | Insertion_Delete_Tree b = new Insertion_Delete_Tree(); 208 | Scanner sc=new Scanner(System.in); 209 | int a=sc.nextInt(); 210 | while(a>0) 211 | { 212 | int data=sc.nextInt(); 213 | b.append_value(data); 214 | a--; 215 | } 216 | System.out.println("After append function"); 217 | b.Print(head); 218 | 219 | int push_data=sc.nextInt(); 220 | b.push(push_data); 221 | System.out.println("\npush "+push_data+" data in function"); 222 | b.Print(head); 223 | 224 | delete_atfirst(head); 225 | System.out.println("\n delete at first index function"); 226 | b.Print(head); 227 | 228 | delete_atlast(head); 229 | System.out.println("\n delete at last index function"); 230 | b.Print(head); 231 | 232 | int After_node_delete = sc.nextInt(); 233 | System.out.println("\nAfter delete at node whose data is "+After_node_delete+" function"); 234 | b.After(After_node_delete); 235 | b.Print(head); 236 | 237 | 238 | int before_node=sc.nextInt(); 239 | System.out.println("\nBefore delete at node whose data is "+before_node); 240 | b.before(before_node); 241 | b.Print(head); 242 | 243 | int node=sc.nextInt(); 244 | System.out.println("\ndelete a specific node "+node); 245 | b.delete(node); 246 | b.Print(head); 247 | 248 | } 249 | } -------------------------------------------------------------------------------- /Data Structure/Insertion_sort.java: -------------------------------------------------------------------------------- 1 | class Insertion_sort 2 | 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int index=0; 8 | int k=sc.nextInt(); 9 | int arr[]=new int[k]; 10 | for(int i=0;i=0;j--) 18 | { 19 | int data=arr[temp]; 20 | if(arr[j]>data) 21 | { 22 | int m=arr[temp]; 23 | arr[temp]=arr[j]; 24 | arr[j]=m; 25 | temp=j; 26 | } 27 | else 28 | { 29 | break; 30 | } 31 | } 32 | } 33 | for(int i=0;i= (MAX - 1)) 26 | { 27 | System.out.println("Stack Overflow"); 28 | return false; 29 | } 30 | else 31 | { 32 | ++top; 33 | a[top] = x; 34 | System.out.println(x + " pushed into stack"); 35 | return true; 36 | } 37 | } 38 | 39 | int pop() 40 | { 41 | if (top < 0) 42 | { 43 | System.out.println("Stack Underflow"); 44 | return 0; 45 | } 46 | else 47 | { 48 | int x = a[top]; 49 | top--; 50 | return x; 51 | } 52 | } 53 | 54 | int peek() 55 | { 56 | if (top < 0) 57 | { 58 | System.out.println("Stack Underflow"); 59 | return 0; 60 | } 61 | else 62 | { 63 | int x = a[top]; 64 | return x; 65 | } 66 | } 67 | } 68 | 69 | // Driver code 70 | class Main 71 | { 72 | public static void main(String args[]) 73 | { 74 | Stack s = new Stack(); 75 | s.push(10); 76 | s.push(20); 77 | s.push(30); 78 | System.out.println(s.isEmpty()); 79 | System.out.println(s.peek()); 80 | System.out.println(s.pop() + " Popped from stack"); 81 | System.out.println(s.pop() + " Popped from stack"); 82 | System.out.println(s.pop() + " Popped from stack"); 83 | System.out.println(s.isEmpty()); 84 | System.out.println(s.peek()); 85 | } 86 | } -------------------------------------------------------------------------------- /Data Structure/Linear Sort using Doubly Linked list.txt: -------------------------------------------------------------------------------- 1 | 2 | public class linear_sort 3 | { 4 | Node prev1; 5 | static Node head; 6 | static class Node 7 | { 8 | int data; 9 | Node next, prev; 10 | Node(int d) 11 | { 12 | data = d; 13 | next = prev = null; 14 | } 15 | } 16 | 17 | public void sort(Node head) 18 | { 19 | Node k=null; 20 | Node k1=null; 21 | int temp; 22 | for(k=head;k.next!=null;k=k.next) 23 | { 24 | for(k1=k.next;k1!=null;k1=k1.next) 25 | { 26 | if(k.data>k1.data) 27 | { 28 | temp=k.data; 29 | k.data=k1.data; 30 | k1.data=temp; 31 | } 32 | } 33 | } 34 | } 35 | 36 | void push(int data) 37 | { 38 | Node k=new Node(data); 39 | if(head==null) 40 | { 41 | prev1=k; 42 | head=k; 43 | } 44 | else 45 | { 46 | prev1.next=k; 47 | k.prev=prev1; 48 | prev1=k; 49 | } 50 | } 51 | 52 | void printList(Node node) 53 | { 54 | while (node != null) 55 | { 56 | System.out.println(node.data + " "); 57 | node = node.next; 58 | } 59 | } 60 | public static void main(String[] args) 61 | { 62 | linear_sort list = new linear_sort(); 63 | list.push(5); 64 | list.push(4); 65 | list.push(3); 66 | list.push(2); 67 | list.push(1); 68 | list.sort(head); 69 | list.printList(head); 70 | } 71 | } -------------------------------------------------------------------------------- /Data Structure/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | //--------------------------Not Copied From GeeksforGeeks Ide Like others-------------------------------- 3 | class Node 4 | { 5 | int data; 6 | Node next; 7 | Node(int d) 8 | { 9 | data=d; 10 | } 11 | } 12 | class Main 13 | { 14 | static Node insertEnd(Node head, int data) 15 | { 16 | Node newLink = new Node(data); 17 | Node last = head; 18 | newLink.next = null; 19 | if (head == null) 20 | { 21 | head = newLink; 22 | return head; 23 | } 24 | while (last.next != null) 25 | last = last.next; 26 | last.next = newLink; 27 | return head; 28 | } 29 | public static void main(String[] args) 30 | { 31 | int t,n,m; 32 | Scanner s = new Scanner(System.in); 33 | t=Integer.parseInt(s.nextLine()); 34 | while(t>0) 35 | { 36 | Node head = null; 37 | n = Integer.parseInt(s.nextLine()); 38 | while(n > 0) 39 | { 40 | m = Integer.parseInt(s.nextLine()); 41 | head = insertEnd(head, m); 42 | n--; 43 | } 44 | Print(head); 45 | System.out.print("\n"); 46 | duplicate(head); 47 | System.out.print("\n"); 48 | t--; 49 | } 50 | } 51 | static void Print(Node head) 52 | { 53 | if(head==null) 54 | { 55 | return; 56 | } 57 | System.out.print(head.data+"-"); 58 | Print(head.next); 59 | } 60 | static void duplicate(Node head) 61 | { 62 | Node head1=head; 63 | Node head2=head; 64 | Node prev=null; 65 | while(head1!=null) 66 | { 67 | head2=head1.next; 68 | prev=head1; 69 | while(head2!=null) 70 | { 71 | if(head1.data==head2.data) 72 | { 73 | prev.next=head2.next; 74 | } 75 | else 76 | { 77 | prev=head2; 78 | } 79 | head2=head2.next; 80 | } 81 | head1=head1.next; 82 | } 83 | Print(head); 84 | } 85 | } -------------------------------------------------------------------------------- /Data Structure/Main12.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class QuickSort_array 3 | { 4 | int divide(int arr[], int left, int right) 5 | { 6 | int pivit=arr[right]; 7 | int i=left-1; 8 | for(int j=left;j pivot) 15 | { 16 | right--; 17 | } 18 | if (left <= right) 19 | { 20 | int tmp = arr[left]; 21 | arr[left] = arr[right]; 22 | arr[right] = tmp; 23 | left++; 24 | right--; 25 | } 26 | } 27 | return left; 28 | } 29 | void sort(int arr[], int left, int right) 30 | { 31 | if (left < right) 32 | { 33 | int x = divide(arr, left, right); 34 | sort(arr, left, x-1); 35 | sort(arr, x, right); 36 | } 37 | } 38 | void printArray(int arr[]) 39 | { 40 | int n = arr.length; 41 | for (int i=0; ip) 41 | { 42 | temp.next = head; 43 | head = temp; 44 | } 45 | else 46 | { 47 | while(start.next!=null && start.next.priorityarr[j]) 23 | { 24 | min=arr[j]; 25 | index=j; 26 | } 27 | 28 | } 29 | int temp=arr[i]; 30 | arr[i]=arr[index]; 31 | arr[index]=temp; 32 | } 33 | for(int i=0;iarr[j]) 23 | { 24 | min=arr[j]; 25 | index=j; 26 | } 27 | 28 | } 29 | int temp=arr[i]; 30 | arr[i]=arr[index]; 31 | arr[index]=temp; 32 | } 33 | for(int i=0;i0) 34 | { 35 | Node head = null; 36 | n = Integer.parseInt(s.nextLine()); 37 | 38 | while(n > 0) 39 | { 40 | m = Integer.parseInt(s.nextLine()); 41 | head = insertEnd(head, m); 42 | n--; 43 | } 44 | Print(head); 45 | System.out.print("\n"); 46 | Swap_Alternate_node(head); 47 | System.out.print("\n"); 48 | t--; 49 | } 50 | } 51 | static void Print(Node head) 52 | { 53 | if(head==null) 54 | { 55 | return; 56 | } 57 | System.out.print(head.data+"-"); 58 | Print(head.next); 59 | } 60 | static void Swap_Alternate_node(Node head) 61 | { 62 | Node head1=head; 63 | Node head2=head; 64 | int count=0; 65 | while(head1!=null) 66 | { 67 | count++; 68 | head1=head1.next; 69 | } 70 | Node prev=null; 71 | int temp=0; 72 | for(int i=1;i<=count;i++) 73 | { 74 | if(i%2==0) 75 | { 76 | temp=prev.data; 77 | prev.data=head2.data; 78 | head2.data=temp; 79 | } 80 | prev=head2; 81 | head2=head2.next; 82 | } 83 | Print(head); 84 | } 85 | } -------------------------------------------------------------------------------- /Data Structure/TopView.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node left; 6 | Node right; 7 | Node(int data) 8 | { 9 | this.data=data; 10 | } 11 | 12 | } 13 | class TopView 14 | { 15 | static Node root; 16 | public static Node insert(int arr[],Node root,int i) 17 | { 18 | if(i que=new LinkedList<>(); 41 | Map map=new TreeMap<>(); 42 | if(root==null) 43 | { 44 | System.out.println("Error"); 45 | } 46 | else 47 | { 48 | que.add(new obj(root,0)); 49 | while(que.size()>0) 50 | { 51 | obj temp=que.poll(); 52 | if(!map.containsKey(temp.level)) 53 | { 54 | map.put(temp.level,temp.data); 55 | } 56 | if(temp.data.left!=null) 57 | { 58 | que.add(new obj(temp.data.left,temp.level-1)); 59 | } 60 | if(temp.data.right!=null) 61 | { 62 | que.add(new obj(temp.data.right,temp.level+1)); 63 | } 64 | } 65 | 66 | } 67 | for(Map.Entry s:map.entrySet()) 68 | { 69 | System.out.println(s.getValue().data); 70 | } 71 | } 72 | public static void main(String args[]) 73 | { 74 | Scanner sc=new Scanner(System.in); 75 | int size=sc.nextInt(); 76 | int arr[]=new int[size]; 77 | for(int i=0;ik.data) 37 | { 38 | if(k==head) 39 | { 40 | head=k.next; 41 | } 42 | else 43 | { 44 | prev.next=k.next; 45 | 46 | } 47 | } 48 | prev=k; 49 | k=k.next; 50 | } 51 | 52 | return head; 53 | } 54 | public void printList() 55 | { 56 | Node n=head; 57 | while (n != null) { 58 | System.out.print(n.data + " "); 59 | n = n.next; 60 | } 61 | } 62 | } 63 | class a{ 64 | public static void main(String[] args) 65 | { 66 | LinkedList llist = new LinkedList(); 67 | Scanner sc=new Scanner(System.in); 68 | int t=sc.nextInt(); 69 | int b; 70 | while(t>0) 71 | { 72 | b=sc.nextInt(); 73 | llist.addNode(b); 74 | t--; 75 | } 76 | 77 | llist.delete(); 78 | llist.printList(); 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /Data Structure/a1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class LinkedList 3 | { 4 | static Node head; 5 | static Node tail; 6 | 7 | static class Node { 8 | int data; 9 | Node next; 10 | Node(int d) 11 | { 12 | data = d; 13 | next = null; 14 | } 15 | } 16 | public void addNode(int data) 17 | { 18 | Node newNode = new Node(data); 19 | if(head == null) { 20 | head = newNode; 21 | tail=newNode; 22 | } 23 | else { 24 | 25 | tail.next = newNode; 26 | tail = newNode; 27 | } 28 | } 29 | Node delete() 30 | { 31 | int count=0; 32 | Node prev=null; 33 | Node k=head; 34 | while(k!=null) 35 | { 36 | if(count%2!=0) 37 | { 38 | prev.next=k.next; 39 | } 40 | count++; 41 | prev=k; 42 | k=k.next; 43 | } 44 | 45 | return head; 46 | } 47 | public void printList() 48 | { 49 | Node n=head; 50 | while (n != null) { 51 | System.out.print(n.data + " "); 52 | n = n.next; 53 | } 54 | } 55 | } 56 | class a1{ 57 | public static void main(String[] args) 58 | { 59 | LinkedList llist = new LinkedList(); 60 | Scanner sc=new Scanner(System.in); 61 | int t=sc.nextInt(); 62 | int b; 63 | while(t>0) 64 | { 65 | b=sc.nextInt(); 66 | llist.addNode(b); 67 | t--; 68 | } 69 | 70 | llist.delete(); 71 | llist.printList(); 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /Data Structure/a2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class LinkedList 3 | { 4 | static Node head; 5 | static Node tail; 6 | 7 | static class Node { 8 | int data; 9 | Node next; 10 | Node(int d) 11 | { 12 | data = d; 13 | next = null; 14 | } 15 | } 16 | public void addNode(int data) 17 | { 18 | Node newNode = new Node(data); 19 | if(head == null) { 20 | head = newNode; 21 | tail=newNode; 22 | } 23 | else { 24 | 25 | tail.next = newNode; 26 | tail = newNode; 27 | } 28 | } 29 | static void duplicate() 30 | { 31 | Node head1=head; 32 | Node head2=head; 33 | Node prev=null; 34 | while(head1!=null) 35 | { 36 | head2=head1.next; 37 | prev=head1; 38 | while(head2!=null) 39 | { 40 | if(head1.data==head2.data) 41 | { 42 | prev.next=head2.next; 43 | } 44 | else 45 | { 46 | prev=head2; 47 | } 48 | head2=head2.next; 49 | } 50 | head1=head1.next; 51 | } 52 | } 53 | public void printList() 54 | { 55 | Node n=head; 56 | while (n != null) { 57 | System.out.print(n.data + " "); 58 | n = n.next; 59 | } 60 | } 61 | } 62 | class a2{ 63 | public static void main(String[] args) 64 | { 65 | LinkedList llist = new LinkedList(); 66 | Scanner sc=new Scanner(System.in); 67 | int t=sc.nextInt(); 68 | int b; 69 | while(t>0) 70 | { 71 | b=sc.nextInt(); 72 | llist.addNode(b); 73 | t--; 74 | } 75 | 76 | llist.duplicate(); 77 | llist.printList(); 78 | } 79 | } 80 | 81 | -------------------------------------------------------------------------------- /Data Structure/alternate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int d) 7 | { 8 | data = d; 9 | next = null; 10 | } 11 | } 12 | class LinkedList 13 | { 14 | static Node head; 15 | static Node tail; 16 | public void addNode(int data) 17 | { 18 | Node newNode = new Node(data); 19 | if(head == null) { 20 | head = newNode; 21 | tail=newNode; 22 | } 23 | else { 24 | tail.next = newNode; 25 | tail = newNode; 26 | } 27 | } 28 | static Node AlternatingSplit() 29 | { 30 | Node head1=head; 31 | Node head2=null; 32 | Node prev=null; 33 | Node temp=null; 34 | Node temp1=null; 35 | int count=0; 36 | while(head1!=null) 37 | { 38 | temp1=head1.next; 39 | if(count%2!=0) 40 | { 41 | prev.next=head1.next; 42 | if(head2==null) 43 | { 44 | head1.next=null; 45 | head2=head1; 46 | temp=head2; 47 | } 48 | else 49 | { 50 | head1.next=null; 51 | temp.next=head1; 52 | } 53 | } 54 | count++; 55 | prev=head1; 56 | head1=temp1; 57 | } 58 | return head2; 59 | } 60 | public void printList() 61 | { 62 | Node n=head; 63 | while (n != null) { 64 | System.out.print(n.data + " "); 65 | n = n.next; 66 | } 67 | System.out.println(); 68 | } 69 | } 70 | class alternate 71 | { 72 | public static void main(String[] args) 73 | { 74 | LinkedList llist = new LinkedList(); 75 | Scanner sc=new Scanner(System.in); 76 | int t=sc.nextInt(); 77 | int b; 78 | while(t>0) 79 | { 80 | b=sc.nextInt(); 81 | llist.addNode(b); 82 | t--; 83 | } 84 | 85 | Node m=llist.AlternatingSplit(); 86 | llist.printList(); 87 | LinkedList.head=m; 88 | llist.printList(); 89 | 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Data Structure/append.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class append 3 | { 4 | static Node head; 5 | Node temp; 6 | Node prev1; 7 | class Node 8 | { 9 | int data; 10 | Node prev; 11 | Node next; 12 | Node(int d) 13 | { 14 | data = d; 15 | } 16 | } 17 | public void append_value(int data) 18 | { 19 | Node n=new Node(data); 20 | if(head==null) 21 | { 22 | head=n; 23 | prev1=n; 24 | 25 | } 26 | else 27 | { 28 | prev1.next=n; 29 | n.prev=prev1; 30 | prev1=n; 31 | } 32 | } 33 | public void Print(Node head) 34 | { 35 | if(head==null) 36 | { 37 | return; 38 | } 39 | System.out.print(head.data+"-"); 40 | Print(head.next); 41 | } 42 | public static void main(String arg[]) 43 | { 44 | append b=new append(); 45 | Scanner sc=new Scanner(System.in); 46 | int a=sc.nextInt(); 47 | while(a>0) 48 | { 49 | int data=sc.nextInt(); 50 | b.append_value(data); 51 | a--; 52 | } 53 | b.Print(head); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Data Structure/bottomView.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node left; 6 | Node right; 7 | Node(int data) 8 | { 9 | this.data=data; 10 | } 11 | 12 | } 13 | class BottomView 14 | { 15 | static Node root; 16 | public static Node insert(int arr[],Node root,int i) 17 | { 18 | if(i que=new LinkedList<>(); 41 | Map map=new TreeMap<>(); 42 | if(root==null) 43 | { 44 | System.out.println("Error"); 45 | } 46 | else 47 | { 48 | que.add(new obj(root,0)); 49 | while(que.size()>0) 50 | { 51 | obj temp=que.poll(); 52 | map.put(temp.level,temp.data); 53 | 54 | if(temp.data.left!=null) 55 | { 56 | que.add(new obj(temp.data.left,temp.level-1)); 57 | } 58 | if(temp.data.right!=null) 59 | { 60 | que.add(new obj(temp.data.right,temp.level+1)); 61 | } 62 | } 63 | 64 | } 65 | for(Map.Entry s:map.entrySet()) 66 | { 67 | System.out.println(s.getValue().data); 68 | } 69 | } 70 | public static void main(String args[]) 71 | { 72 | Scanner sc=new Scanner(System.in); 73 | int size=sc.nextInt(); 74 | int arr[]=new int[size]; 75 | for(int i=0;iarr[j+1]) 20 | { 21 | int temp=arr[j]; 22 | arr[j]=arr[j+1]; 23 | arr[j+1]=temp; 24 | } 25 | } 26 | } 27 | for(int i=0;i0) 35 | { 36 | Node head = null; 37 | n = Integer.parseInt(s.nextLine()); 38 | 39 | while(n > 0) 40 | { 41 | m = s.next().charAt(0); 42 | head = insertEnd(head, m); 43 | n--; 44 | } 45 | Print(head); 46 | System.out.print("\n"); 47 | count_vowel(head); 48 | System.out.print("\n"); 49 | t--; 50 | } 51 | } 52 | static void Print(Node head) 53 | { 54 | if(head==null) 55 | { 56 | return; 57 | } 58 | System.out.print(head.data+"-"); 59 | Print(head.next); 60 | } 61 | static void count_vowel(Node head) 62 | { 63 | Node head1=head; 64 | int count=0; 65 | while(head1!=null) 66 | { 67 | if(head1.data=='A'||head1.data=='a'||head1.data=='e'||head1.data=='E'||head1.data=='i'||head1.data=='I'||head1.data=='o'||head1.data=='O'||head1.data=='u'||head1.data=='U') 68 | { 69 | count++; 70 | } 71 | head1=head1.next; 72 | } 73 | System.out.println(count); 74 | } 75 | } -------------------------------------------------------------------------------- /Data Structure/delete.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class delete 3 | { 4 | static Node head; 5 | static Node temp; 6 | Node prev1; 7 | class Node 8 | { 9 | int data; 10 | Node prev; 11 | Node next; 12 | Node(int d) 13 | { 14 | data = d; 15 | } 16 | } 17 | public void append_value(int data) 18 | { 19 | Node n=new Node(data); 20 | if(head==null) 21 | { 22 | head=n; 23 | prev1=n; 24 | 25 | } 26 | else 27 | { 28 | prev1.next=n; 29 | n.prev=prev1; 30 | prev1=n; 31 | } 32 | } 33 | public void After(int a) 34 | { 35 | Node head1=head; 36 | int flag=0; 37 | while(head1!=null) 38 | { 39 | if(head1.data==a) 40 | { 41 | flag=1; 42 | if(head1.next==null) 43 | { 44 | System.out.println("No data found to delete"); 45 | return; 46 | } 47 | else 48 | { 49 | temp=head1.next; 50 | temp=temp.next; 51 | if(temp!=null) 52 | { 53 | temp.prev=head1; 54 | head1.next=temp; 55 | } 56 | else 57 | { 58 | delete_atlast(head);// head1.next=null; 59 | } 60 | } 61 | } 62 | head1=head1.next; 63 | } 64 | if(flag==0) 65 | { 66 | System.out.println(a+" Not found"); 67 | } 68 | } 69 | public void before(int a) 70 | { 71 | Node head1=head; 72 | int flag=0; 73 | while(head1!=null) 74 | { 75 | if(head1.data==a) 76 | { 77 | if(head1.prev==null) 78 | { 79 | System.out.println("No data found to delete"); 80 | return; 81 | } 82 | else 83 | { 84 | if(prev1.prev==null) 85 | { 86 | delete_atfirst(head) ; // head=head1; 87 | } 88 | else 89 | { 90 | 91 | temp=prev1.prev; 92 | head1.prev=temp; 93 | temp.next=head1; 94 | } 95 | } 96 | flag=1; 97 | } 98 | prev1=head1; 99 | head1=head1.next; 100 | } 101 | if(flag==0) 102 | { 103 | System.out.println(a+" Not Found in the list"); 104 | } 105 | } 106 | public void push(int data) 107 | { 108 | Node n=new Node(data); 109 | if(head==null) 110 | { 111 | head=n; 112 | prev1=n; 113 | 114 | } 115 | else 116 | { 117 | n.next=head; 118 | head.prev=n; 119 | head=n; 120 | } 121 | } 122 | public static void delete_atfirst(Node head1) 123 | { 124 | head1=head1.next; 125 | head1.prev=null; 126 | head=head1; 127 | } 128 | public static void delete_atlast(Node head1) 129 | { 130 | while(head1.next!=null) 131 | { 132 | head1=head1.next; 133 | } 134 | temp= head1.prev; 135 | temp.next=null; 136 | } 137 | public void delete(int a) 138 | { 139 | int flag=0; 140 | Node head1=head; 141 | while(head1!=null) 142 | { 143 | if(head1.data==a) 144 | { 145 | if(head1.prev==null) 146 | { 147 | delete_atfirst(head); 148 | } 149 | else if(head1.next==null) 150 | { 151 | delete_atlast(head); 152 | } 153 | else 154 | { 155 | temp=head1.prev; 156 | temp.next=head1.next; 157 | temp=head1.next; 158 | temp.prev=head1.prev; 159 | flag=1; 160 | } 161 | } 162 | head1=head1.next; 163 | } 164 | if(flag==0) 165 | { 166 | System.out.println("No data found"); 167 | } 168 | } 169 | public void Print(Node head) 170 | { 171 | if(head==null) 172 | { 173 | return; 174 | } 175 | System.out.print(head.data+"-"); 176 | Print(head.next); 177 | } 178 | public static void main(String arg[]) 179 | { 180 | delete b=new delete(); 181 | Scanner sc=new Scanner(System.in); 182 | int a=sc.nextInt(); 183 | while(a>0) 184 | { 185 | int data=sc.nextInt(); 186 | b.append_value(data); 187 | a--; 188 | } 189 | System.out.println("After append function"); 190 | b.Print(head); 191 | int push_data=sc.nextInt(); 192 | b.push(push_data); 193 | System.out.println("\npush "+push_data+" data in function"); 194 | b.Print(head); 195 | delete_atfirst(head); 196 | System.out.println("\n delete at first index function"); 197 | b.Print(head); 198 | delete_atlast(head); 199 | System.out.println("\n delete at last index function"); 200 | b.Print(head); 201 | 202 | int After_node_delete=sc.nextInt(); 203 | System.out.println("\nAfter delete at node whose data is "+After_node_delete+" function"); 204 | b.After(After_node_delete); 205 | 206 | b.Print(head); 207 | int before_node=sc.nextInt(); 208 | System.out.println("\nBefore delete at node whose data is "+before_node); 209 | b.before(before_node); 210 | b.Print(head); 211 | int node=sc.nextInt(); 212 | System.out.println("\ndelete a specific node "+node); 213 | b.delete(node); 214 | b.Print(head); 215 | 216 | 217 | 218 | 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /Data Structure/delete_circular.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class delete_circular 3 | { 4 | 5 | static Node head=null; 6 | static Node prev1; 7 | static Node temp; 8 | class Node 9 | { 10 | int data; 11 | Node next; 12 | Node(int d) 13 | { 14 | data = d; 15 | } 16 | } 17 | 18 | public void After(int a) 19 | { 20 | Node temp; 21 | Node head1=head; 22 | int flag=0; 23 | 24 | do 25 | { 26 | if(head1.data==a) 27 | { 28 | flag=1; 29 | if(head1.next==head) 30 | { 31 | System.out.println("No data found to delete"); 32 | return; 33 | } 34 | else 35 | { 36 | temp=head1.next; 37 | temp=temp.next; 38 | if(temp!=head) 39 | { 40 | head1.next=temp; 41 | } 42 | else 43 | { 44 | delete_atlast(head); 45 | } 46 | } 47 | } 48 | head1=head1.next; 49 | } 50 | while(head1!=head); 51 | if(flag==0) 52 | { 53 | System.out.println(a+" Not found"); 54 | } 55 | } 56 | 57 | public void before(int a) 58 | { 59 | Node head1=head; 60 | int flag=0; 61 | do 62 | { 63 | if(head1.data==a) 64 | { 65 | if(head1==head) 66 | { 67 | System.out.println("No data found to delete"); 68 | return; 69 | } 70 | else 71 | { 72 | if(prev1==head) 73 | { 74 | delete_atfirst(head) ; // head=head1; 75 | } 76 | else 77 | { 78 | 79 | temp=head1.next; 80 | prev1.next=temp; 81 | } 82 | } 83 | flag=1; 84 | } 85 | prev1=head1; 86 | head1=head1.next; 87 | } 88 | while(head1!=head); 89 | if(flag==0) 90 | { 91 | System.out.println(a+" Not Found in the list"); 92 | } 93 | } 94 | 95 | 96 | public void push(int data) 97 | { 98 | Node n=new Node(data); 99 | if(head==null) 100 | { 101 | head=n; 102 | n.next=head; 103 | prev1=n; 104 | } 105 | else 106 | { 107 | 108 | prev1.next=n; 109 | n.next=head; 110 | prev1=n; 111 | } 112 | 113 | 114 | } 115 | 116 | public static void delete_atfirst(Node head1) 117 | { 118 | while(head!=head1.next) 119 | { 120 | head1=head1.next; 121 | } 122 | head1.next=head.next; 123 | head=head.next; 124 | } 125 | 126 | 127 | public static void delete_atlast(Node head1) 128 | { 129 | while(head1.next!=head) 130 | { 131 | prev1=head1; 132 | head1=head1.next; 133 | } 134 | prev1.next=head; 135 | } 136 | 137 | public void delete(int a) 138 | { 139 | int flag=0; 140 | Node head1=head; 141 | do 142 | { 143 | if(head1.data==a) 144 | { 145 | if(head1==head) 146 | { 147 | delete_atfirst(head); 148 | flag=1; 149 | } 150 | else if(head1.next==head) 151 | { 152 | delete_atlast(head); 153 | flag=1; 154 | } 155 | else 156 | { 157 | prev1.next=head1.next; 158 | flag=1; 159 | } 160 | } 161 | prev1=head1; 162 | head1=head1.next; 163 | } 164 | while(head1!=head); 165 | if(flag==0) 166 | { 167 | System.out.println("No data found"); 168 | } 169 | } 170 | 171 | public void Print(Node head) 172 | { 173 | Node head1=head; 174 | System.out.print("Your list is : "); 175 | while(head!=head1.next) 176 | { 177 | System.out.print(head1.data+"-"); 178 | head1=head1.next; 179 | 180 | } 181 | System.out.println(head1.data+" "); 182 | } 183 | 184 | 185 | 186 | public static void main(String arg[]) 187 | { 188 | int push_data=0; 189 | delete_circular b=new delete_circular(); 190 | Scanner sc=new Scanner(System.in); 191 | int a=sc.nextInt(); 192 | while(a>0) 193 | { 194 | push_data=sc.nextInt(); 195 | b.push(push_data); 196 | a--; 197 | } 198 | 199 | b.Print(head); 200 | 201 | delete_atfirst(head); 202 | System.out.println("\ndelete at first index function"); 203 | b.Print(head); 204 | 205 | delete_atlast(head); 206 | System.out.println("\ndelete at last index function"); 207 | b.Print(head); 208 | 209 | int After_node_delete=sc.nextInt(); 210 | System.out.println("\nAfter delete at node whose data is "+After_node_delete+" function"); 211 | b.After(After_node_delete); 212 | b.Print(head); 213 | 214 | int before_node=sc.nextInt(); 215 | System.out.println("\nBefore delete at node whose data is "+before_node); 216 | b.before(before_node); 217 | b.Print(head); 218 | 219 | int node=sc.nextInt(); 220 | System.out.println("\ndelete a specific node "+node); 221 | b.delete(node); 222 | b.Print(head); 223 | 224 | 225 | 226 | 227 | 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /Data Structure/dequeu_linklist.java: -------------------------------------------------------------------------------- 1 | import java.util.* ; 2 | class dequeu_linklist 3 | { 4 | static class Node 5 | { 6 | int data; 7 | Node next; 8 | } 9 | static class Queue 10 | { 11 | Node front, rear; 12 | } 13 | static void enQueue_front(Queue q, int value) 14 | { 15 | Node temp = new Node(); 16 | temp.data=value; 17 | if(q.front==null) 18 | { 19 | q.front=temp; 20 | q.rear=temp; 21 | } 22 | else 23 | { 24 | temp.next=q.front; 25 | q.front=temp; 26 | } 27 | q.rear.next=q.front; 28 | } 29 | static void enQueue_rear(Queue q, int value) 30 | { 31 | Node temp = new Node(); 32 | temp.data=value; 33 | if(q.front==null) 34 | { 35 | q.front=temp; 36 | } 37 | else 38 | { 39 | q.rear.next=temp; 40 | } 41 | q.rear=temp; 42 | q.rear.next=q.front; 43 | } 44 | static int deQueue_rear(Queue q) 45 | { 46 | if (q .front == null) 47 | { 48 | System.out.printf ("Queue is empty"); 49 | return -1; 50 | } 51 | int value; 52 | if(q.rear==q.front) 53 | { 54 | value=q.rear.data; 55 | q.rear=null; 56 | q.front=null; 57 | } 58 | else 59 | { 60 | 61 | value=q.rear.data; 62 | Node temp1=q.front; 63 | Node prev1=null; 64 | while(temp1!=q.rear) 65 | { 66 | prev1=temp1; 67 | temp1=temp1.next; 68 | } 69 | prev1.next=q.front; 70 | q.rear=prev1; 71 | 72 | } 73 | return value ; 74 | } 75 | static int deQueue_front(Queue q) 76 | { 77 | if (q .front == null) 78 | { 79 | System.out.printf ("Queue is empty"); 80 | return -1; 81 | } 82 | int value; 83 | if(q.rear==q.front) 84 | { 85 | value=q.front.data; 86 | q.rear=null; 87 | q.front=null; 88 | } 89 | else 90 | { 91 | value=q.front.data; 92 | q.front=q.front.next; 93 | q.rear.next=q.front; 94 | } 95 | return value ; 96 | } 97 | static void displayQueue( Queue q) 98 | { 99 | Node temp = q .front; 100 | System.out.printf("Elements in Circular Queue are: "); 101 | while (temp .next != q .front) 102 | { 103 | System.out.printf("%d ", temp .data); 104 | temp = temp .next; 105 | } 106 | System.out.println(temp.data); 107 | } 108 | public static void main(String args[]) 109 | { 110 | Queue q = new Queue(); 111 | q .front = q .rear = null; 112 | enQueue_rear(q, 14); 113 | enQueue_rear(q, 22); 114 | enQueue_rear(q, 6); 115 | displayQueue(q); 116 | System.out.println("Deleted value ="+deQueue_front(q)); 117 | System.out.println("Deleted value ="+deQueue_front(q)); 118 | displayQueue(q); 119 | enQueue_rear(q, 9); 120 | enQueue_rear(q, 20); 121 | displayQueue(q); 122 | enQueue_front(q,100); 123 | displayQueue(q); 124 | System.out.println("Deleted value ="+deQueue_rear(q)); 125 | displayQueue(q); 126 | 127 | } 128 | } -------------------------------------------------------------------------------- /Data Structure/dequeue.txt: -------------------------------------------------------------------------------- 1 | // Java implementation of De-queue using circular 2 | // array 3 | 4 | // A structure to represent a Deque 5 | class Deque 6 | { 7 | static final int MAX = 100; 8 | int arr[]; 9 | int front; 10 | int rear; 11 | int size; 12 | 13 | public Deque(int size) 14 | { 15 | arr = new int[MAX]; 16 | front = -1; 17 | rear = -1; 18 | this.size = size; 19 | } 20 | 21 | /*// Operations on Deque: 22 | void insertfront(int key); 23 | void insertrear(int key); 24 | void deletefront(); 25 | void deleterear(); 26 | bool isFull(); 27 | bool isEmpty(); 28 | int getFront(); 29 | int getRear();*/ 30 | 31 | // Checks whether Deque is full or not. 32 | boolean isFull() 33 | { 34 | return ((front == 0 && rear == size-1)|| 35 | front > rear); 36 | } 37 | 38 | // Checks whether Deque is empty or not. 39 | boolean isEmpty () 40 | { 41 | return (front == -1); 42 | } 43 | 44 | // Inserts an element at front 45 | void insertfront(int key) 46 | { 47 | // check whether Deque if full or not 48 | if (isFull()) 49 | { 50 | System.out.println("Overflow"); 51 | return; 52 | } 53 | 54 | // If queue is initially empty 55 | if (front == -1) 56 | { 57 | front = 0; 58 | rear = 0; 59 | } 60 | 61 | // front is at first position of queue 62 | else if (front == 0) 63 | front = size - 1 ; 64 | 65 | else // decrement front end by '1' 66 | front = front-1; 67 | 68 | // insert current element into Deque 69 | arr[front] = key ; 70 | } 71 | 72 | // function to inset element at rear end 73 | // of Deque. 74 | void insertrear(int key) 75 | { 76 | if (isFull()) 77 | { 78 | System.out.println(" Overflow "); 79 | return; 80 | } 81 | 82 | // If queue is initially empty 83 | if (front == -1) 84 | { 85 | front = 0; 86 | rear = 0; 87 | } 88 | 89 | // rear is at last position of queue 90 | else if (rear == size-1) 91 | rear = 0; 92 | 93 | // increment rear end by '1' 94 | else 95 | rear = rear+1; 96 | 97 | // insert current element into Deque 98 | arr[rear] = key ; 99 | } 100 | 101 | // Deletes element at front end of Deque 102 | void deletefront() 103 | { 104 | // check whether Deque is empty or not 105 | if (isEmpty()) 106 | { 107 | System.out.println("Queue Underflow\n"); 108 | return ; 109 | } 110 | 111 | // Deque has only one element 112 | if (front == rear) 113 | { 114 | front = -1; 115 | rear = -1; 116 | } 117 | else 118 | // back to initial position 119 | if (front == size -1) 120 | front = 0; 121 | 122 | else // increment front by '1' to remove current 123 | // front value from Deque 124 | front = front+1; 125 | } 126 | 127 | // Delete element at rear end of Deque 128 | void deleterear() 129 | { 130 | if (isEmpty()) 131 | { 132 | System.out.println(" Underflow"); 133 | return ; 134 | } 135 | 136 | // Deque has only one element 137 | if (front == rear) 138 | { 139 | front = -1; 140 | rear = -1; 141 | } 142 | else if (rear == 0) 143 | rear = size-1; 144 | else 145 | rear = rear-1; 146 | } 147 | 148 | // Returns front element of Deque 149 | int getFront() 150 | { 151 | // check whether Deque is empty or not 152 | if (isEmpty()) 153 | { 154 | System.out.println(" Underflow"); 155 | return -1 ; 156 | } 157 | return arr[front]; 158 | } 159 | 160 | // function return rear element of Deque 161 | int getRear() 162 | { 163 | // check whether Deque is empty or not 164 | if(isEmpty() || rear < 0) 165 | { 166 | System.out.println(" Underflow\n"); 167 | return -1 ; 168 | } 169 | return arr[rear]; 170 | } 171 | 172 | // Driver program to test above function 173 | public static void main(String[] args) 174 | { 175 | 176 | Deque dq = new Deque(5); 177 | 178 | System.out.println("Insert element at rear end : 5 "); 179 | dq.insertrear(5); 180 | 181 | System.out.println("insert element at rear end : 10 "); 182 | dq.insertrear(10); 183 | 184 | System.out.println("get rear element : "+ dq.getRear()); 185 | 186 | dq.deleterear(); 187 | System.out.println("After delete rear element new rear become : " + 188 | dq.getRear()); 189 | 190 | System.out.println("inserting element at front end"); 191 | dq.insertfront(15); 192 | 193 | System.out.println("get front element: " +dq.getFront()); 194 | 195 | dq.deletefront(); 196 | 197 | System.out.println("After delete front element new front become : " + 198 | + dq.getFront()); 199 | 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /Data Structure/from_left.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class QuickSort_array 3 | { 4 | int divide(int arr[], int left, int right) 5 | { 6 | int pivit=arr[left]; 7 | int i=left; 8 | for(int j=left+1;j<=right;j++) 9 | { 10 | if(arr[j]<=pivit) 11 | { 12 | i++; 13 | int temp=arr[i]; 14 | arr[j]=arr[i]; 15 | arr[i]=temp; 16 | } 17 | } 18 | int temp=arr[i]; 19 | arr[i]=arr[left]; 20 | arr[left]=temp; 21 | return i; 22 | } 23 | void sort(int arr[], int left, int right) 24 | { 25 | if (left < right) 26 | { 27 | int x = divide(arr, left, right); 28 | sort(arr, left, x-1); 29 | sort(arr, x+1, right); 30 | } 31 | } 32 | void printArray(int arr[]) 33 | { 34 | int n = arr.length; 35 | for (int i=0; i=0;j--) 19 | { 20 | int data=arr[temp]; 21 | if(arr[j]>data) 22 | { 23 | int m=arr[temp]; 24 | arr[temp]=arr[j]; 25 | arr[j]=m; 26 | temp=j; 27 | } 28 | else 29 | { 30 | break; 31 | } 32 | } 33 | } 34 | for(int i=0;i0) 156 | { 157 | int data=sc.nextInt(); 158 | b.append_value(data); 159 | a--; 160 | } 161 | System.out.println("After append function"); 162 | b.Print(head); 163 | int push_data=sc.nextInt(); 164 | b.push(push_data); 165 | System.out.println("\nAfter push "+push_data+" data in function"); 166 | b.Print(head); 167 | 168 | int After_node=sc.nextInt(); 169 | int insert=sc.nextInt(); 170 | b.After(After_node,insert); 171 | System.out.println("\nAfter insertion at node "+After_node+" function"); 172 | b.Print(head); 173 | 174 | int before_node=sc.nextInt(); 175 | insert=sc.nextInt(); 176 | b.before(before_node,insert); 177 | System.out.println("\nBefore insertion at node "+before_node+" function"); 178 | b.Print(head); 179 | 180 | 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /Data Structure/k.txt: -------------------------------------------------------------------------------- 1 | /* class LinkList 2 | { 3 | int data; 4 | LinkList next; 5 | LinkList prev; 6 | } */ 7 | 8 | static LinkList rotateByK(LinkList head, int k) 9 | { 10 | LinkList head1=head; 11 | LinkList prev1=null; 12 | LinkList temp=null; 13 | while(head1!=null) 14 | { 15 | if(head1.data==k) 16 | { 17 | temp=head1; 18 | head1.prev.next=null; 19 | head1.prev=null; 20 | } 21 | prev1=head1; 22 | head1=head1.next; 23 | } 24 | prev1.next=head; 25 | head.prev=prev1; 26 | head=temp1; 27 | } -------------------------------------------------------------------------------- /Data Structure/leftView.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node left; 6 | Node right; 7 | Node(int data) 8 | { 9 | this.data=data; 10 | } 11 | 12 | } 13 | class leftView 14 | { 15 | static Node root; 16 | static int count=0; 17 | public static Node insert(int arr[],Node root,int i) 18 | { 19 | if(i a=new ArrayList<>(); 33 | if(root==null) 34 | { 35 | return ; 36 | } 37 | else 38 | { 39 | temp(root,1,a); 40 | } 41 | for(int i=0;i a) 47 | { 48 | if(level>count) 49 | { 50 | count=level; 51 | a.add(root.data); 52 | } 53 | if(root.left!=null) 54 | { 55 | temp(root.left,level+1,a); 56 | } 57 | if(root.right!=null) 58 | { 59 | temp(root.right,level+1,a); 60 | } 61 | } 62 | public static void main(String args[]) 63 | { 64 | Scanner sc=new Scanner(System.in); 65 | int size=sc.nextInt(); 66 | int arr[]=new int[size]; 67 | for(int i=0;i queue=new LinkedList<>(); 19 | queue.add(root); 20 | while(queue.size()>0) 21 | { 22 | Node temp=queue.poll(); 23 | System.out.println(temp.data); 24 | if(temp.left!=null) 25 | { 26 | queue.add(temp.left); 27 | } 28 | if(temp.right!=null) 29 | { 30 | queue.add(temp.right); 31 | } 32 | } 33 | } 34 | //function for insertion 35 | public Node insert(Node root,int arr[],int i) 36 | { 37 | if(ik1.data) 29 | { 30 | temp=k.data; 31 | k.data=k1.data; 32 | k1.data=temp; 33 | } 34 | } 35 | } 36 | 37 | } 38 | 39 | 40 | void push(int data) 41 | { 42 | 43 | Node k=new Node(data); 44 | 45 | if(head==null) 46 | { 47 | prev1=k; 48 | head=k; 49 | } 50 | else 51 | { 52 | prev1.next=k; 53 | k.prev=prev1; 54 | prev1=k; 55 | } 56 | } 57 | void printList(Node node) 58 | { 59 | while (node != null) { 60 | System.out.println(node.data + " "); 61 | node = node.next; 62 | } 63 | } 64 | 65 | public static void main(String[] args) { 66 | linear_sort list = new linear_sort(); 67 | 68 | 69 | list.push(5); 70 | list.push(4); 71 | list.push(3); 72 | list.push(2); 73 | list.push(1); 74 | 75 | 76 | list.sort(head); 77 | list.printList(head); 78 | 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /Data Structure/max_depth.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | class Node 4 | { 5 | int data; 6 | Node left; 7 | Node right; 8 | Node(int data) 9 | { 10 | this.data=data; 11 | } 12 | 13 | } 14 | class BFS 15 | { 16 | static Node root; 17 | public static Node insert(int arr[],Node root,int i) 18 | { 19 | if(i0) 34 | { 35 | Node head = null; 36 | n = Integer.parseInt(s.nextLine()); 37 | while(n > 0) 38 | { 39 | m = Integer.parseInt(s.nextLine()); 40 | head = insertEnd(head, m); 41 | n--; 42 | } 43 | 44 | System.out.print("\n"); 45 | Average(head); 46 | System.out.print("\n"); 47 | t--; 48 | } 49 | } 50 | static void Average(Node head) 51 | { 52 | int count=0; 53 | Node head1=head; 54 | Node head2=head; 55 | Node prev=null; 56 | int sum=0; 57 | int index=0; 58 | while(head1!=null) 59 | { 60 | count++; 61 | head1=head1.next; 62 | } 63 | 64 | if(count%2==0) 65 | { 66 | index=count/2; 67 | } 68 | else 69 | { 70 | if(count%2!=0) 71 | { 72 | index=(count/2)+1; 73 | } 74 | } 75 | for(int i=1;i=0;i--) 75 | { 76 | d=exp.charAt(i); 77 | if (Character.isLetterOrDigit(d)) 78 | { 79 | result += d; 80 | } 81 | else if(d==')') 82 | { 83 | s.push(d); 84 | } 85 | else if(d=='(') 86 | { 87 | while(!s.isEmpty()&&s.stackArray[s.top]!=')') 88 | result += (char)s.pop(); 89 | s.pop(); 90 | } 91 | else 92 | { 93 | while (!s.isEmpty() && Prec(d) <= Prec((char)s.stackArray[s.top])) 94 | { 95 | result +=(char) s.pop(); 96 | } 97 | s.push(d); 98 | } 99 | } 100 | while(!s.isEmpty()) 101 | { 102 | result+=(char)s.pop(); 103 | } 104 | char ch1[]=result.toCharArray(); 105 | rev=""; 106 | for(int i=ch1.length-1;i>=0;i--) 107 | { 108 | 109 | rev+=ch1[i]; 110 | 111 | } 112 | result=""; 113 | result=rev; 114 | return result; 115 | } 116 | public static void main(String[] args) 117 | { 118 | CQStack theStack = new CQStack(100); // make new stack 119 | Scanner s=new Scanner(System.in); 120 | int t, n, q1, q2; 121 | String st; 122 | t = Integer.parseInt(s.nextLine()); 123 | while(t>0) 124 | { 125 | st = s.nextLine(); 126 | st = infixToPostfix(theStack, st); 127 | System.out.println(st); 128 | t--; 129 | } 130 | } 131 | } -------------------------------------------------------------------------------- /Data Structure/prefix1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class CQStack 4 | { 5 | public int maxSize; // size of stack array 6 | public int[] stackArray; 7 | public int top; // top of stack 8 | 9 | public CQStack(int s) // constructor 10 | { 11 | maxSize = s; // set array size 12 | stackArray = new int[maxSize]; // create array 13 | top = -1; // no items yet 14 | } 15 | public boolean isEmpty() // true if stack is empty 16 | { 17 | return (top == -1); 18 | } 19 | public boolean isFull() // true if stack is full 20 | { 21 | return (top == maxSize-1); 22 | } 23 | 24 | public void push(int j) // put item on top of stack 25 | { 26 | if(isFull()) 27 | { 28 | } 29 | else 30 | { 31 | stackArray[++top] = j; // increment top, insert item 32 | } 33 | } 34 | public int pop() // take item from top of stack 35 | { 36 | if (isEmpty()) 37 | { 38 | return -1; 39 | } 40 | else 41 | { 42 | int temp=stackArray[top--]; 43 | return temp; // access item, decrement top 44 | } 45 | } 46 | } 47 | 48 | class prefix1 49 | { 50 | static String result = new String(""); 51 | static int Prec(char ch) 52 | { 53 | switch (ch) 54 | { 55 | case '+': 56 | case '-': 57 | return 1; 58 | 59 | case '*': 60 | case '/': 61 | return 2; 62 | 63 | case '^': 64 | return 3; 65 | } 66 | return -1; 67 | } 68 | static String infixToPostfix(CQStack s, String exp) 69 | { 70 | char ch[]=exp.toCharArray(); 71 | String rev=""; 72 | for(int i=ch.length-1;i>=0;i--) 73 | { 74 | if(ch[i]==')') 75 | { 76 | rev+='('; 77 | } 78 | else if(ch[i]=='(') 79 | { 80 | rev+=')'; 81 | } 82 | else 83 | { 84 | rev+=ch[i]; 85 | } 86 | 87 | } 88 | exp=rev; 89 | result=""; 90 | char d; 91 | int n=exp.length(); 92 | for(int i=0;i=0;i--) 125 | { 126 | 127 | rev+=ch1[i]; 128 | 129 | } 130 | result=""; 131 | result=rev; 132 | return result; 133 | } 134 | public static void main(String[] args) 135 | { 136 | CQStack theStack = new CQStack(100); // make new stack 137 | Scanner s=new Scanner(System.in); 138 | int t, n, q1, q2; 139 | String st; 140 | t = Integer.parseInt(s.nextLine()); 141 | while(t>0) 142 | { 143 | st = s.nextLine(); 144 | st = infixToPostfix(theStack, st); 145 | System.out.println(st); 146 | t--; 147 | } 148 | } 149 | } -------------------------------------------------------------------------------- /Data Structure/print.txt: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | // Other imports go here 3 | // Do NOT change the class name 4 | class Node 5 | { 6 | int data; 7 | Node next; 8 | Node(int d) 9 | { 10 | data=d; 11 | } 12 | } 13 | 14 | class Main 15 | { 16 | static Node insertEnd(Node head, int data) 17 | { 18 | Node newLink = new Node(data); 19 | Node last = head; 20 | newLink.next = null; // link new node to NULL as it is last node 21 | if (head == null) // if list is empty add in beginning. 22 | { 23 | head = newLink; 24 | return head; 25 | } 26 | while (last.next != null) // Find the last node 27 | last = last.next; 28 | last.next = newLink; // Add the node after the last node of list 29 | return head; 30 | } 31 | static void forwardPrint(Node head) 32 | { 33 | Node a=head; 34 | while(a!=null) 35 | { 36 | System.out.print(a.data+"-"); 37 | a=a.next; 38 | } 39 | } 40 | 41 | static void backwardPrint(Node head) 42 | { 43 | Node a=head; 44 | if(a.next!=null) 45 | { 46 | backwardPrint(a.next); 47 | } 48 | 49 | System.out.print(a.data+"-"); 50 | } 51 | public static void main(String[] args) 52 | { 53 | int t,n,m; 54 | Scanner s = new Scanner(System.in); 55 | t=Integer.parseInt(s.nextLine()); 56 | while(t>0) 57 | { 58 | Node head = null; 59 | n = Integer.parseInt(s.nextLine()); 60 | while(n > 0) 61 | { 62 | m = Integer.parseInt(s.nextLine()); 63 | head = insertEnd(head, m); 64 | n--; 65 | } 66 | forwardPrint(head); 67 | System.out.print("\n"); 68 | backwardPrint(head); 69 | System.out.print("\n"); 70 | t--; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /Data Structure/priority.java: -------------------------------------------------------------------------------- 1 | class CQPriorityQ 2 | { 3 | 4 | private int maxSize; 5 | private int[] queArray; 6 | private int nItems; 7 | 8 | public CQPriorityQ(int s) 9 | { 10 | maxSize = s; 11 | queArray = new int[maxSize]; 12 | nItems = 0; 13 | } 14 | 15 | public void insert(int item) 16 | { 17 | int j; 18 | if(nItems==0) // if no items, 19 | queArray[nItems++] = item; //----------------insert at 0 20 | else 21 | { 22 | for(j=nItems-1; j>=0; j--) 23 | { 24 | if( item > queArray[j] ) 25 | { 26 | 27 | queArray[j+1] = queArray[j]; 28 | } 29 | else 30 | { 31 | break; 32 | } 33 | } 34 | queArray[j+1] = item; // insert it 35 | nItems++; 36 | } 37 | } 38 | 39 | public int remove() //-----------------remove minimum item 40 | { return queArray[--nItems]; } 41 | 42 | public int peekMin() //--------------- peek at minimum item 43 | { 44 | return queArray[nItems-1]; 45 | } 46 | 47 | public boolean isEmpty() //------------ true if queue is empty 48 | { 49 | return (nItems==0); 50 | } 51 | 52 | public boolean isFull() //--------------- true if queue is full 53 | { 54 | return (nItems == maxSize); 55 | } 56 | 57 | } 58 | 59 | class priority 60 | { 61 | public static void main(String[] args) 62 | { 63 | CQPriorityQ thePQ = new CQPriorityQ(5); 64 | thePQ.insert(30); 65 | thePQ.insert(50); 66 | thePQ.insert(10); 67 | thePQ.insert(40); 68 | thePQ.insert(20); 69 | 70 | 71 | while( !thePQ.isEmpty() ) 72 | { 73 | int item = thePQ.remove(); 74 | System.out.print(item + " "); 75 | } 76 | System.out.println(""); 77 | } 78 | } -------------------------------------------------------------------------------- /Data Structure/reverse.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class reverse { 4 | Node prev1; 5 | static Node head; 6 | 7 | static class Node { 8 | 9 | int data; 10 | Node next, prev; 11 | 12 | Node(int d) { 13 | data = d; 14 | next = prev = null; 15 | } 16 | } 17 | 18 | void reverse() { 19 | Node t=head; 20 | Node prev1=null; 21 | while(t!=null) 22 | { 23 | prev1=t; 24 | t=t.next; 25 | } 26 | head=prev1; 27 | t=prev1; 28 | Node o; 29 | Node q; 30 | while(t!=null) 31 | { 32 | o=t.prev; 33 | q=t.next; 34 | t.next=t.prev; 35 | t.prev=q; 36 | t=o; 37 | } 38 | 39 | 40 | } 41 | 42 | void push(int data) 43 | { 44 | 45 | Node k=new Node(data); 46 | 47 | if(head==null) 48 | { 49 | prev1=k; 50 | head=k; 51 | } 52 | else 53 | { 54 | prev1.next=k; 55 | k.prev=prev1; 56 | prev1=k; 57 | } 58 | } 59 | void printList(Node node) 60 | { 61 | while (node != null) { 62 | System.out.print(node.data + " "); 63 | node = node.next; 64 | } 65 | } 66 | 67 | public static void main(String[] args) { 68 | reverse list = new reverse(); 69 | 70 | 71 | list.push(2); 72 | list.push(4); 73 | list.push(8); 74 | list.push(10); 75 | 76 | System.out.println("Original linked list "); 77 | list.printList(head); 78 | 79 | list.reverse(); 80 | System.out.println(""); 81 | System.out.println("The reversed Linked List is "); 82 | list.printList(head); 83 | } 84 | } 85 | 86 | -------------------------------------------------------------------------------- /Data Structure/reverse_circular.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class reverse_circular { 4 | Node prev1; 5 | static Node head; 6 | 7 | static class Node { 8 | 9 | int data; 10 | Node next; 11 | 12 | Node(int d) { 13 | data = d; 14 | } 15 | } 16 | // Same like reverse in singly we only add last node.next=null to address of head; 17 | void reverse() 18 | { 19 | 20 | Node t=head; 21 | Node prev1=null; 22 | Node temp; 23 | do{ 24 | temp=t.next; 25 | t.next=prev1; 26 | prev1=t; 27 | t=temp; 28 | } 29 | while(t!=head); 30 | head.next=prev1; 31 | head=prev1; 32 | 33 | 34 | } 35 | 36 | void push(int data) 37 | { 38 | 39 | Node n=new Node(data); 40 | if(head==null) 41 | { 42 | head=n; 43 | n.next=head; 44 | prev1=n; 45 | } 46 | else 47 | { 48 | 49 | prev1.next=n; 50 | n.next=head; 51 | prev1=n; 52 | } 53 | } 54 | 55 | public void Print(Node head) 56 | { 57 | Node head1=head; 58 | System.out.print("Your list is : "); 59 | while(head!=head1.next) 60 | { 61 | System.out.print(head1.data+"-"); 62 | head1=head1.next; 63 | 64 | } 65 | System.out.println(head1.data+" "); 66 | } 67 | 68 | public static void main(String[] args) { 69 | reverse_circular list = new reverse_circular(); 70 | 71 | 72 | list.push(2); 73 | list.push(4); 74 | list.push(8); 75 | list.push(10); 76 | 77 | System.out.println("Original linked list "); 78 | list.Print(head); 79 | 80 | list.reverse(); 81 | System.out.println(""); 82 | System.out.println("The reversed Linked List is "); 83 | list.Print(head); 84 | } 85 | } 86 | 87 | -------------------------------------------------------------------------------- /Data Structure/reverseprint.txt: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | // Other imports go here 3 | // Do NOT change the class name 4 | class Node 5 | { 6 | int data; 7 | Node next; 8 | Node(int d) 9 | { 10 | data=d; 11 | } 12 | } 13 | 14 | class Main 15 | { 16 | static Node insertEnd(Node head, int data) 17 | { 18 | Node newLink = new Node(data); 19 | Node last = head; 20 | newLink.next = null; // link new node to NULL as it is last node 21 | if (head == null) // if list is empty add in beginning. 22 | { 23 | head = newLink; 24 | return head; 25 | } 26 | while (last.next != null) // Find the last node 27 | last = last.next; 28 | last.next = newLink; // Add the node after the last node of list 29 | return head; 30 | } 31 | 32 | static void forwardPrint(Node head) 33 | { 34 | if(head==null) 35 | { 36 | return; 37 | } 38 | System.out.print(head.data+"-"); 39 | forwardPrint(head.next); 40 | } 41 | 42 | static void reverse(Node head) 43 | { 44 | if(head==null) 45 | { 46 | return; 47 | } 48 | reverse(head.next); 49 | System.out.print(head.data+"-"); 50 | } 51 | public static void main(String[] args) 52 | { 53 | int t,n,m; 54 | Scanner s = new Scanner(System.in); 55 | t=Integer.parseInt(s.nextLine()); 56 | while(t>0) 57 | { 58 | Node head = null; 59 | n = Integer.parseInt(s.nextLine()); 60 | while(n > 0) 61 | { 62 | m = Integer.parseInt(s.nextLine()); 63 | head = insertEnd(head, m); 64 | n--; 65 | } 66 | forwardPrint(head); 67 | System.out.print("\n"); 68 | reverse(head); 69 | System.out.print("\n"); 70 | t--; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /Data Structure/rightview.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node left; 6 | Node right; 7 | Node(int data) 8 | { 9 | this.data=data; 10 | } 11 | 12 | } 13 | class rightView 14 | { 15 | static Node root; 16 | static int count=0; 17 | public static Node insert(int arr[],Node root,int i) 18 | { 19 | if(i a=new ArrayList<>(); 33 | if(root==null) 34 | { 35 | return ; 36 | } 37 | else 38 | { 39 | temp(root,1,a); 40 | } 41 | for(int i=0;i a) 47 | { 48 | if(level>count) 49 | { 50 | count=level; 51 | a.add(root.data); 52 | } 53 | 54 | if(root.right!=null) 55 | { 56 | temp(root.right,level+1,a); 57 | } 58 | if(root.left!=null) 59 | { 60 | temp(root.left,level+1,a); 61 | } 62 | } 63 | public static void main(String args[]) 64 | { 65 | Scanner sc=new Scanner(System.in); 66 | int size=sc.nextInt(); 67 | int arr[]=new int[size]; 68 | for(int i=0;i0) 75 | { 76 | Node head = null; 77 | n = Integer.parseInt(s.nextLine()); 78 | while(n > 0) 79 | { 80 | m = Integer.parseInt(s.nextLine()); 81 | head = insertEnd(head, m); 82 | n--; 83 | } 84 | int element = Integer.parseInt(s.nextLine()); 85 | forwardPrint(head); 86 | System.out.print("\n"); 87 | shift_element(head,element); 88 | System.out.print("\n"); 89 | t--; 90 | } 91 | } 92 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures 2 | This is my Data Structure code using Java Language 3 | --------------------------------------------------------------------------------