package dataStructure;
import java.util.Arrays;
class OrderStack<T>{
private int top;
private T[] stackArrays;
private static final int defaultcapacity = 5;
public OrderStack(){
this(defaultcapacity);
}
public OrderStack(int capacity){
this.stackArrays = (T[])new Object[capacity];
}
public void push(T value){
if(isFull()){
this.stackArrays = Arrays.copyOf(stackArrays, stackArrays.length*2);
}
this.stackArrays[top++] = value;
}
public boolean isFull(){
return top == stackArrays.length;
}
public T pop(){
if(isEmpty()){
throw new UnsupportedOperationException("the stack has been empty");
}
T result = stackArrays[top-1];
stackArrays[--top] = null;
return result;
}
public boolean isEmpty(){
return top == 0;
}
public T peek(){
return stackArrays[top-1];
}
public void show(){
for(int i=top-1; i>=0; i--){
System.out.print(stackArrays[i] + " ");
}
System.out.println();
}
}
class LoopQueue<T>{
private int header;
private int tail;
private T[] queueArrays;
private static final int defaultcapacity = 5;
public LoopQueue(){
this(defaultcapacity);
}
public LoopQueue(int capacity){
this.queueArrays = (T[])new Object[capacity];
}
public boolean enqueue(T value){
if(isFull()){
queueArrays = Arrays.copyOf(queueArrays, queueArrays.length*2);
}
queueArrays[tail] = value;
tail = (tail+1) % queueArrays.length;
return true;
}
public boolean isFull(){
return (tail+1)%queueArrays.length == header;
}
public T dequeue(){
if(isEmpty()){
throw new UnsupportedOperationException("the queue has been empty");
}
T result = queueArrays[header];
queueArrays[header] = null;
header = (header+1) % queueArrays.length;
return result;
}
public boolean isEmpty(){
return header == tail;
}
public void show(){
for(int i=header; i<tail; i=(i+1)%queueArrays.length){
System.out.print(queueArrays[i]+" ");
}
System.out.println();
}
}
class OrderQueue<T>{
private int header;
private int tail;
private int size;
private T[] queueArrays;
private static final int defaultcapacity = 5;
public OrderQueue(){
this(defaultcapacity);
}
public OrderQueue(int capacity){
this.queueArrays = (T[])new Object[capacity];
}
public boolean enqueue(T value){
if(isFull()){
if(size == queueArrays.length){
queueArrays = Arrays.copyOf(queueArrays, queueArrays.length*2);
}else{
System.arraycopy(queueArrays, header, queueArrays, 0, size);
}
}
queueArrays[tail++] = value;
size++;
return true;
}
public boolean isFull(){
return tail == queueArrays.length;
}
public T dequeue(){
if(isEmpty()){
throw new UnsupportedOperationException("the queue has been empty");
}
T result = queueArrays[header];
queueArrays[header++] = null;
size--;
return result;
}
public boolean isEmpty(){
return size == 0;
}
public void show(){
for(int i=header; i<tail; i++){
System.out.print(queueArrays[i]+" ");
}
System.out.println();
}
}
public class TestDemo8 {
public static void main(String[] args) {
OrderStack<Integer> stack = new OrderStack<>();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
stack.show();
stack.pop();
stack.show();
System.out.println(stack.peek());
stack.show();
System.out.println("==============");
OrderQueue<String> queue = new OrderQueue<>();
queue.enqueue("zjhjdhjhh");
queue.enqueue("dhjhh");
queue.enqueue("jhjdhjhh");
queue.enqueue("dhjhh");
queue.enqueue("zdhjhh");
queue.enqueue("zjhdhjhh");
queue.enqueue("zjhhjhh");
queue.show();
queue.dequeue();
queue.dequeue();
queue.show();
System.out.println("==============");
LoopQueue<Integer> loopQueue = new LoopQueue<>();
loopQueue.enqueue(10);
loopQueue.enqueue(20);
loopQueue.enqueue(30);
loopQueue.enqueue(40);
loopQueue.enqueue(50);
loopQueue.dequeue();
loopQueue.dequeue();
loopQueue.dequeue();
loopQueue.show();
loopQueue.enqueue(100);
loopQueue.enqueue(200);
loopQueue.show();
}
}