栈
顺序栈
/**
* 顺序栈实现
* @param <T>
*/
class SeqStack<T>{
// 存储栈的元素的数组
private T[] stack;
// top表示栈顶的位置
private int top;
public SeqStack(){
this(10);
}
public SeqStack(int size){
this.stack = (T[])new Object[size];
this.top = 0;
}
/**
* 入栈操作
* @param val
*/
public void push(T val){
if(full()){
// 栈如果满了,要进行内存2倍扩容
this.stack = Arrays.copyOf(this.stack,
this.stack.length*2);
}
this.stack[this.top] = val;
this.top++;
}
/**
* 出栈操作
*/
public void pop(){
if(empty())
return;
this.top--;
this.stack[this.top] = null;
}
/**
* 返回栈顶元素
* @return
*/
public T top(){
return this.stack[this.top - 1];
}
/**
* 判断栈满
* @return
*/
public boolean full(){
return this.top == this.stack.length;
}
/**
* 判断栈空
* @return
*/
public boolean empty(){
return this.top == 0;
}
}
链式栈
/**
* 描述:链式栈结构
*/
public class LinkStack<T> {
// top指向头节点,头节点的后面就是栈顶节点
private Entry<T> top;
public LinkStack(){
this.top = new Entry<>(null, null);
}
/**
* 入栈操作
* @param val
*/
public void push(T val){
Entry<T> node = new Entry<>(val, this.top.next);
this.top.next = node;
}
/**
* 出栈操作
* @return
*/
public T pop(){
T val = null;
if(this.top.next != null){
val = this.top.next.data;
this.top.next = this.top.next.next;
}
return val;
}
/**
* 查看栈顶元素
* @return
*/
public T peek(){
T val = null;
if(this.top.next != null){
val = this.top.next.data;
}
return val;
}
/**
* 判断栈空
* @return
*/
public boolean isEmpty(){
return this.top.next == null;
}
/**
* 节点类型定义
* @param <T>
*/
static class Entry<T>{
T data;
Entry<T> next;
public Entry(T data, Entry<T> next) {
this.data = data;
this.next = next;
}
}
}
队列
循环队列
/**
* 循环队列
*/
class Queue<E>{
// 存储队列元素的数组
private E[] que;
// 表示队头的位置
private int front;
// 表示队尾的位置
private int rear;
/**
* 默认构造队列,初始大小是10
*/
public Queue(){
this(10);
}
/**
* 用户可以指定队列的大小size
* @param size
*/
public Queue(int size){
this.que = (E[])new Object[size];
this.front = this.rear = 0;
}
/**
* 入队操作
* @param val
*/
public void offer(E val){
if(full()){
// 扩容
E[] newQue = Arrays.copyOf(this.que,
this.que.length*2);
int index = 0;
for(int i=this.front;
i != this.rear;
i=(i+1)%this.que.length){
newQue[index++] = this.que[i];
}
this.front = 0;
this.rear = index;
this.que = newQue;
}
this.que[this.rear] = val;
this.rear = (this.rear+1)%this.que.length;
}
/**
* 出队操作,并把出队的元素的值返回
*/
public E poll(){
if(empty()){
return null;
}
E front = this.que[this.front];
this.que[this.front] = null;
this.front = (this.front+1)%this.que.length;
return front;
}
/**
* 查看队头元素
* @return
*/
public E peek(){
if(empty()){
return null;
}
return this.que[this.front];
}
/**
* 判断队满
* @return
*/
public boolean full(){
return (this.rear+1)%this.que.length == this.front;
}
/**
* 判断队空
* @return
*/
public boolean empty(){
return this.rear == this.front;
}
}
链式队列
/**
* 链式队列
* front:指向的是链表的头节点
* rear: 永远指向的是末尾节点
* @param <T>
*/
public class LinkQueue<T>{
// 指向头节点(队头)
private Entry<T> front;
// 指向尾节点(队尾)
private Entry<T> rear;
// 记录队列节点的个数
private int count;
/**
* 初始化,front和rear都指向头节点
*/
public LinkQueue(){
this.front = this.rear = new Entry<>(null, null);
}
/**
* 入队操作
* @param val
*/
public void offer(T val){
Entry<T> node = new Entry<>(val, null);
this.rear.next = node;
this.rear = node;
this.count++;
}
/**
* 出队操作
* @return
*/
public T poll(){
T val = null;
if(this.front.next != null){
val = this.front.next.data;
this.front.next = this.front.next.next;
// 删除队列最后一个元素,要把rear指向front,队列才能判空
if(this.front.next == null){
this.rear = this.front;
}
this.count--;
}
return val;
}
public T peek(){
T val = null;
if(this.front.next != null){
val = this.front.next.data;
}
return val;
}
/**
* 判断队列空
* @return
*/
public boolean isEmpty(){
return this.front == this.rear;
}
/**
* 返回队列元素的个数
* @return
*/
public int size(){
return this.count;
}
/**
* 节点类型定义
* @param <T>
*/
static class Entry<T>{
T data;
Entry<T> next;
public Entry(T data, Entry<T> next) {
this.data = data;
this.next = next;
}
}
}