Skip to content

Commit d342c2d

Browse files
authored
Merge pull request TheAlgorithms#1322 from Ritik2604/master
Added Important Algos
2 parents ce04b7f + a23a17b commit d342c2d

File tree

8 files changed

+1003
-74
lines changed

8 files changed

+1003
-74
lines changed

DataStructures/Graphs/GraphAlgos

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package DataStructures.HashMap.Hashing;
2+
/*
3+
* this is algo which implies common mathematical set theory concept
4+
* called intersection in which result is common values of both the sets
5+
* here metaphor of sets is HashMap
6+
7+
8+
Test Case:
9+
Scanner scn=new Scanner(System.in);
10+
int len =scn.nextInt();
11+
int arr[]=new int[len];
12+
int arr2[]=new int[len];
13+
14+
for(int i=0;i<2*len;i++) {
15+
16+
if(i<len)
17+
arr[i]=scn.nextInt();
18+
if(i>=len) {
19+
arr2[i-len]=scn.nextInt();
20+
}
21+
}
22+
System.out.println(Main(arr,arr2));
23+
24+
25+
26+
*/
27+
28+
import java.util.ArrayList;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import java.util.Scanner;
32+
import java.util.Set;
33+
34+
public class Intersection {
35+
36+
public static ArrayList Main(int arr[],int arr2[]) {
37+
HashMap<Integer,Integer> hmap=new HashMap<>();
38+
HashMap<Integer,Integer> hmap2=new HashMap<>();
39+
for(int i=0;i<arr.length;i++) {
40+
if(hmap.containsKey(arr[i])) {
41+
int val=hmap.get(arr[i]);
42+
hmap.put(arr[i],val+1);
43+
}else
44+
hmap.put(arr[i],1);
45+
46+
}
47+
ArrayList<Integer> res=new ArrayList<>();
48+
for(int i=0;i<arr2.length;i++) {
49+
if(hmap.containsKey(arr2[i])&&hmap.get(arr2[i])>0) {
50+
int val=hmap.get(arr2[i]);
51+
hmap.put(arr2[i],val-1);
52+
res.add(arr2[i]);
53+
}
54+
55+
}
56+
return res;
57+
}
58+
public Intersection() {
59+
60+
}
61+
62+
63+
64+
}

DataStructures/HashMap/Hashing/LinkedList.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

DataStructures/HashMap/Hashing/Node.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

DataStructures/Lists/ListAddnFun

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package DataStructures.Lists;
2+
3+
/*
4+
* This class implements a SinglyLinked List.
5+
* A linked list is similar to an array, it hold values.
6+
* However, links in a linked list do not have indexes. With
7+
* a linked list you do not need to predetermine it's size as
8+
* it grows and shrinks as it is edited.
9+
*it has functions called mid that gives node at mid
10+
* in addn to linked list there is algo that
11+
* construct a linked list with alternate sums of linked list
12+
* and added to new one and add mid value
13+
* i.e sum of first and last value of inital list
14+
15+
Test Case:
16+
17+
18+
LinkedList LL1 = new LinkedList();
19+
Scanner scn=new Scanner(System.in);
20+
int numNodes=scn.nextInt();
21+
for(int i=0;i<2*numNodes;i++) {
22+
LL1.addLast(scn.nextInt());
23+
}
24+
LL1.display();
25+
LinkedList LL2=new LinkedList();
26+
LL2.formLL2(LL1);
27+
LL2.display();
28+
LinkedList LL3=new LinkedList();
29+
LL3.formLL3(LL1);
30+
LL3.display();
31+
Node MID=LL1.midValue();
32+
System.out.println(MID.data);
33+
LinkedList updLL1=new LinkedList();
34+
updLL1.formRes(LL1,LL2,LL3,MID);
35+
updLL1.display();
36+
updLL1.Size();
37+
38+
*/
39+
40+
41+
42+
import java.util.*;
43+
import java.lang.*;
44+
import java.io.*;
45+
class LinkedList {
46+
private class Node{
47+
int data;
48+
Node next;
49+
50+
Node(int data) {
51+
this.data = data;
52+
this.next = null;
53+
}
54+
}
55+
public Node head = null;
56+
public Node tail = null;
57+
private int size=0;
58+
59+
public void addLast(int data) {
60+
Node newNode = new Node(data);
61+
62+
if(this.head == null) {
63+
this.head = newNode;
64+
this.tail = newNode;
65+
this.size++;
66+
}
67+
else {
68+
this.tail.next = newNode;
69+
this.tail = newNode;
70+
this.size++;
71+
}
72+
}
73+
74+
75+
public void display() {
76+
Node current = this.head;
77+
if(this.head == null) {
78+
return;
79+
}
80+
while(current != null) {
81+
System.out.print(current.data + " ");
82+
current = current.next;
83+
}
84+
System.out.println();
85+
}
86+
87+
public void formLL2(LinkedList LL1) {
88+
Node current=LL1.head;
89+
while(current.next!=null&&current.next.next!=null) {
90+
int sum=current.data+current.next.next.data;
91+
this.addLast(sum);
92+
current=current.next.next;
93+
}
94+
}
95+
public void formLL3(LinkedList LL1) {
96+
Node current=LL1.head.next;
97+
while(current.next!=null&&current.next.next!=null) {
98+
int sum=current.data+current.next.next.data;
99+
this.addLast(sum);
100+
current=current.next.next;
101+
}
102+
}
103+
public Node mid() {
104+
Node slow=this.head;
105+
Node fast=this.head;
106+
while(fast.next!=null && fast.next.next!=null) {
107+
slow=slow.next;
108+
fast=fast.next.next;
109+
}
110+
return slow;
111+
}
112+
public Node midValue() {
113+
int sum=this.head.data+this.tail.data;
114+
Node mid=new Node(sum);
115+
return mid;
116+
}
117+
public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) {
118+
Node LL1mid=LL1.mid();
119+
Node currentLL1=LL1.head;
120+
Node currentLL2=LL2.head;
121+
Node currentLL3=LL3.head;
122+
while(currentLL1!=null) {
123+
this.addLast(currentLL1.data);
124+
125+
if(currentLL2!=null) {
126+
this.addLast(currentLL2.data);
127+
currentLL2=currentLL2.next;
128+
}else if(currentLL1.equals(LL1mid)) {
129+
this.addLast(MID.data);
130+
}
131+
else if(currentLL2==null&&currentLL3!=null) {
132+
this.addLast(currentLL3.data);
133+
currentLL3=currentLL3.next;
134+
}
135+
currentLL1=currentLL1.next;
136+
}
137+
}
138+
public void Size() {
139+
System.out.println(this.size);
140+
}
141+
}

0 commit comments

Comments
 (0)