-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFibonacciWithStackDemo.java
107 lines (82 loc) · 1.92 KB
/
FibonacciWithStackDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ds_008_recursion;
public class FibonacciWithStackDemo {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
System.out.printf("Fib(%2d) = %3d\n", i, fibonacci(i));
}
System.out.printf("Fib(43) = %d\n", fibonacci(43)); // F(43): 701408733
System.out.printf("Fib(80) = %d\n", fibonacci(80)); // F(80): 37889062373143906
}
private static long fibonacci(int N) {
if(N == 0 || N == 1) {
return 1;
}
SimpleStack stack = new SimpleStack();
stack.push(1); // f(0)
stack.push(1); // f(1)
while(N != 1) {
long prev1 = stack.pop();
long prev2 = stack.pop();
long fib = prev1 + prev2;
stack.push(prev1);
stack.push(fib);
N--;
}
return stack.pop();
}
private static class SimpleStack {
private Node START_SENTINEL;
private long size;
public SimpleStack() {
START_SENTINEL = new Node(Long.MIN_VALUE);
size = 0;
}
public void push(long value) {
Node newNode = new Node(value);
if(isEmpty()) {
START_SENTINEL.next = newNode;
} else {
newNode.next = START_SENTINEL.next;
START_SENTINEL.next = newNode;
}
size++;
}
public Long pop() {
if(isEmpty()) {
return null;
}
Node popped = START_SENTINEL.next;
START_SENTINEL.next = popped.next;
size--;
return popped.data;
}
public Long peek() {
if(isEmpty()) {
return null;
}
return START_SENTINEL.next.data;
}
private boolean isEmpty() {
return size == 0;
}
private class Node {
private long data;
private Node next;
public Node(long data) {
this.data = data;
this.next = null;
}
}
private void printStack() {
System.out.print("Stack: ");
if(isEmpty()) {
System.out.print("EMPTY");
} else {
for(Node temp = START_SENTINEL.next; temp != null; temp = temp.next) {
System.out.printf("%d ", temp.data);
}
}
System.out.print("\n");
}
}
}