Last active
April 19, 2018 19:14
-
-
Save Parabellum1905y/f0305d0e3cf4c5de8dd0825db9bd934b to your computer and use it in GitHub Desktop.
stack revereing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Optional; | |
class Stack<T>{ | |
private int capacity = 32; | |
private Object[] container = new Object[capacity]; | |
private int size = 0; | |
boolean isEmpty(){return size == 0;} | |
public Stack(int capacity){ | |
this.capacity = capacity; | |
} | |
public Stack(){} | |
public void push(T o) { | |
if (size == capacity) { | |
capacity += 32; | |
container = Arrays.copyOf(container, capacity, Object[].class); | |
} | |
container[size++] = o; | |
} | |
public Optional<T> pop(){ | |
if (isEmpty()) return Optional.empty(); | |
if (size + 32 < capacity){ | |
capacity -= 32; | |
container = Arrays.copyOf(container, capacity, Object[].class); | |
} | |
return Optional.of ((T) container[(size--)-1]); | |
} | |
public Optional<T> top(){ | |
if (isEmpty()) return Optional.empty(); | |
return Optional.of((T) container[size-1]); | |
} | |
public Stack<T> reverse(){ | |
addToTop(); | |
return this; | |
} | |
private void addToEnd(T o){ | |
if (isEmpty()){ | |
push(o); | |
}else { | |
Optional<T> topVal = pop(); | |
addToEnd(o); | |
push(topVal.get()); | |
} | |
} | |
private void addToTop(){ | |
if (!isEmpty()){ | |
Optional<T> topVal = pop(); | |
addToTop(); | |
addToEnd(topVal.get()); | |
} | |
} | |
@Override | |
public String toString(){ | |
StringBuffer sb = new StringBuffer(); | |
sb.append("capacity :").append (container.length).append(", size :").append (size).append(", ["); | |
for (int i = size - 1; i > -1; i--){ | |
sb.append(container[i]); | |
if (i > 0) sb.append(","); | |
} | |
sb.append("]"); | |
return sb.toString(); | |
} | |
} | |
class Queue<T> { | |
private Stack<T> stack1 = new Stack<>(); | |
private Stack<T> stack2 = new Stack<>(); | |
public Queue (){} | |
public void enqueue (T o){ | |
stack1.push(o); | |
} | |
public Optional<T> dequeue (){ | |
if (stack2.isEmpty()){ | |
if (stack1.isEmpty()) return Optional.empty(); | |
while (!stack1.isEmpty()) stack2.push(stack1.pop().get()); | |
} | |
return stack2.pop(); | |
} | |
public boolean isEmpty (){ | |
return stack2.isEmpty() && stack1.isEmpty(); | |
} | |
} | |
class StackOverQueueOverStack<T>{ | |
private Queue<T> q1 = new Queue<>(); | |
private Queue<T> q2 = new Queue<>(); | |
boolean isEmpty(){return q1.isEmpty();} | |
public StackOverQueueOverStack(){} | |
public void push(T o) { | |
q1.enqueue(o); | |
} | |
public Optional<T> pop(){ | |
if (isEmpty()) return Optional.empty(); | |
T last = q1.dequeue().get(); | |
while (!q1.isEmpty()){ | |
q2.enqueue(last); | |
last = q1.dequeue().get(); | |
} | |
while (!q2.isEmpty()) q1.enqueue(q2.dequeue().get()); | |
return Optional.of(last); | |
} | |
} | |
public class Main{ | |
public static void main(String[] args){ | |
Stack <Integer> a = new Stack<>(); | |
a.push(1); | |
a.push(2); | |
a.pop(); | |
a.push(3); | |
a.push(4); | |
a.push(5); | |
a.pop(); | |
a.push(3); | |
a.push(4); | |
a.push(5); | |
System.out.println(a); | |
for (int i = 0; i < 35; i ++){ | |
a.push(i + 10); | |
} | |
System.out.println(a); | |
a.reverse(); | |
System.out.println(a); | |
for (int i = 0; i < 35; i ++){ | |
a.pop(); | |
} | |
System.out.println(a); | |
Queue<Integer> b = new Queue<>(); | |
b.enqueue(1); | |
b.enqueue(2); | |
b.enqueue(3); | |
b.enqueue(4); | |
b.enqueue(5); | |
b.enqueue(6); | |
b.dequeue(); | |
b.dequeue(); | |
while (!b.isEmpty()){ | |
System.out.println(b.dequeue().get()); | |
} | |
StackOverQueueOverStack<Integer> stack = new StackOverQueueOverStack<>(); | |
stack.push(1); | |
stack.push(2); | |
stack.push(3); | |
//stack.pop(); | |
while (!stack.isEmpty()) System.out.println( stack.pop() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment