Skip to content

Commit 55a1249

Browse files
committed
algorithms lesson #7
1 parent de26431 commit 55a1249

3 files changed

Lines changed: 136 additions & 3 deletions

File tree

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1-
package lesson7;public class DynamiListDemo {
1+
package lesson7;
2+
3+
public class DynamiListDemo {
4+
public static void main(String[] args) {
5+
DynamicList dynamicList = new DynamicList();
6+
dynamicList.push(12);
7+
dynamicList.push(8);
8+
dynamicList.push(5);
9+
dynamicList.push(2);
10+
System.out.println(dynamicList);
11+
for (int i = 0; i < 5; i++) {
12+
System.out.println(dynamicList.pop() + " <- " + dynamicList);
13+
}
14+
dynamicList.offer(12);
15+
dynamicList.offer(8);
16+
dynamicList.offer(5);
17+
dynamicList.offer(2);
18+
System.out.println(dynamicList);
19+
}
220
}
Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,83 @@
1-
package lesson7;public class DynamicList {
1+
package lesson7;
2+
3+
public class DynamicList {
4+
private int size;
5+
private Node first;
6+
private Node last;
7+
8+
public void push(Integer value) {
9+
if (first == null) { // push 1st element
10+
first = new Node(value, null, null);
11+
} else if (last == null) { // push 2nd element
12+
last = first;
13+
first = new Node(value, null, last);
14+
first.pred = first;
15+
last.pred = first;
16+
} else { // push 3rd and other(s)
17+
Node newFirst = new Node(value, null, first);
18+
first.pred = newFirst;
19+
first = newFirst;
20+
}
21+
size++;
22+
}
23+
24+
public void offer(Integer value) {
25+
if (first == null) { // offer 1st element
26+
first = new Node(value, null, null);
27+
} else if (last == null) { // offer 2nd element
28+
last = new Node(value, first, null);
29+
first.next = last;
30+
} else { // offer 3rd and other(s)
31+
Node newLast = new Node(value, last, null);
32+
last.next = newLast;
33+
last = newLast;
34+
}
35+
size++;
36+
}
37+
38+
// TODO implement poll()
39+
40+
public Integer pop() {
41+
Integer value = null;
42+
if (first != null) {
43+
value = first.value;
44+
first = first.next;
45+
if (first != null) {
46+
first.pred = null;
47+
if (first.next == null) {
48+
last = null;
49+
}
50+
}
51+
size--;
52+
}
53+
return value;
54+
}
55+
56+
@Override
57+
public String toString() {
58+
StringBuffer sb = new StringBuffer("[");
59+
if (first != null) {
60+
Node cursor = first;
61+
do {
62+
sb.append(cursor.value);
63+
cursor = cursor.next;
64+
if (cursor != null) {
65+
sb.append(", ");
66+
}
67+
} while (cursor != null);
68+
}
69+
return sb.append("]").toString();
70+
}
71+
72+
private class Node {
73+
Integer value;
74+
Node pred;
75+
Node next;
76+
77+
public Node(Integer value, Node pred, Node next) {
78+
this.value = value;
79+
this.pred = pred;
80+
this.next = next;
81+
}
82+
}
283
}
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
1-
package lesson7;public class Lesson7 {
1+
package lesson7;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* Algorithms. Lesson #7. Stack and Queue
7+
*
8+
* @author Sergey Iryupin
9+
* @version 11 Aug 2023
10+
*/
11+
12+
public class Lesson7 {
13+
public static void main(String[] args) {
14+
LinkedList<Integer> stackQueue = new LinkedList<>();
15+
// LinkedList like Stack
16+
stackQueue.push(5);
17+
System.out.println(stackQueue);
18+
stackQueue.push(12);
19+
System.out.println(stackQueue);
20+
stackQueue.push(-2);
21+
System.out.println("like Stack: " + stackQueue);
22+
for (int i = 0; i < 3; i++) {
23+
System.out.println(stackQueue.pop() + " <- " + stackQueue);
24+
}
25+
// LinkedList like Queue
26+
stackQueue.offer(8);
27+
System.out.println(stackQueue);
28+
stackQueue.offer(-5);
29+
System.out.println(stackQueue);
30+
stackQueue.offer(11);
31+
System.out.println("like Queue: " + stackQueue);
32+
for (int i = 0; i < 3; i++) {
33+
System.out.println(stackQueue.poll() + " <- " + stackQueue);
34+
}
35+
}
236
}

0 commit comments

Comments
 (0)