Java代码:
package day02;
public class Code01_Array2Stack {
public static class ArrayStack{
private Integer[] arr;
private Integer index;
public ArrayStack(int initSize) {
if(initSize < 0) {
throw new IllegalArgumentException("初始化长度小于0");
}
arr = new Integer[initSize];
index = 0;
}
public Integer peek() {
if(index == 0) {
return null;
}
return arr[index - 1];
}
public void push(int obj) {
if(index == arr.length) {
throw new ArrayIndexOutOfBoundsException("栈满了");
}
arr[index++] = obj;
}
public Integer pop() {
if (index == 0) {
throw new ArrayIndexOutOfBoundsException("栈空了");
}
return arr[--index];
}
}
public static void main(String[] args) {
ArrayStack as = new ArrayStack(5);
as.push(1);
as.push(2);
as.push(3);
as.push(4);
as.push(5);
//as.push(5);
System.out.println(as.peek());
System.out.println(as.pop());
}
}
Java代码:
package day02;
public class Code02_Array2Queue {
public static class ArrayQueue{
private Integer[] arr;
private Integer size;
private Integer start;
private Integer end;
public ArrayQueue(int initSize) {
if(initSize < 0) {
throw new IllegalArgumentException("初始化长度小于0");
}
arr = new Integer[initSize];
size = 0;
start = 0;
end = 0;
}
public Integer peek() {
if(size == 0) {
return null;
}
return arr[start];
}
public void push(int obj) {
if(size == arr.length) {
throw new ArrayIndexOutOfBoundsException("队列满了");
}
size++;
arr[end] = obj;
end = (end == arr.length) ? 0 : (end + 1); //设定end
}
public Integer poll() {
if(size == 0) {
throw new ArrayIndexOutOfBoundsException("队列空了");
}
size--;
int tmp = start;
start = (start == arr.length) ? 0 : start + 1;
return arr[tmp];
}
}
public static void main(String[] args) {
ArrayQueue aq = new ArrayQueue(3);
aq.push(1);
aq.push(2);
aq.push(3);
//aq.push(4);
System.out.println(aq.peek());
System.out.println(aq.poll());
}
}
package day02;
import java.util.LinkedList;
import java.util.Queue;
public class Code03_TwoQueues2Stack {
public static class TwoQueuesStack{
private Queue<Integer> data;
private Queue<Integer> help;
//构造函数
public TwoQueuesStack() {
data = new LinkedList<Integer>();
help = new LinkedList<Integer>();
}
public void push(int obj) {
data.add(obj);
}
public int peek() {
if(data.isEmpty()) {
throw new RuntimeException("栈空了");
}
while(data.size() != 1) {
help.add(data.poll());
}
int res = data.poll();
help.add(res);
swap();
return res;
}
public int pop() {
if(data.isEmpty()) {
throw new RuntimeException("栈空了");
}
while(data.size() != 1) {
help.add(data.poll());
}
int res = data.poll();
swap();
return res;
}
private void swap() {
Queue<Integer> tmp = help;
help = data;
data = tmp;
}
}
public static void main(String[] args) {
TwoQueuesStack tqs = new TwoQueuesStack();
tqs.push(1);
tqs.push(2);
tqs.push(3);
System.out.println(tqs.peek());
System.out.println(tqs.pop());
System.out.println(tqs.pop());
System.out.println(tqs.pop());
//tqs.pop();
}
}
package day02;
import java.util.Stack;
public class Code04_TwoStacks2Queue {
public static class TwoStacksQueue{
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStacksQueue() {
stackPush = new Stack<Integer>();
stackPop = new Stack<Integer>();
}
public void push(int obj) {
stackPush.push(obj);
}
public int poll() {
if(stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("队列空了");
}else if(stackPop.empty()) {
while(!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public int peek() {
if(stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("队列空了");
}else if(stackPop.empty()) {
while(!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
}
public static void main(String[] args) {
TwoStacksQueue twq = new TwoStacksQueue();
twq.push(1);
twq.push(2);
twq.push(3);
System.out.println(twq.peek());
System.out.println(twq.poll());
System.out.println(twq.poll());
System.out.println(twq.poll());
}
}
实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
pop
、push
、getMin
操作的时间复杂度都是O(1)
Java代码:
package day02;
import java.util.Stack;
public class Code05_GetMinStack {
public static class MyStack{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int obj) {
if(this.stackMin.isEmpty()) {
this.stackMin.push(obj);
}else if(obj < this.getmin()) {
this.stackMin.push(obj);
}else {
int newMin = this.stackMin.peek();
this.stackMin.push(newMin);
}
this.stackData.push(obj);
}
public int pop() {
if(this.stackData.isEmpty()) {
throw new RuntimeException("栈空了");
}
this.stackMin.pop();
return this.stackData.pop();
}
public int getmin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("栈空了");
}
return this.stackMin.peek();
}
}
public static void main(String[] args) {
MyStack ms = new MyStack();
ms.push(3);
ms.push(1);
ms.push(2);
System.out.println(ms.getmin());
System.out.println(ms.pop());
System.out.println(ms.getmin());
}
}
package day02;
import java.util.Stack;
public class Code05_GetMinStack {
public static class MyStack{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int obj) {
if(this.stackMin.isEmpty()) {
this.stackMin.push(obj);
}else if(obj <= this.getmin()) {
this.stackMin.push(obj);
}
this.stackData.push(obj);
}
public int pop() {
if(this.stackData.isEmpty()) {
throw new RuntimeException("栈空了");
}
int value = this.stackData.pop();
if(value == this.getmin()) {
this.stackMin.pop();
}
return value;
}
public int getmin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("栈空了");
}
return this.stackMin.peek();
}
}
public static void main(String[] args) {
MyStack ms = new MyStack();
ms.push(3);
ms.push(1);
ms.push(2);
System.out.println(ms.getmin());
System.out.println(ms.pop());
System.out.println(ms.getmin());
}
}
宠物、狗和猫的类如下:
public class Pet {
private String type;
public Pet(String type) {
this.type = type;
}
public String getPetType() {
return this.type;
}
}
public class Dog extends Pet {
public Dog() {
super("dog");
}
}
public class Cat extends Pet {
public Cat() {
super("cat");
}
}
add
方法将 cat类
或 dog类
的实例放入队列中;pollAll
方法,将队列中所有的实例按照进队列的先后顺序依次弹出;pollDog
方法,将队列中 dog类
的实例按照进队列的先后顺序依次弹出;pollCat
方法,将队列中 cat类
的实例按照进队列的先后顺序依次弹出;isEmpty
方法,检查队列中是否还有 dog
或 cat
的实例;isDogEmpty
方法,检查队列中是否有 dog类
的实例;isCatEmpty
方法,检查队列中是否有 cat类
的实例Java代码:
package day02;
import java.util.LinkedList;
import java.util.Queue;
public class Code06_DogCatQueue {
//宠物类
public static class Pet {
private String type; //类型(私有变量)
public Pet(String type) {
this.type = type;
}
public String getPetType() {
return this.type;
}
}
//狗类继承宠物类
public static class Dog extends Pet {
public Dog() {
super("dog"); //调用父类的有参构造方法
}
}
//猫类继承宠物类
public static class Cat extends Pet {
public Cat() {
super("cat");
}
}
//进队列的宠物
public static class EnterQueuePet {
private Pet pet;
private long count;
//初始化
public EnterQueuePet(Pet pet, long count) {
this.pet = pet;
this.count = count;
}
//获取宠物
public Pet getPet() {
return this.pet;
}
//获取宠物进队列时的标号
public long getCount() {
return this.count;
}
//获取宠物类型
public String getPetType() {
return this.pet.getPetType();
}
}
//狗猫队列类
public static class DogCatQueue{
private Queue<EnterQueuePet> dogQ; //狗队列
private Queue<EnterQueuePet> catQ; //猫队列
private long count;
//构造函数
public DogCatQueue() {
this.dogQ = new LinkedList<EnterQueuePet>();
this.catQ = new LinkedList<EnterQueuePet>();
this.count = 0;
}
public void add(Pet pet) {
if(pet.getPetType().equals("dog")) {
this.dogQ.add(new EnterQueuePet(pet, this.count++));
}else if(pet.getPetType().equals("cat")) {
this.catQ.add(new EnterQueuePet(pet, this.count++));
}else {
throw new RuntimeException("不是猫或狗");
}
}
public Pet pollAll(){
if(!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
if(this.dogQ.peek().getCount() < this.catQ.peek().getCount()) {
return this.dogQ.poll().getPet();
}else {
return this.catQ.poll().getPet();
}
}else if(!this.dogQ.isEmpty()) {
return this.dogQ.poll().getPet();
}else if(!this.catQ.isEmpty()) {
return this.catQ.poll().getPet();
} else {
throw new RuntimeException("err, queue is empty!");
}
}
public Dog pollDog() {
if (!this.isEmpty()) {
return (Dog) this.dogQ.poll().getPet();
} else {
throw new RuntimeException("狗队列空了");
}
}
public Cat pollCat() {
if (!this.isCatQueueEmpty()) {
return (Cat) this.catQ.poll().getPet();
} else {
throw new RuntimeException("猫队列空了");
}
}
public boolean isEmpty() {
return this.dogQ.isEmpty() && this.catQ.isEmpty();
}
public boolean isDogQueueEmpty() {
return this.dogQ.isEmpty();
}
public boolean isCatQueueEmpty() {
return this.catQ.isEmpty();
}
}
public static void main(String[] args) {
DogCatQueue dcq = new DogCatQueue();
Pet dog = new Dog();
Pet cat = new Cat();
dcq.add(dog);
dcq.add(cat);
dcq.add(dog);
if(!dcq.isDogQueueEmpty()) {
System.out.println(dcq.pollDog().getPetType());
}
while (!dcq.isEmpty()) {
System.out.println(dcq.pollAll().getPetType());
}
}
}