1.栈
public class MyStack {
private int arr[];
private int top;
public MyStack() {
arr = new int[50];
top = -1;
}
public MyStack(int maxSize) {
arr = new int[maxSize];
top = -1;
}
/**
*
* @Title: push
* @Description: 压栈
* @param value
*/
public void push(int value) {
arr[++top] = value;
}
/**
*
* @Title: pop
* @Description: 出栈
* @return
* @throws Exception
*/
public int pop() throws Exception {
if(isEmpty()) {
throw new Exception("The stack is Empty!");
}else {
return arr[top--];
}
}
/**
*
* @Title: isEmpty
* @Description: 判断是否为空
* @return
*/
public boolean isEmpty() {
return top == -1;
}
/**
*
* @Title: isFull
* @Description: 判断是否已满
* @return
*/
public boolean isFull() {
return top == arr.length - 1;
}
/**
*
* @Title: peek
* @Description: 查看
* @return
* @throws Exception
*/
public int peek() throws Exception {
if(isEmpty()) {
throw new Exception("The stack is Empty!");
}else {
return arr[top];
}
}
}
2.队列
public class MyQueue {
private int arr [];
private int elements;
private int front;
private int end;
public MyQueue() {
arr = new int[50];
elements = 0;
front = 0;
end = -1;
}
public MyQueue(int maxSize) {
arr = new int[maxSize];
elements = 0;
front = 0;
end = -1;
}
/**
*
* @Title: add
* @Description: 添加数据
* @param value
*/
public void add(int value) {
arr[++end] = value;
elements ++;
}
/**
*
* @Title: remove
* @Description: 删除数据
* @return
*/
public int remove() {
elements --;
return arr[front++];
}
/**
*
* @Title: peek
* @Description: TODO
* @return
*/
public int peek() {
return arr[front];
}
/**
*
* @Title: isEmpty
* @Description: 判断是否为空
* @return
*/
public boolean isEmpty() {
return elements == 0;
}
/**
*
* @Title: isFull
* @Description: 判断是否已满
* @return
*/
public boolean isFull() {
return elements == arr.length;
}
}