-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from tadiharshith/main
Array.java
- Loading branch information
Showing
5 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
class GFG { | ||
public static void main(String[] args) | ||
{ | ||
|
||
int[] arr; | ||
|
||
|
||
arr = new int[5]; | ||
|
||
|
||
arr[0] = 10; | ||
|
||
|
||
arr[1] = 20; | ||
|
||
// so on... | ||
arr[2] = 30; | ||
arr[3] = 40; | ||
arr[4] = 50; | ||
|
||
|
||
for (int i = 0; i < arr.length; i++) | ||
System.out.println("Element at index " + i | ||
+ " : " + arr[i]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
|
||
|
||
import java.util.*; | ||
|
||
class Graph<T> { | ||
|
||
|
||
private Map<T, List<T> > map = new HashMap<>(); | ||
|
||
|
||
public void addVertex(T s) | ||
{ | ||
map.put(s, new LinkedList<T>()); | ||
} | ||
|
||
public void addEdge(T source, | ||
T destination, | ||
boolean bidirectional) | ||
{ | ||
|
||
if (!map.containsKey(source)) | ||
addVertex(source); | ||
|
||
if (!map.containsKey(destination)) | ||
addVertex(destination); | ||
|
||
map.get(source).add(destination); | ||
if (bidirectional == true) { | ||
map.get(destination).add(source); | ||
} | ||
} | ||
|
||
|
||
public void getVertexCount() | ||
{ | ||
System.out.println("The graph has " | ||
+ map.keySet().size() | ||
+ " vertex"); | ||
} | ||
|
||
|
||
public void getEdgesCount(boolean bidirection) | ||
{ | ||
int count = 0; | ||
for (T v : map.keySet()) { | ||
count += map.get(v).size(); | ||
} | ||
if (bidirection == true) { | ||
count = count / 2; | ||
} | ||
System.out.println("The graph has " | ||
+ count | ||
+ " edges."); | ||
} | ||
|
||
public void hasVertex(T s) | ||
{ | ||
if (map.containsKey(s)) { | ||
System.out.println("The graph contains " | ||
+ s + " as a vertex."); | ||
} | ||
else { | ||
System.out.println("The graph does not contain " | ||
+ s + " as a vertex."); | ||
} | ||
} | ||
|
||
|
||
public void hasEdge(T s, T d) | ||
{ | ||
if (map.get(s).contains(d)) { | ||
System.out.println("The graph has an edge between " | ||
+ s + " and " + d + "."); | ||
} | ||
else { | ||
System.out.println("The graph has no edge between " | ||
+ s + " and " + d + "."); | ||
} | ||
} | ||
|
||
public String toString() | ||
{ | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
for (T v : map.keySet()) { | ||
builder.append(v.toString() + ": "); | ||
for (T w : map.get(v)) { | ||
builder.append(w.toString() + " "); | ||
} | ||
builder.append("\n"); | ||
} | ||
|
||
return (builder.toString()); | ||
} | ||
} | ||
|
||
|
||
public class Main { | ||
|
||
public static void main(String args[]) | ||
{ | ||
|
||
|
||
Graph<Integer> g = new Graph<Integer>(); | ||
|
||
g.addEdge(0, 1, true); | ||
g.addEdge(0, 4, true); | ||
g.addEdge(1, 2, true); | ||
g.addEdge(1, 3, true); | ||
g.addEdge(1, 4, true); | ||
g.addEdge(2, 3, true); | ||
g.addEdge(3, 4, true); | ||
|
||
|
||
System.out.println("Graph:\n" | ||
+ g.toString()); | ||
|
||
|
||
g.getVertexCount(); | ||
|
||
|
||
g.getEdgesCount(true); | ||
|
||
|
||
g.hasEdge(3, 4); | ||
|
||
|
||
g.hasVertex(5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import java.io.*; | ||
public class LinkedList { | ||
|
||
Node head; // head of list | ||
|
||
static class Node { | ||
|
||
int data; | ||
Node next; | ||
|
||
|
||
Node(int d) | ||
{ | ||
data = d; | ||
next = null; | ||
} | ||
} | ||
|
||
|
||
public static LinkedList insert(LinkedList list, int data) | ||
{ | ||
|
||
Node new_node = new Node(data); | ||
|
||
|
||
if (list.head == null) { | ||
list.head = new_node; | ||
} | ||
else { | ||
Node last = list.head; | ||
while (last.next != null) { | ||
last = last.next; | ||
} | ||
|
||
|
||
last.next = new_node; | ||
} | ||
|
||
|
||
return list; | ||
} | ||
|
||
|
||
public static void printList(LinkedList list) | ||
{ | ||
Node currNode = list.head; | ||
|
||
System.out.print("LinkedList: "); | ||
while (currNode != null) { | ||
|
||
System.out.print(currNode.data + " "); | ||
|
||
// Go to next node | ||
currNode = currNode.next; | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) | ||
{ | ||
|
||
LinkedList list = new LinkedList(); | ||
|
||
list = insert(list, 1); | ||
list = insert(list, 2); | ||
list = insert(list, 3); | ||
list = insert(list, 4); | ||
list = insert(list, 5); | ||
list = insert(list, 6); | ||
list = insert(list, 7); | ||
list = insert(list, 8); | ||
|
||
|
||
|
||
printList(list); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
public class QueueExample { | ||
|
||
public static void main(String[] args) | ||
{ | ||
Queue<Integer> q | ||
= new LinkedList<>(); | ||
|
||
for (int i = 0; i < 5; i++) | ||
q.add(i); | ||
|
||
|
||
System.out.println("Elements of queue " | ||
+ q); | ||
|
||
|
||
int removedele = q.remove(); | ||
System.out.println("removed element-" | ||
+ removedele); | ||
|
||
System.out.println(q); | ||
|
||
|
||
int head = q.peek(); | ||
System.out.println("head of queue-" | ||
+ head); | ||
|
||
int size = q.size(); | ||
System.out.println("Size of queue-" | ||
+ size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
static Node addAfter(Node last, int data, int item) | ||
{ | ||
if (last == null) | ||
return null; | ||
|
||
Node temp, p; | ||
p = last.next; | ||
do { | ||
if (p.data == item) { | ||
temp = new Node(); | ||
temp.data = data; | ||
temp.next = p.next; | ||
p.next = temp; | ||
|
||
if (p == last) | ||
last = temp; | ||
return last; | ||
} | ||
p = p.next; | ||
} while (p != last.next); | ||
|
||
System.out.println(item + " not present in the list."); | ||
return last; | ||
} | ||
|