Skip to content

Commit 54f04cd

Browse files
author
aarshakyan
committed
bobocode-projects#2 Data Structures and Algorithms
1 parent 1c7b217 commit 54f04cd

File tree

4 files changed

+140
-18
lines changed

4 files changed

+140
-18
lines changed

2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Node.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,31 @@
99
* @author Taras Boychuk
1010
*/
1111
public class Node<T> {
12-
// todo:
12+
13+
private T value;
14+
private Node<T> next;
15+
16+
public Node() {
17+
}
18+
19+
public Node(T value) {
20+
this.value = value;
21+
}
22+
23+
24+
public T getValue() {
25+
return value;
26+
}
27+
28+
public void setValue(T value) {
29+
this.value = value;
30+
}
31+
32+
public Node<T> getNext() {
33+
return next;
34+
}
35+
36+
public void setNext(Node<T> next) {
37+
this.next = next;
38+
}
1339
}

2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Nodes.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private Nodes() {
2222
* @return a new instance of {@link Node}
2323
*/
2424
public static <T> Node<T> create(T element) {
25-
throw new ExerciseNotCompletedException(); // todo:
25+
return new Node<>(element);
2626
}
2727

2828
/**
@@ -33,7 +33,7 @@ public static <T> Node<T> create(T element) {
3333
* @param <T> a genetic type
3434
*/
3535
public static <T> void link(Node<T> first, Node<T> second) {
36-
throw new ExerciseNotCompletedException(); // todo:
36+
first.setNext(second);
3737
}
3838

3939
/**
@@ -46,7 +46,10 @@ public static <T> void link(Node<T> first, Node<T> second) {
4646
* @return a reference to a first node created based on firstElement
4747
*/
4848
public static <T> Node<T> pairOf(T firstElement, T secondElement) {
49-
throw new ExerciseNotCompletedException(); // todo:
49+
Node<T> firstNode = create(firstElement);
50+
Node<T> secondNode = create(secondElement);
51+
firstNode.setNext(secondNode);
52+
return firstNode;
5053
}
5154

5255
/**
@@ -60,7 +63,9 @@ public static <T> Node<T> pairOf(T firstElement, T secondElement) {
6063
* @return a reference to the first node
6164
*/
6265
public static <T> Node<T> closedPairOf(T firstElement, T secondElement) {
63-
throw new ExerciseNotCompletedException(); // todo:
66+
Node<T> firstNode = pairOf(firstElement, secondElement);
67+
firstNode.getNext().setNext(firstNode);
68+
return firstNode;
6469
}
6570

6671
/**
@@ -72,7 +77,15 @@ public static <T> Node<T> closedPairOf(T firstElement, T secondElement) {
7277
* @return a reference to the first element of the chain
7378
*/
7479
public static <T> Node<T> chainOf(T... elements) {
75-
throw new ExerciseNotCompletedException(); // todo:
80+
Node<T>[] array = new Node[elements.length];
81+
for (int i = 0; i < elements.length; i++) {
82+
Node<T> node = create(elements[i]);
83+
array[i] = node;
84+
if (i > 0) {
85+
array[i - 1].setNext(node);
86+
}
87+
}
88+
return array[0];
7689
}
7790

7891
/**
@@ -85,6 +98,13 @@ public static <T> Node<T> chainOf(T... elements) {
8598
* @return a reference to the first element of the chain
8699
*/
87100
public static <T> Node<T> circleOf(T... elements) {
88-
throw new ExerciseNotCompletedException(); // todo:
101+
Node<T> firstNode = chainOf(elements);
102+
Node<T> lastNode = firstNode;
103+
104+
while (lastNode.getNext() != null) {
105+
lastNode = lastNode.getNext();
106+
}
107+
lastNode.setNext(firstNode);
108+
return firstNode;
89109
}
90110
}

2-0-data-structures-and-algorithms/2-2-2-stack/src/main/java/com/bobocode/cs/LinkedStack.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import com.bobocode.cs.exception.EmptyStackException;
44
import com.bobocode.util.ExerciseNotCompletedException;
5+
import com.sun.security.auth.NTUserPrincipal;
6+
import org.checkerframework.checker.units.qual.A;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
511

612
/**
713
* {@link LinkedStack} is a stack implementation that is based on singly linked generic nodes.
@@ -15,6 +21,10 @@
1521
* @author Serhii Hryhus
1622
*/
1723
public class LinkedStack<T> implements Stack<T> {
24+
private int size;
25+
26+
private Node<T> head;
27+
1828

1929
/**
2030
* This method creates a stack of provided elements
@@ -24,7 +34,11 @@ public class LinkedStack<T> implements Stack<T> {
2434
* @return a new stack of elements that were passed as method parameters
2535
*/
2636
public static <T> LinkedStack<T> of(T... elements) {
27-
throw new ExerciseNotCompletedException(); // todo: implement this method
37+
LinkedStack<T> stack = new LinkedStack<>();
38+
for (T element : elements) {
39+
stack.push(element);
40+
}
41+
return stack;
2842
}
2943

3044
/**
@@ -35,7 +49,13 @@ public static <T> LinkedStack<T> of(T... elements) {
3549
*/
3650
@Override
3751
public void push(T element) {
38-
throw new ExerciseNotCompletedException(); // todo: implement this method
52+
if (element == null) {
53+
throw new NullPointerException();
54+
}
55+
Node<T> tNode = new Node<>(element);
56+
tNode.setNext(head);
57+
head = tNode;
58+
size++;
3959
}
4060

4161
/**
@@ -47,7 +67,13 @@ public void push(T element) {
4767
*/
4868
@Override
4969
public T pop() {
50-
throw new ExerciseNotCompletedException(); // todo: implement this method
70+
if (head == null) {
71+
throw new EmptyStackException();
72+
}
73+
Node<T> nodeToReturn = head;
74+
head = head.getNext();
75+
size--;
76+
return nodeToReturn.value;
5177
}
5278

5379
/**
@@ -57,7 +83,7 @@ public T pop() {
5783
*/
5884
@Override
5985
public int size() {
60-
throw new ExerciseNotCompletedException(); // todo: implement this method
86+
return size;
6187
}
6288

6389
/**
@@ -67,7 +93,26 @@ public int size() {
6793
*/
6894
@Override
6995
public boolean isEmpty() {
70-
throw new ExerciseNotCompletedException(); // todo: implement this method;
96+
return head == null;
7197
}
7298

99+
private static class Node<T> {
100+
private final T value;
101+
102+
private Node<T> next;
103+
104+
private Node(T element) {
105+
this.value = element;
106+
}
107+
108+
109+
public void setNext(Node<T> next) {
110+
this.next = next;
111+
}
112+
113+
public Node<T> getNext() {
114+
return next;
115+
}
116+
117+
}
73118
}

2-0-data-structures-and-algorithms/2-2-3-linked-queue/src/main/java/com/bobocode/cs/LinkedQueue.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.bobocode.cs;
22

3-
import com.bobocode.util.ExerciseNotCompletedException;
4-
53
/**
64
* {@link LinkedQueue} implements FIFO {@link Queue}, using singly linked nodes. Nodes are stores in instances of nested
75
* class Node. In order to perform operations {@link LinkedQueue#add(Object)} and {@link LinkedQueue#poll()}
@@ -15,14 +13,37 @@
1513
* @author Ivan Virchenko
1614
*/
1715
public class LinkedQueue<T> implements Queue<T> {
16+
static final class Node<T> {
17+
private T element;
18+
private Node<T> next;
19+
20+
static <T> Node<T> valueOf(T element) {
21+
return new Node<>(element);
22+
}
23+
24+
private Node(T element) {
25+
this.element = element;
26+
}
27+
}
28+
29+
private Node<T> head;
30+
private Node<T> tail;
31+
private int size;
1832

1933
/**
2034
* Adds an element to the end of the queue.
2135
*
2236
* @param element the element to add
2337
*/
2438
public void add(T element) {
25-
throw new ExerciseNotCompletedException(); // todo: implement this method
39+
Node<T> newNode = Node.valueOf(element);
40+
if (head == null) {
41+
head = tail = newNode;
42+
} else {
43+
tail.next = newNode;
44+
tail = newNode;
45+
}
46+
size++;
2647
}
2748

2849
/**
@@ -31,7 +52,17 @@ public void add(T element) {
3152
* @return an element that was retrieved from the head or null if queue is empty
3253
*/
3354
public T poll() {
34-
throw new ExerciseNotCompletedException(); // todo: implement this method
55+
if (head != null) {
56+
T element = head.element;
57+
head = head.next;
58+
if (head == null) {
59+
tail = null;
60+
}
61+
size--;
62+
return element;
63+
} else {
64+
return null;
65+
}
3566
}
3667

3768
/**
@@ -40,7 +71,7 @@ public T poll() {
4071
* @return an integer value that is a size of queue
4172
*/
4273
public int size() {
43-
throw new ExerciseNotCompletedException(); // todo: implement this method
74+
return size;
4475
}
4576

4677
/**
@@ -49,6 +80,6 @@ public int size() {
4980
* @return {@code true} if the queue is empty, returns {@code false} if it's not
5081
*/
5182
public boolean isEmpty() {
52-
throw new ExerciseNotCompletedException(); // todo: implement this method
83+
return head == null;
5384
}
5485
}

0 commit comments

Comments
 (0)