Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 47a0749

Browse files
Linked list update 1
1 parent f785528 commit 47a0749

File tree

8 files changed

+465
-171
lines changed

8 files changed

+465
-171
lines changed

‎.idea/workspace.xml‎

Lines changed: 203 additions & 171 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Arrays and Strings/D16_Product_Of_Array_Except_Self.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import java.util.ArrayList;
12
import java.util.Scanner;
23

34
public class D16_Product_Of_Array_Except_Self {
@@ -13,6 +14,7 @@ public static int[] productExceptSelf(int[] nums) {
1314
nums[i] = (int)Math.round(Math.exp(sum-arr[i]));
1415
}
1516
return nums;
17+
1618
}
1719

1820
//Don't make changes here.

‎Arrays and Strings/gs.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import java.util.*;
3+
import java.lang.*;
4+
import java.io.*;
5+
6+
public class gs {
7+
/* package codechef; // don't place package name! */
8+
9+
10+
/* Name of the class has to be "Main" only if the class is public. */
11+
public static void main(String[] args) {
12+
B b = new B();
13+
System.out.println((b instanceof B));
14+
System.out.println((b instanceof B)&&(!(b instanceof A)));
15+
System.out.println((b instanceof B)&&(!(b instanceof C)));
16+
}
17+
18+
}
19+
class A{}
20+
class B extends A{}
21+
class C extends B{}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.*;
2+
3+
public class D61_LenOfShortestChain_ToReachATarget {
4+
public static void main(String[] args) {
5+
Set<String> dict = new HashSet<>();
6+
dict.add("poon");
7+
dict.add("plee");
8+
dict.add("same");
9+
dict.add("poie");
10+
dict.add("plie");
11+
dict.add("poin");
12+
dict.add("plea");
13+
System.out.println(shortestChainLen(dict,"toon","plea"));
14+
15+
}
16+
public static int shortestChainLen(Set<String> dict,String src,String target){
17+
Queue<Pair> q = new LinkedList<>();
18+
Pair item = new Pair(src,1);
19+
q.add(item);
20+
Set<String> used = new HashSet<>();
21+
while (!q.isEmpty()){
22+
Pair curr = q.remove();
23+
for(String w:dict){
24+
if(!used.contains(w) && isAdjacent(w,curr.word)){
25+
item.word = w;
26+
item.len = curr.len+1;
27+
q.add(item);
28+
used.add(w);
29+
if(w.equals(target))
30+
return item.len;
31+
}
32+
}
33+
}
34+
return 0;
35+
}
36+
private static boolean isAdjacent(String word, String word1) {
37+
int count=0;
38+
39+
for (int i = 0; i < word.length(); i++) {
40+
char a = word.charAt(i);
41+
char b = word1.charAt(i);
42+
if(a!=b)
43+
count++;
44+
if(count>1)
45+
return false;
46+
47+
}
48+
return true;
49+
}
50+
}
51+
class Pair{
52+
String word;
53+
int len;
54+
Pair(String word,int len){
55+
this.word = word;
56+
this.len = len;
57+
}
58+
}

‎Graphs/D61_Topological_Sort.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.LinkedList;
2+
import java.util.Stack;
3+
4+
public class D61_Topological_Sort {
5+
public static void main(String[] args) {
6+
Graph graph = new Graph(5);
7+
graph.addEdge(0,1);
8+
graph.addEdge(0,4);
9+
graph.addEdge(4,1);
10+
graph.addEdge(4,3);
11+
graph.addEdge(3,1);
12+
graph.addEdge(3,2);
13+
graph.addEdge(1,2);
14+
graph.printGraph();
15+
//though pointless here since while adding edge
16+
//it makes undirected graph in which topological sort
17+
//is DFS only
18+
String ans = topologicalSort(graph);
19+
System.out.println(ans);
20+
}
21+
public static String topologicalSort(Graph graph){
22+
boolean[] visited = new boolean[graph.V];
23+
topoSort = new Stack<>();
24+
for (int i = 0; i <graph.V ; i++) {
25+
if(!visited[i]){
26+
topologicalSortUtil(graph,visited,i);
27+
}
28+
}
29+
StringBuilder sb = new StringBuilder();
30+
while (!topoSort.isEmpty()){
31+
sb.append(topoSort.pop());
32+
if(!topoSort.isEmpty())
33+
sb.append(", ");
34+
}
35+
return sb.toString();
36+
37+
}
38+
public static Stack<Integer> topoSort;
39+
private static void topologicalSortUtil(Graph graph, boolean[] visited, int i) {
40+
visited[i] = true;
41+
LinkedList<Integer> adj = graph.list.get(i);
42+
for (int ele:adj){
43+
if (!visited[ele]){
44+
topologicalSortUtil(graph, visited, ele);
45+
}
46+
}
47+
topoSort.push(i);
48+
}
49+
}

‎Graphs/Graph.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
4+
public class Graph {
5+
int V;
6+
ArrayList<LinkedList<Integer>> list;
7+
Graph(int V){
8+
this.V = V;
9+
list = new ArrayList<>();
10+
for (int i = 0; i < V; i++) {
11+
list.add(new LinkedList<Integer>());
12+
}
13+
}
14+
void addEdge(int src,int dest){
15+
LinkedList<Integer> s = list.get(src);
16+
s.add(dest);
17+
LinkedList<Integer> d = list.get(dest);
18+
d.add(src);
19+
}
20+
void printGraph(){
21+
for (int i = 0; i <V ; i++) {
22+
System.out.print("Adjacency List of "+i+" : ");
23+
LinkedList<Integer> ll = list.get(i);
24+
for (Integer curr:ll){
25+
System.out.print(curr+", ");
26+
}
27+
System.out.println();
28+
}
29+
}
30+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.Scanner;
2+
3+
public class D1_arrange_vowels_and_consonants{
4+
5+
public static void main(String args[]) {
6+
Scanner sc = new Scanner(System.in);
7+
8+
int n = sc.nextInt();
9+
char h = sc.next().charAt(0);
10+
insert(h);
11+
12+
for (int i = 1; i < n; i++) {
13+
char ch = sc.next().charAt(0);
14+
insert(ch);
15+
}
16+
17+
head = arcv(head);
18+
display();
19+
20+
}
21+
22+
//---------------------------------------------------------------
23+
//This is a functional problem. Only this function has to be written.
24+
// This function should return the head of node after sorting.
25+
public static Node arcv(Node head) {
26+
Node vowel = null;
27+
Node vt = null;
28+
Node consonant = null;
29+
Node ct = null;
30+
while(head!=null){
31+
if(isVowel(head.data)){
32+
if(vowel==null){
33+
vowel = new Node(head.data,null);
34+
vt = vowel;
35+
}else{
36+
vt.next = new Node(head.data,null);
37+
vt = vt.next;
38+
}
39+
}
40+
else{
41+
if(consonant==null){
42+
consonant = new Node(head.data,null);
43+
ct = consonant;
44+
}else{
45+
ct.next = new Node(head.data,null);
46+
ct = ct.next;
47+
}
48+
}
49+
50+
head = head.next;
51+
}
52+
if(vowel!=null)
53+
vt.next = consonant;
54+
else
55+
return consonant;
56+
return vowel;
57+
58+
}
59+
public static boolean isVowel(char ch){
60+
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
61+
return true;
62+
63+
return false;
64+
}
65+
//------------------------------------------------------------------
66+
67+
68+
private static class Node {
69+
char data;
70+
Node next;
71+
72+
public Node(char data, Node next) {
73+
this.data = data;
74+
this.next = next;
75+
}
76+
77+
}
78+
79+
static Node head;
80+
static Node tail;
81+
static int size;
82+
83+
public static void insert(char data) {
84+
85+
Node nn = new Node(data, null);
86+
if (head == null) {
87+
head = nn;
88+
tail = nn;
89+
} else {
90+
tail.next = nn;
91+
tail = nn;
92+
}
93+
size++;
94+
}
95+
96+
public static void display() {
97+
for (Node node = head; node != null; node = node.next) {
98+
System.out.print(node.data+" ");
99+
}
100+
}
101+
102+
}
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /