今日学习总结:
自己创建一个栈
/**
* Created by novax_000 on 2016/4/11.
*/
public class MiniStack<T> {
private int size;
private Object[] array = new Object[4];
public boolean isEmpty()
{
if(size==0)
{
return true;
}else
{
return false;
}
}
public int size()
{
return size;
}
public void expandCapacity()
{
Object[] newArray = new Object[size*2];
System.arraycopy(array,0,newArray,0,size);
array = newArray;
}
public void push(T t)
{
array[size]=t;
size++;
if(size==array.length)
{
expandCapacity();
}
}
public T peek()
{
if(isEmpty())
{
return null;
}else
{
return (T) array[size-1];
}
}
public T pop(){
if(isEmpty())
{
return null;
}else
{
T t = peek();
array[size-1]=null;
size--;
return t;
}
}
}
自己创建一个队列
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by novax_000 on 2016/4/13.
*/
public class MiniQueue<T> {
private int size;
public boolean isEmpty() {
if (size == 0) {
return true;
} else {
return false;
}
}
public int size(){
return size;
}
private ListNode<T> head;
private ListNode<T> last;
public MiniQueue(){
super();
head = new ListNode<T>(null,null);
last = head;
}
public void offer(T t)
{
ListNode<T> node = new ListNode<T>(t,null);
last.next = node;
last = node;
size++;
}
public T peek()
{
if(isEmpty())
{
return null;
}else
{
return head.next.value;
}
}
public T poll()
{
if(isEmpty())
{
return null;
}else {
ListNode<T> p = head.next;
head.next = p.next;
size--;
if(size==0)
{
last = head;
}
return p.value;
}
}
}
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by novax_000 on 2016/4/13.
*/
public class TwoQueueBeOneStack {
public Queue<Integer> queue1 = new LinkedList<Integer>();
public Queue<Integer> queue2 = new LinkedList<Integer>();
private int size;
// Push element x onto stack.
public void push(int x) {
if(empty()||!queue1.isEmpty())
{
queue1.offer(x);
}else
{
queue2.offer(x);
}
size++;
}
// Removes the element on top of the stack.
public void pop() {
if(!queue1.isEmpty())
{
while(queue1.size()>1)
{
queue2.offer(queue1.poll());
}
queue1.poll();
}else {
while(queue2.size()>1)
{
queue1.offer(queue2.poll());
}
queue2.poll();
}
size--;
}
// Get the top element.
public int top() {
if(!queue1.isEmpty())
{
while(queue1.size()>1)
{
queue2.offer(queue1.poll());
}
int k=queue1.poll();
queue2.offer(k);
return k;
}else {
while(queue2.size()>1)
{
queue1.offer(queue2.poll());
}
int k=queue2.poll();
queue1.offer(k);
return k;
}
}
// Return whether the stack is empty.
public boolean empty() {
return size==0?true:false;
}
}
用两个栈实现一个队列
import java.util.Stack;
/**
* Created by novax_000 on 2016/4/13.
*/
public class TwoStackBeOneQueue {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
private int size;
// Push element x to the back of queue.
public void push(int x) {
stack1.push(x);
size++;
}
// Removes the element from in front of queue.
public void pop() {
if(!stack2.isEmpty())
{
stack2.pop();
}else
{
while(!stack1.isEmpty())
{
stack2.push(stack1.pop());
}
stack2.pop();
}
size--;
}
// Get the front element.
public int peek() {
if(!stack2.isEmpty())
{
return stack2.peek();
}else
{
while(!stack1.isEmpty())
{
stack2.push(stack1.pop());
}
return stack2.peek();
}
}
// Return whether the queue is empty.
public boolean empty() {
if(size==0)
{
return true;
}else
{
return false;
}
}
}
获得栈的最小元素
import java.util.Stack;
/**
* Created by novax_000 on 2016/4/13.
*/
public class MinStack {
public Stack<Integer> stack = new Stack<Integer>();
public Stack<Integer> minStack = new Stack<Integer>();
public void push(int x) {
if(stack.isEmpty())
{
stack.push(x);
minStack.push(x);
}else
{
stack.push(x);
int min = minStack.peek();
minStack.push(Math.min(x,min));
}
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}