Skip to content

Commit 91d6547

Browse files
committed
Lesson 2 more examples and activities
1 parent 99a4aeb commit 91d6547

12 files changed

Lines changed: 349 additions & 19 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.packt.datastructuresandalg.lesson2.activity.postfix;
2+
3+
public class EvalPostfix {
4+
public double evaluate(String postfix) {
5+
return 0.0;
6+
}
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.packt.datastructuresandalg.lesson2.activity.postfix.solution;
2+
3+
import com.packt.datastructuresandalg.lesson2.stack.Stack;
4+
5+
public class EvalPostfix {
6+
7+
public double evaluate(String postfix) {
8+
Stack<Integer> stack = new Stack<>();
9+
for (String token : postfix.split(" ")) {
10+
if (token.chars().allMatch(Character::isDigit))
11+
stack.push(Integer.parseInt(token));
12+
else if (token.equals("/"))
13+
stack.pop().flatMap(op1 -> stack.pop().map(op2 -> op2 / op1))
14+
.ifPresent(stack::push);
15+
else if (token.equals("*"))
16+
stack.pop().flatMap(op1 -> stack.pop().map(op2 -> op2 * op1))
17+
.ifPresent(stack::push);
18+
else if (token.equals("+"))
19+
stack.pop().flatMap(op1 -> stack.pop().map(op2 -> op2 + op1))
20+
.ifPresent(stack::push);
21+
else if (token.equals("-"))
22+
stack.pop().flatMap(op1 -> stack.pop().map(op2 -> op2 - op1))
23+
.ifPresent(stack::push);
24+
}
25+
return stack.pop().orElseThrow(() -> new IllegalArgumentException("Bad expression"));
26+
}
27+
}

src/main/java/com/packt/datastructuresandalg/lesson2/linkedlist/DblLinkedListNode.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44

55
public class DblLinkedListNode<V> {
66
private V value;
7-
private Optional<DblLinkedListNode<V>> next;
8-
private Optional<DblLinkedListNode<V>> previous;
7+
private DblLinkedListNode<V> next;
8+
private DblLinkedListNode<V> previous;
99

1010
public DblLinkedListNode(V value,
11-
Optional<DblLinkedListNode<V>> next,
12-
Optional<DblLinkedListNode<V>> previous) {
11+
DblLinkedListNode<V> next,
12+
DblLinkedListNode<V> previous) {
1313
this.value = value;
1414
this.next = next;
1515
this.previous = previous;
1616
}
1717

18+
public Optional<DblLinkedListNode<V>> getNext() {
19+
return Optional.ofNullable(next);
20+
}
21+
22+
public Optional<DblLinkedListNode<V>> getPrevious() {
23+
return Optional.ofNullable(previous);
24+
}
25+
1826
public V getValue() {
1927
return value;
2028
}
@@ -24,12 +32,14 @@ public DblLinkedListNode setValue(V value) {
2432
return this;
2533
}
2634

27-
public Optional<DblLinkedListNode<V>> getNext() {
28-
return next;
35+
public DblLinkedListNode setNext(DblLinkedListNode<V> next) {
36+
this.next = next;
37+
return this;
2938
}
3039

31-
public DblLinkedListNode setNext(Optional<DblLinkedListNode<V>> next) {
32-
this.next = next;
40+
41+
public DblLinkedListNode setPrevious(DblLinkedListNode<V> previous) {
42+
this.previous = previous;
3343
return this;
3444
}
3545
}

src/main/java/com/packt/datastructuresandalg/lesson2/linkedlist/LinkedList.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,65 @@
22

33
import java.util.Optional;
44

5-
public class LinkedList {
5+
public class LinkedList<V> {
6+
private LinkedListNode<V> head;
7+
8+
public LinkedList() {
9+
head = null;
10+
}
11+
12+
public void addFront(V item) {
13+
this.head = new LinkedListNode<>(item, head);
14+
}
15+
16+
public void deleteFront() {
17+
Optional<LinkedListNode<V>> firstNode = Optional.ofNullable(this.head);
18+
this.head = firstNode.flatMap(LinkedListNode::getNext).orElse(null);
19+
firstNode.ifPresent(n -> n.setNext(null));
20+
}
21+
22+
public Optional<LinkedListNode<V>> find(V item) {
23+
Optional<LinkedListNode<V>> node = Optional.ofNullable(this.head);
24+
while (node.filter(n -> n.getValue() != item).isPresent()) {
25+
node = node.flatMap(LinkedListNode::getNext);
26+
}
27+
return node;
28+
}
29+
30+
public void addAfter(LinkedListNode<V> aNode, V item) {
31+
aNode.setNext(new LinkedListNode<>(item, aNode.getNext().orElse(null)));
32+
}
33+
34+
public String toString() {
35+
Optional<LinkedListNode<V>> node = Optional.ofNullable(this.head);
36+
StringBuffer result = new StringBuffer("[");
37+
while (node.isPresent()) {
38+
node.ifPresent(n -> result.append(n.getValue().toString()));
39+
node = node.flatMap(LinkedListNode::getNext);
40+
node.ifPresent(n -> result.append(", "));
41+
}
42+
return result.append("]").toString();
43+
}
644

745
public static void main(String[] args) {
8-
LinkedListNode<Integer> x = new LinkedListNode<>(5, Optional.empty());
46+
LinkedList<String> list = new LinkedList();
47+
list.addFront("Isabel");
48+
list.addFront("Ruth");
49+
list.addFront("Karl");
50+
list.addFront("John");
51+
System.out.println(list.find("Isabel"));
52+
System.out.println(list.find("Ruth"));
53+
System.out.println(list.find("Karl"));
54+
System.out.println(list.find("John"));
55+
System.out.println(list.find("James"));
56+
57+
list.deleteFront();
58+
System.out.println(list.find("John"));
59+
list.addFront("Oliver");
60+
System.out.println(list.find("Ruth"));
61+
list.addAfter(list.find("Ruth").get(), "Sam");
62+
System.out.println(list.toString());
63+
64+
LinkedListNode<Integer> x = new LinkedListNode<>(5, null);
965
}
1066
}

src/main/java/com/packt/datastructuresandalg/lesson2/linkedlist/LinkedListNode.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
public class LinkedListNode<V> {
66
private V value;
7-
private Optional<LinkedListNode<V>> next;
7+
private LinkedListNode<V> next;
88

9-
public LinkedListNode(V value, Optional<LinkedListNode<V>> next) {
9+
public LinkedListNode(V value, LinkedListNode<V> next) {
1010
this.value = value;
1111
this.next = next;
1212
}
1313

14+
public Optional<LinkedListNode<V>> getNext() {
15+
return Optional.ofNullable(next);
16+
}
17+
1418
public V getValue() {
1519
return value;
1620
}
@@ -20,12 +24,15 @@ public LinkedListNode setValue(V value) {
2024
return this;
2125
}
2226

23-
public Optional<LinkedListNode<V>> getNext() {
24-
return next;
25-
}
26-
27-
public LinkedListNode setNext(Optional<LinkedListNode<V>> next) {
27+
public LinkedListNode setNext(LinkedListNode<V> next) {
2828
this.next = next;
2929
return this;
3030
}
31+
32+
@Override
33+
public String toString() {
34+
return "LinkedListNode{" +
35+
"value=" + value +
36+
'}';
37+
}
3138
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.packt.datastructuresandalg.lesson2.queue;
2+
3+
import com.packt.datastructuresandalg.lesson2.linkedlist.DblLinkedListNode;
4+
5+
import java.util.Optional;
6+
7+
public class Queue<V> {
8+
private DblLinkedListNode<V> head;
9+
private DblLinkedListNode<V> tail;
10+
11+
public Queue() {
12+
head = null;
13+
tail = null;
14+
}
15+
16+
public void enqueue(V item) {
17+
DblLinkedListNode<V> node = new DblLinkedListNode<>(item, null, tail);
18+
Optional.ofNullable(tail).ifPresent(n -> n.setNext(node));
19+
tail = node;
20+
if(head == null) head = node;
21+
}
22+
23+
public Optional<V> dequeue() {
24+
Optional<DblLinkedListNode<V>> node = Optional.ofNullable(head);
25+
head = node.flatMap(DblLinkedListNode::getNext).orElse(null);
26+
Optional.ofNullable(head).ifPresent(n -> n.setPrevious(null));
27+
if (head == null) tail = null;
28+
return node.map(DblLinkedListNode::getValue);
29+
}
30+
31+
public static void main(String[] args) {
32+
Queue<String> q = new Queue<>();
33+
q.enqueue("one");
34+
q.enqueue("two");
35+
q.enqueue("three");
36+
37+
System.out.println(q.dequeue());
38+
System.out.println(q.dequeue());
39+
System.out.println(q.dequeue());
40+
System.out.println(q.dequeue());
41+
42+
q.enqueue("one");
43+
44+
System.out.println(q.dequeue());
45+
System.out.println(q.dequeue());
46+
47+
}
48+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.packt.datastructuresandalg.lesson2.queue;
2+
3+
import java.util.Optional;
4+
5+
public class QueueArray<V> {
6+
private V[] array;
7+
private int headPtr = 0;
8+
private int tailPtr = 0;
9+
private boolean full = false;
10+
11+
public boolean enqueueSafe(V item) {
12+
if (!full) {
13+
array[tailPtr] = item;
14+
tailPtr = (tailPtr + 1) % array.length;
15+
this.full = tailPtr == headPtr;
16+
return true;
17+
}
18+
return false;
19+
}
20+
21+
public Optional<V> dequeueSafe() {
22+
if (headPtr != tailPtr || full) {
23+
Optional<V> item = Optional.of(array[headPtr]);
24+
headPtr = (headPtr + 1) % array.length;
25+
this.full = false;
26+
return item;
27+
} else return Optional.empty();
28+
}
29+
30+
31+
public QueueArray(int capacity) {
32+
array = (V[]) new Object[capacity];
33+
}
34+
35+
public void enqueue(V item) {
36+
array[tailPtr] = item;
37+
tailPtr = (tailPtr + 1) % array.length;
38+
}
39+
40+
public Optional<V> dequeue() {
41+
if (headPtr != tailPtr) {
42+
Optional<V> item = Optional.of(array[headPtr]);
43+
headPtr = (headPtr + 1) % array.length;
44+
return item;
45+
} else return Optional.empty();
46+
}
47+
48+
49+
public static void main(String[] args) {
50+
QueueArray<String> q = new QueueArray<>(5);
51+
52+
System.out.println(q.enqueueSafe("one"));
53+
System.out.println(q.enqueueSafe("two"));
54+
System.out.println(q.enqueueSafe("three"));
55+
System.out.println(q.enqueueSafe("four"));
56+
System.out.println(q.enqueueSafe("five"));
57+
System.out.println(q.enqueueSafe("six"));
58+
59+
System.out.println(q.dequeue());
60+
System.out.println(q.dequeue());
61+
System.out.println(q.dequeue());
62+
System.out.println(q.dequeue());
63+
System.out.println(q.dequeue());
64+
System.out.println(q.dequeue());
65+
66+
System.out.println(q.enqueueSafe("one"));
67+
System.out.println(q.enqueueSafe("two"));
68+
System.out.println(q.enqueueSafe("three"));
69+
70+
System.out.println(q.dequeue());
71+
System.out.println(q.dequeue());
72+
System.out.println(q.dequeue());
73+
System.out.println(q.dequeue());
74+
75+
System.out.println(q.enqueueSafe("one"));
76+
System.out.println(q.enqueueSafe("two"));
77+
System.out.println(q.enqueueSafe("three"));
78+
79+
System.out.println(q.dequeue());
80+
System.out.println(q.dequeue());
81+
System.out.println(q.dequeue());
82+
System.out.println(q.dequeue());
83+
}
84+
}

src/main/java/com/packt/datastructuresandalg/lesson2/sorting/BobbleSort.java renamed to src/main/java/com/packt/datastructuresandalg/lesson2/sorting/BubbleSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Arrays;
44

5-
public class BobbleSort {
5+
public class BubbleSort {
66

77
public void sort(int[] numbers) {
88
for (int i = 1; i < numbers.length; i++) {
@@ -48,7 +48,7 @@ private void swap(int[] numbers, int j, int k) {
4848
}
4949

5050
public static void main(String[] args) {
51-
BobbleSort bubbleSort = new BobbleSort();
51+
BubbleSort bubbleSort = new BubbleSort();
5252
int[] numbers = new int[]{2, 5, 7, 2, 4, 2, 8, 1, 0};
5353
int[] numbers1 = new int[]{2, 5, 7, 2, 4, 2, 8, 1, 0};
5454
int[] numbers2 = new int[]{2, 5, 7, 2, 4, 2, 8, 1, 0};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.packt.datastructuresandalg.lesson2.stack;
2+
3+
import com.packt.datastructuresandalg.lesson2.linkedlist.LinkedListNode;
4+
5+
import java.util.Optional;
6+
7+
public class Stack<V> {
8+
private LinkedListNode<V> head;
9+
10+
public void push(V item) {
11+
head = new LinkedListNode<V>(item, head);
12+
}
13+
14+
public Optional<V> pop() {
15+
Optional<LinkedListNode<V>> node = Optional.ofNullable(head);
16+
head = node.flatMap(LinkedListNode::getNext).orElse(null);
17+
return node.map(LinkedListNode::getValue);
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.packt.datastructuresandalg.lesson2.stack;
2+
3+
import java.util.Optional;
4+
5+
public class StackArray<V> {
6+
private V[] array;
7+
private int headPtr = 0;
8+
9+
public StackArray(int capacity) {
10+
array = (V[]) new Object[capacity];
11+
}
12+
13+
public void push(V item) {
14+
array[headPtr++] = item;
15+
}
16+
17+
public Optional<V> pop() {
18+
if (headPtr > 0) return Optional.of(array[--headPtr]);
19+
else return Optional.empty();
20+
}
21+
}

0 commit comments

Comments
 (0)