package Java_Interview_Book;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
/**
* 主要包含如何实现栈和队列,用数组和链表都可以。
* 队列和栈之间的转换可以参考剑指offer(5)用两个栈实现队列
* @author hexiaoli
* 大拿Fan0628拿ArrayList做的
*/
/**
* 用数组实现栈
* @author hexiaoli
*
* @param <E>
*/
class Stack_Arrays<E>{
public Object[] stack;
public int size = 0;//数组中存储元素的个数
public Stack_Arrays() {
stack = new Object[10];//初始长度设为10
}
//判断栈是否为空
public boolean isEmpty() {
return size == 0;
}
@SuppressWarnings("unchecked")
public E peek() {
if(this.isEmpty()) {
return null;
}
return (E)stack[size];
}
public E pop() {
E pop_value =peek();
stack[size] = null;
size --;
return pop_value;
}
public E push(E push_value) {
size++;
ensureCapacity(size);//检查容量
stack[size] = push_value;
return push_value;
}
//判断容器是否已满,满的话扩容
public void ensureCapacity(int size) {
int length = stack.length;
if(size > length) {//如果数组已满
int newLength =length+10;//每次扩充的容量
//System.arraycopy(),在原数组上操作,效率最高
//Arrays.copyOf(),新开辟了一个空间
//.clone()和for
// 数组元素个数不是很大时,使用for来复制数组,时间性能是最好的,clone次之
//其次是System.arraycopy,最差的是Arrays.copyof。
//数组元素个数很大时,使用System.arraycopy复制数组,时间性能是最好的
//clone次之,其次是Arrays.copyof,最差的就是for。
stack = Arrays.copyOf(stack, newLength);
}
}
}
/**
* 用链表实现栈
* @author hexiaoli
*
* @param <E>
*/
class Node_stack<E>{
Node_stack<E> next =null;
E data;
public Node_stack(E data) {
this.data = data ;
}
}
class Stack_Link<E>{
Node_stack<E> head = null;
public int length = 0;
public boolean isEmpty() {
return head == null;
}
public E peek() {
if (this.isEmpty()) {
return null;
}
return head.data;
}
public E pop() {
if (this.isEmpty()) {
return null;
}
E data = head.data;
head = head.next;
length--;
return data;
}
public E push(E data) {
Node_stack <E> newNode= new Node_stack <E>(data);
newNode.data = data;
head = newNode;
length++;
return data;
}
}
/**
* 用o(1)时间复杂度求栈中最小元素
* @author hexiaoli
* 用空间换时间,一个栈存储数据,另一个栈存储栈的最小元素
*/
class FindMinimumNumberinStack{
Stack_Link<Integer> elem = new Stack_Link<>();
Stack_Link<Integer> min= new Stack_Link<>();
public void push(int data) {
elem.push(data);
if(min.isEmpty()) {
min.push(data);
}else {
if(data<min.peek()) {
min.push(data);
}
}
}
public int pop() {
int topData = elem.peek();
elem.pop();
if(topData == this.min()) {
min.pop();
}
return topData;
}
public int min() {
if(min.isEmpty()) {
return Integer.MAX_VALUE;
}else {
return min.peek();
}
}
}
/**
* 用链表实现队列
* @author hexiaoli
*/
class Node_Queue<E>{
Node_Queue<E> next =null;
E data;
public Node_Queue(E data) {
this.data = data ;
}
}
class Queue_Node<E>{
public Node_Queue head;
public Node_Queue tail;
public Queue_Node() {
head = null;
tail = null;
}
public boolean isEmpty() {
if(head == tail) {
return true;
}
return false;
}
public void put(E data) {
Node_Queue<E> newNode = new Node_Queue<E>(data);
if(head == null && tail == null) {
head = tail = newNode;
}else {
tail.next = newNode;
tail = newNode;
}
}
public E pop() {
if(this.isEmpty()) {
return null;
}
E data = (E) head.data;
head = head.next;
return data;
}
public int size() {
Node_Queue<E> temp = head;
int count = 0;
while(temp !=null) {
count++;
temp = temp.next;
}
return count;
}
}
/**
* 用数组实现队列
* @author hexiaoli
*/
class Queue_Linkedlist<E>{
public LinkedList<E> list = new LinkedList<>();
public int size = 0;
public synchronized void put(E e) {
list.addLast(e);
size++;
}
public synchronized E pop() {
size--;
return list.removeFirst();
}
public synchronized boolean isEmpty() {
return size==0;
}
public synchronized int size() {
return size;
}
}
public class StackAndQueue {
public static void main(String[] args) {
// Stack_Arrays<Integer> sa = new Stack_Arrays<>();
// sa.push(5);
// sa.push(2);
// sa.push(3);
// System.out.println("stack大小:"+sa.size);
// System.out.println("栈顶元素为:"+sa.peek());
// System.out.println("弹出栈顶元素:"+sa.pop());
// Stack_Link<Integer> sl = new Stack_Link<>();
// sl.push(5);
// sl.push(2);
// sl.push(7);
// System.out.println("stack大小:"+sl.length);
// System.out.println("栈顶元素为:"+sl.peek());
// System.out.println("弹出栈顶元素:"+sl.pop());
// System.out.println("stack大小:"+sl.length);
// FindMinimumNumberinStack fmns =new FindMinimumNumberinStack();
// fmns.push(5);
// fmns.push(2);
// fmns.push(7);
// fmns.push(4);
// System.out.println("栈中最小元素"+fmns.min());
// Queue_Node<Integer> ql = new Queue_Node<>();
// ql.put(3);
// ql.put(2);
// ql.put(7);
// System.out.println("队列大小:"+ql.size());
// System.out.println("队列首元素为:"+ql.pop());
// System.out.println("队列大小:"+ql.size());
// Queue_Linkedlist<Integer> qll = new Queue_Linkedlist<>();
// qll.put(2);
// qll.put(1);
// qll.put(7);
// System.out.println("队列大小:"+qll.size());
// System.out.println("队列首元素为:"+qll.pop());
}
}
//大拿实现的stack
//import java.util.ArrayList;
//class Stack_Arrays<E> {
// private ArrayList<E> stack;
//
// public Stack_Arrays(ArrayList<E> list) {
// stack = new ArrayList<>();
// }
//
// public int size() {
// return stack.size();
// }
//
// public E peek() {
// if (stack.size() == 0) {
// return null;
// } else {
// return stack.get(stack.size() - 1);
// }
// }
//
// public E pop() {
// E pop_value = peek();
// stack.remove(stack.size() - 1);
// return pop_value;
// }
//
// public void push(E push_value) {
// stack.add(push_value);
// }
//}
//
//public class Main {
// public static void main(String[] args) {
// ArrayList<Integer> list = new ArrayList<>();
// Stack_Arrays<Integer> sa = new Stack_Arrays<>(list);
// sa.push(1);
// sa.push(2);
// sa.push(3);
// sa.push(4);
// sa.push(5);
// sa.push(6);
// sa.push(7);
// sa.push(8);
// sa.push(9);
// sa.push(10);
// sa.push(11);
// sa.push(12);
// sa.pop();
// System.out.println("栈的大小:" + sa.size());
// System.out.println("栈顶元素为:" + sa.peek());
// System.out.println("弹出栈顶元素:" + sa.pop());
// }
//}
Java程序员面试笔记_第八章_数据结构和算法_栈和队列
猜你喜欢
转载自blog.csdn.net/hxl0925/article/details/89429350
今日推荐
周排行