目录
链表实现队列
数组实现队列
链表实现栈
数组实现栈
图片:
链表实现队列
package Queue;
import java.util.Iterator;
public class LinkedListQueue <E> implements Queue<E>, Iterable<E>{//单向环形哨兵链表
//节点类
private static class Node<E>{
E value;
Node<E> next;
Node(E value, Node<E> next){
this.value=value;
this.next=next;
}
}
Node<E> head=new Node<>(null,null);
Node<E> tail=head;//tail指向头
private int size=0;//节点数
private int capacity=Integer.MIN_VALUE;//容量
public LinkedListQueue(){
tail.next=head;
}
public LinkedListQueue(int capacity){
this.capacity=capacity;
tail.next=head;
}
//从队尾插入
@Override
public boolean offer(E value) {
if(isFull()){
return false;
}
Node<E> added= new Node<>(value,head);//added新节点尾部指向头
tail.next=added;//原来的尾部指向新节点
tail=added;//尾部指向新节点
size++;
return true;
}
//获取队列的第一个值,并移除
@Override
public E poll() {
if(isEmpty()){
return null;
}
E value=head.next.value;
Node<E> first=head.next;
head.next=first.next;
size--;
if(first==tail){
tail=head;
}
return value;
}
//从对头获取值,但不移除
@Override
public E peek() {
if(isEmpty()){
return null;
}
return head.next.value;
}
@Override
public boolean isEmpty() {
return head==tail;
}
@Override
public boolean isFull() {
return size==capacity;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>(){
Node<E> p=head.next;
public boolean hasNext() {//p==head退出
return p!=head;
}
public E next(){
E value=p.value;
p=p.next;
return value;
}
};
}
}
数组实现队列
package circleQueueArray;
import java.util.Iterator;
public class circleQueueArray<T> implements Queue<T>, Iterable<T>{//环形数组实现队列
private T[] arry;
private int head=0;
private int tail=0;
public circleQueueArray(int capacity){
arry= (T[]) new Object[capacity+1];//+1是为了区分满和空
}
//向队列尾部添加元素
@Override
public boolean offer(T value) {
if(isFull()){
return false;
}
arry[tail]=value;
tail=(tail+1)%arry.length;
return true;
}
//获取头部元素,移除
@Override
public T poll() {
if(isEmpty()){
return null;
}
T value=arry[head];
head=(head+1)%arry.length;
return value;
}
@Override
public T peek() {
if(isEmpty()){
return null;
}
return arry[head];
}
@Override
public boolean isEmpty() {
if(head==tail){
return true;
}
return false;
}
@Override
public boolean isFull() {
if(head==(tail+1)%arry.length){
return true;
}
return false;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
int p=head;
@Override
public boolean hasNext() {
return p!=tail;
}
@Override
public T next() {
T value=arry[p];
p=(p+1)%arry.length;
return value;
}
};
}
}
链表实现栈
import java.util.Iterator;
public class LinkedListStack<E> implements Stack<E>, Iterable<E> {
static class Node<E>{
E value;
Node<E> next;
public Node(E value, Node<E> next) {
this.value = value;
this.next = next;
}
}
private int capacity;//栈的容量
private int size;
private Node<E> head=new Node<E>(null,null);//头指针
public LinkedListStack(int capacity) {
this.capacity = capacity;
}
//向栈顶压入元素
// head->1->null
@Override
public boolean push(E value) {
if(isFull()){
return false;
}
Node<E> added=new Node<>(value,head.next);
head.next=added;//头指针指向新节点
size++;
return true;
}
//弹出栈顶元素
@Override
public E pop() {
if(isEmpty()){
return null;
}
E value=head.next.value;
head.next=head.next.next;
size--;
return value;
}
//返回栈顶元素,但不弹出
@Override
public E peek() {
if(isEmpty()){
return null;
}
return head.next.value;
}
//判断栈是否为空
@Override
public boolean isEmpty() {
// return head.next == null;
return size==0;
}
//判断栈是否已满
@Override
public boolean isFull() {
return size==capacity;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
Node<E> p=head.next;
@Override
public boolean hasNext() {
return p!=null;
}
@Override
public E next() {
E value= p.value;
p=p.next;
return value;
}
};
}
}
数组实现栈
package StackArray;
import java.util.Iterator;
public class ArrayStack<E> implements Stack<E>, Iterable<E> {
private E[] array;
private int top;//栈顶 top是索引
/*底 顶
* 0 1 2 3
* a
* t
* */
public ArrayStack(int capacity) {
this.array = (E[]) new Object[capacity];
}
@Override
public boolean push(E value) {
if(isFull()){
return false;
}
array[top++]=value;
return true;
}
//弹出栈顶元素
@Override
public E pop() {
if(isEmpty()){
return null;
}
//栈顶元素 添加完后,向前移动一个
//--top是--先让他找到栈顶元素,并保留在此,意思为这没有元素
return array[--top];
}
@Override
public E peek() {
if(isEmpty()){
return null;
}
return array[top-1];
}
@Override
public boolean isEmpty() {
//指向0索引
return top==0;
}
@Override
public boolean isFull() {
return top==array.length;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>(){
int p=top-1;
@Override
public boolean hasNext() {
return p>=0;
}
@Override
public E next() {
return array[p--];
}
};
}
}