Skip to content

Commit eb779d9

Browse files
authored
Add files via upload
This involves the basic data structures used in java😁
1 parent 08df7de commit eb779d9

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

DSA/DynamicArray.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
public class DynamicArray {
2+
int size, capacity = 10;
3+
Object[] array;
4+
5+
public DynamicArray() {
6+
this.capacity = capacity;
7+
this.array = new Object[capacity];
8+
}
9+
10+
public void add(Object data){
11+
if (size >= capacity){
12+
grow();
13+
}
14+
array[size] = data;
15+
size++;
16+
}
17+
public void insert(int index, Object data){
18+
if(size >= capacity){
19+
grow();
20+
}
21+
for(int i = size; i > index; i--){
22+
array[i] = array[i - 1];
23+
}
24+
array[index] = data;
25+
size++;
26+
}
27+
public void delete(Object data){
28+
for(int i = 0; i < size; i++){
29+
if(array[i] == data){
30+
for(int j = 0; j < (size - i - 1); j++){
31+
array[i + j] = array[i + j + 1];
32+
}
33+
array[size - 1] = null;
34+
size--;
35+
if(size <= (int)(capacity/3)){
36+
shrink();
37+
}
38+
break;
39+
}
40+
}
41+
}
42+
public int search(Object data){
43+
return -1;
44+
}
45+
46+
private void grow(){
47+
48+
}
49+
private void shrink(){
50+
51+
}
52+
public boolean isEmpty(){
53+
return size == 0;
54+
}
55+
56+
public String toString(){
57+
String string = "";
58+
59+
for (int i =0; i < capacity; i++){
60+
string += array[i] + ", ";
61+
}
62+
if(string != ""){
63+
string = "["+string.substring(0, string.length() - 2) + "]";
64+
}
65+
else{
66+
string = "[]";
67+
}
68+
return string;
69+
}
70+
}

DSA/Main.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
DynamicArray dynamicArray = new DynamicArray();
4+
5+
dynamicArray.add("A");
6+
dynamicArray.add("B");
7+
dynamicArray.add("C");
8+
9+
dynamicArray.insert(9, "D");
10+
dynamicArray.delete("A");
11+
12+
System.out.println(dynamicArray);
13+
System.out.println("size: "+dynamicArray.size);
14+
System.out.println("capacity: "+dynamicArray.capacity);
15+
System.out.println("Empty: "+dynamicArray.isEmpty());
16+
}
17+
}

DSA/Main_ArrayList.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Main_ArrayList{
4+
5+
//Class method for the ArrayList.
6+
7+
8+
9+
public static void main(String[] args) {
10+
//ArrayList variable.
11+
ArrayList <Integer> array = new ArrayList<>(List.of(10, 20, 30, 40, 50));
12+
13+
//Listing elements at index
14+
System.out.println("Elements at index 0:" + array.get(0));
15+
System.out.println("Elements at index 2:" + array.get(20));
16+
System.out.println("Elements at index 4:" + array.get(4));
17+
18+
int Sum = array.stream().mapToInt(Integer::intValue).sum();
19+
System.out.println("Sum of the ArrayList elements:" + Sum);
20+
21+
array.set(2, 35);
22+
System.out.println("Updated elements at index 2:" + array.get(2));
23+
24+
System.out.println("Elements in the ArrayList: ");
25+
for(int element: array){
26+
System.out.println(element);
27+
}
28+
}
29+
}

DSA/Main_Queue.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import java.util.LinkedList;
2+
import java.util.PriorityQueue;
3+
import java.util.Queue;
4+
/*
5+
* Queue - FIFO Data Structure. First-In First-Out.
6+
A collection design for holding elements prior to processing
7+
Linear data structures.
8+
9+
*Dequeue - Removing queued elements, .poll();
10+
*Enqueue - Adding queued elements, .offer();
11+
*/
12+
13+
14+
public class Main_Queue {
15+
16+
//Queue_ method.
17+
public void Queue_(){
18+
Queue <String> queue = new LinkedList<String>(); // Queue is an interface not a class.
19+
20+
// Adding queue elements
21+
queue.offer("Karen");
22+
queue.offer("Chad");
23+
queue.offer("Steve");
24+
queue.offer("Harold");
25+
26+
//Removing queue elements
27+
queue.poll();
28+
29+
//To see the number of people present in the queue
30+
System.out.println(queue.size());
31+
32+
//To see if an element is present in the queue
33+
System.out.println(queue.contains("Chad"));
34+
}
35+
36+
//Linked_LIst method
37+
public void Linked_List(){
38+
LinkedList<String> linkedList = new LinkedList<String>();
39+
/*
40+
As Stack
41+
linkedList.push("A");
42+
linkedList.push("C");
43+
linkedList.push("S");
44+
linkedList.push("X");
45+
*/
46+
47+
//As Queue
48+
linkedList.offer("A");
49+
linkedList.offer("C");
50+
linkedList.offer("S");
51+
linkedList.offer("X");
52+
53+
//Adding and removing items
54+
linkedList.add(3,"E");
55+
linkedList.remove(4);
56+
57+
linkedList.addFirst("0");//Adding an item to the first
58+
linkedList.addLast("G");//Adding an item to the last
59+
String first = linkedList.removeFirst(); //Removing the first item
60+
String last = linkedList.removeLast(); //Removing the last item.
61+
62+
//Peek
63+
System.out.println(linkedList.peekFirst()); // To see first item
64+
System.out.println(linkedList.peekLast()); // To see last item
65+
66+
//Output of new stack.
67+
System.err.println(linkedList);
68+
}
69+
70+
//Priority_Queue method.
71+
public void Priority_Queue(){//Higher priority are served first before lower ones.
72+
Queue<String> queue = new PriorityQueue<>();
73+
queue.offer("B");
74+
queue.offer("C");
75+
queue.offer("A");
76+
queue.offer("D");
77+
queue.offer("F");
78+
79+
//output
80+
81+
while(!(queue.isEmpty())){
82+
System.out.println(queue.poll());
83+
}
84+
}
85+
public static void main(String[] args) {
86+
Main_Queue q = new Main_Queue();
87+
q.Linked_List();
88+
//q.Priority_Queue();
89+
}
90+
}

0 commit comments

Comments
 (0)