├── Adding two array ├── Deleting Node in Linked List ├── Hashmap ├── Number of Good Pairs.c++ ├── Polymorphism ├── README.md ├── add_elements_in_array ├── binary_tree_traversal ├── deletion_in_linkedlist ├── merge_two_linked_list └── reverse a queue /Adding two array: -------------------------------------------------------------------------------- 1 | #include 2 | #define ARRAY_SIZE 10 3 | 4 | int main() { 5 | int i; 6 | int largeArray[ARRAY_SIZE]; 7 | 8 | // Populate the array with values 9 | for (i = 0; i < ARRAY_SIZE; i++) { 10 | largeArray[i] = i; 11 | } 12 | 13 | // Print the elements of the array 14 | for (i = 0; i < ARRAY_SIZE; i++) { 15 | printf("largeArray[%d] = %d\n", i, largeArray[i]); 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Deleting Node in Linked List: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int number; 6 | Node* next; 7 | }; 8 | 9 | void Push(Node** head, int A) 10 | { 11 | Node* n = (Node*)malloc(sizeof(Node)); 12 | n->number = A; 13 | n->next = *head; 14 | *head = n; 15 | } 16 | 17 | void deleteN(Node** head, int position) 18 | { 19 | Node* temp; 20 | Node* prev; 21 | temp = *head; 22 | prev = *head; 23 | for (int i = 0; i < position; i++) { 24 | if (i == 0 && position == 1) { 25 | *head = (*head)->next; 26 | free(temp); 27 | } 28 | else { 29 | if (i == position - 1 && temp) { 30 | prev->next = temp->next; 31 | free(temp); 32 | } 33 | else { 34 | prev = temp; 35 | 36 | // Position was greater than 37 | // number of nodes in the list 38 | if (prev == NULL) 39 | break; 40 | temp = temp->next; 41 | } 42 | } 43 | } 44 | } 45 | 46 | void printList(Node* head) 47 | { 48 | while (head) { 49 | if (head->next == NULL) 50 | cout << "[" << head->number << "] " 51 | << "[" << head << "]->" 52 | << "(nil)" << endl; 53 | else 54 | cout << "[" << head->number << "] " 55 | << "[" << head << "]->" << head->next 56 | << endl; 57 | head = head->next; 58 | } 59 | cout << endl << endl; 60 | } 61 | 62 | // Driver code 63 | int main() 64 | { 65 | Node* list = (Node*)malloc(sizeof(Node)); 66 | list->next = NULL; 67 | Push(&list, 1); 68 | Push(&list, 2); 69 | Push(&list, 3); 70 | 71 | printList(list); 72 | 73 | // Delete any position from list 74 | deleteN(&list, 1); 75 | printList(list); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Hashmap: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | //////////////////naveen hashmap 4 | class HashMap3{ 5 | public static void main(String args[]){ 6 | HashMap hm=new HashMap(); 7 | hm.put(100,"Amit"); 8 | hm.put(101,"Vijay"); 9 | hm.put(102,"Rahul"); 10 | System.out.println("Initial list of elements:"); 11 | for(Map.Entry m:hm.entrySet()) 12 | { 13 | System.out.println(m.getKey()+" "+m.getValue()); 14 | } 15 | System.out.println("Updated list of elements:"); 16 | hm.replace(102, "Gaurav"); 17 | for(Map.Entry m:hm.entrySet()) 18 | { 19 | System.out.println(m.getKey()+" "+m.getValue()); 20 | } 21 | System.out.println("Updated list of elements:"); 22 | hm.replace(101, "Vijay", "Ravi"); 23 | for(Map.Entry m:hm.entrySet()) 24 | { 25 | System.out.println(m.getKey()+" "+m.getValue()); 26 | } 27 | System.out.println("Updated list of elements:"); 28 | hm.replaceAll((k,v) -> "Ajay"); 29 | for(Map.Entry m:hm.entrySet()) 30 | { 31 | System.out.println(m.getKey()+" "+m.getValue()); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Number of Good Pairs.c++: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | int res=0; 5 | for(int i=0;i" + "(null)"); // Print the node value and null 46 | } else { 47 | System.out.println("[" + head.number + "] [" + head + "]->" + head.next); // Print the node value and the next node 48 | } 49 | head = head.next; // Move to the next node 50 | } 51 | System.out.println(); 52 | System.out.println(); 53 | } 54 | 55 | public static void main(String[] args) { 56 | Node list = new Node(); // Create a new linked list 57 | list.next = null; // Set the next node of the first node to be null 58 | list = push(list, 1); // Add node with value 1 to the linked list 59 | list = push(list, 2); // Add node with value 2 to the linked list 60 | list = push(list, 3); // Add node with value 3 to the linked list 61 | 62 | printList(list); // Print the linked list 63 | 64 | // Delete node at position 1 from the linked list 65 | list = deleteN(list, 1); 66 | printList(list); // Print the updated linked list 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /merge_two_linked_list: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.List; 4 | 5 | // link list node 6 | class Node { 7 | int key; 8 | Node next; 9 | 10 | public Node(int key) { 11 | this.key = key; 12 | next = null; 13 | } 14 | } 15 | 16 | public class Main { 17 | // return a newnode 18 | public static Node newNode(int key) { 19 | return new Node(key); 20 | } 21 | 22 | // driver code 23 | public static void main(String[] args) { 24 | // let us create two sorted linked lists to test the above 25 | // function. Created lists shall be 26 | // a: 5->10->15->40 27 | // b: 2->3->20 28 | Node a = new Node(5); 29 | a.next = new Node(10); 30 | a.next.next = new Node(15); 31 | a.next.next.next = new Node(40); 32 | 33 | Node b = new Node(2); 34 | b.next = new Node(3); 35 | b.next.next = new Node(20); 36 | 37 | List v = new ArrayList<>(); 38 | while (a != null) { 39 | v.add(a.key); 40 | a = a.next; 41 | } 42 | 43 | while (b != null) { 44 | v.add(b.key); 45 | b = b.next; 46 | } 47 | 48 | Collections.sort(v); 49 | Node result = new Node(-1); 50 | Node temp = result; 51 | for (int i = 0; i < v.size(); i++) { 52 | result.next = new Node(v.get(i)); 53 | result = result.next; 54 | } 55 | 56 | temp = temp.next; 57 | System.out.print("Resultant Merge Linked List is : "); 58 | while (temp != null) { 59 | System.out.print(temp.key + " "); 60 | temp = temp.next; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /reverse a queue: -------------------------------------------------------------------------------- 1 | //using stack 2 | import java.util.*; 3 | class Queue 4 | { 5 | int front, rear, size; 6 | int capacity; 7 | int[] array; 8 | 9 | public Queue (int capacity) 10 | { 11 | this.capacity = capacity; 12 | front = this.size = 0; 13 | rear = capacity - 1; 14 | array = new int[this.capacity]; 15 | } 16 | 17 | // Queue is full when size becomes 18 | // equal to the capacity 19 | boolean isFull (Queue queue) 20 | { 21 | return (queue.size == queue.capacity); 22 | } 23 | 24 | // Queue is empty when size is 0 25 | boolean isEmpty (Queue queue) 26 | { 27 | return (queue.size == 0); 28 | } 29 | 30 | // Method to add an item to the queue. 31 | // It changes rear and size 32 | void enqueue (int item) 33 | { 34 | if (isFull (this)) 35 | return; 36 | this.rear = (this.rear + 1) % this.capacity; 37 | this.array[this.rear] = item; 38 | this.size = this.size + 1; 39 | 40 | } 41 | int dequeue () 42 | { 43 | if (isEmpty (this)) 44 | return Integer.MIN_VALUE; 45 | 46 | int item = this.array[this.front]; 47 | this.front = (this.front + 1) % this.capacity; 48 | this.size = this.size - 1; 49 | return item; 50 | } 51 | 52 | 53 | 54 | public Queue queuereverse (Queue q) 55 | { 56 | Stack < Integer > stk = new Stack <> (); 57 | while (!q.isEmpty (q)) 58 | { 59 | stk.add (q.array[front]); 60 | q.dequeue (); 61 | } 62 | while (!stk.isEmpty ()) 63 | { 64 | q.enqueue (stk.peek ()); 65 | stk.pop (); 66 | } 67 | return q; 68 | } 69 | 70 | public void disp () /* function to display the elements of the queue */ 71 | { 72 | 73 | if (front == -1) 74 | { 75 | System.out.print ("\nQueue is Empty"); 76 | } 77 | else 78 | { 79 | for (int i = front; i <= rear; i++) 80 | { 81 | System.out.print (array[i] + " "); 82 | } 83 | } 84 | System.out.println (); 85 | } 86 | 87 | } 88 | 89 | // Driver class 90 | public class Main 91 | { 92 | public static void main (String[]args) 93 | { 94 | Queue queue = new Queue (1000); 95 | 96 | queue.enqueue (10); 97 | queue.enqueue (20); 98 | queue.enqueue (30); 99 | queue.enqueue (40); 100 | 101 | System.out.println ("Original queue : "); 102 | queue.disp (); 103 | System.out.println (); 104 | 105 | queue = queue.queuereverse (queue); 106 | 107 | System.out.println ("Queue after reversing : "); 108 | queue.disp (); 109 | 110 | System.out.println (); 111 | 112 | 113 | } 114 | } 115 | 116 | //using recursion 117 | import java.util.*; 118 | class Queue 119 | { 120 | int front, rear, size; 121 | int capacity; 122 | int array[]; 123 | 124 | public Queue (int capacity) 125 | { 126 | this.capacity = capacity; 127 | front = this.size = 0; 128 | rear = capacity - 1; 129 | array = new int[this.capacity]; 130 | } 131 | 132 | // Queue is full when size becomes 133 | // equal to the capacity 134 | boolean isFull (Queue queue) 135 | { 136 | return (queue.size == queue.capacity); 137 | } 138 | 139 | // Queue is empty when size is 0 140 | boolean isEmpty (Queue queue) 141 | { 142 | return (queue.size == 0); 143 | } 144 | 145 | // Method to add an item to the queue. 146 | // It changes rear and size 147 | void enqueue (int item) 148 | { 149 | if (isFull (this)) 150 | return; 151 | this.rear = (this.rear + 1) % this.capacity; 152 | this.array[this.rear] = item; 153 | this.size = this.size + 1; 154 | 155 | } 156 | int dequeue () 157 | { 158 | if (isEmpty (this)) 159 | return Integer.MIN_VALUE; 160 | 161 | int item = this.array[this.front]; 162 | this.front = (this.front + 1) % this.capacity; 163 | this.size = this.size - 1; 164 | return item; 165 | } 166 | 167 | public Queue reverseQueue (Queue q) 168 | { 169 | // Base case 170 | if (q.isEmpty (q)) 171 | return q; 172 | 173 | // Dequeue current item 174 | int data = q.array[front]; 175 | q.dequeue (); 176 | 177 | // Reverse remaining queue 178 | q = reverseQueue (q); 179 | 180 | // Enqueue current item 181 | q.enqueue (data); 182 | 183 | return q; 184 | } 185 | 186 | public void disp () /* function to display the elements of the queue */ 187 | { 188 | 189 | if (front == -1) 190 | { 191 | System.out.print ("\nQueue is Empty"); 192 | } 193 | else 194 | { 195 | for (int i = front; i <= rear; i++) 196 | { 197 | System.out.print (array[i] + " "); 198 | } 199 | } 200 | System.out.println (); 201 | } 202 | 203 | } 204 | 205 | // Driver class 206 | public class Main 207 | { 208 | public static void main (String[]args) 209 | { 210 | Queue queue = new Queue (1000); 211 | 212 | queue.enqueue (10); 213 | queue.enqueue (20); 214 | queue.enqueue (30); 215 | queue.enqueue (40); 216 | 217 | System.out.println ("Original queue : "); 218 | queue.disp (); 219 | System.out.println (); 220 | 221 | queue.reverseQueue (queue); 222 | 223 | System.out.println ("Queue after reversing : "); 224 | queue.disp (); 225 | 226 | System.out.println (); 227 | 228 | 229 | } 230 | } 231 | 232 | /* 233 | * Output: 234 | Original queue : 235 | 10 20 30 40 236 | 237 | Queue after reversing : 238 | 40 30 20 10 239 | */ --------------------------------------------------------------------------------