java栈和队列实现

顺序栈的实现

import java.util.Arrays;
public class SequenceStack<T>
{
 private int DEFAULT_SIZE = 10;
 //保存数组的长度。
 private int capacity;
 //定义当底层数组容量不够时,程序每次增加的数组长度
 private int capacityIncrement = 0;
 //定义一个数组用于保存顺序栈的元素
 private Object[] elementData;
 //保存顺序栈中元素的当前个数
 private int size = 0;
 //以默认数组长度创建空顺序栈
 public SequenceStack()
 {
  capacity = DEFAULT_SIZE;
  elementData = new Object[capacity];
 }
 //以一个初始化元素来创建顺序栈
 public SequenceStack(T element)
 {
  this();
  elementData[0] = element;
  size++;
 }
 /**
  * 以指定长度的数组来创建顺序栈
  * @param element 指定顺序栈中第一个元素
  * @param initSize 指定顺序栈底层数组的长度
  */
 public SequenceStack(T element , int initSize)
 {
  this.capacity = initSize;
  elementData = new Object[capacity];
  elementData[0] = element;
  size++;
 }
 /**
  * 以指定长度的数组来创建顺序栈
  * @param element 指定顺序栈中第一个元素
  * @param initSize 指定顺序栈底层数组的长度
  * @param capacityIncrement 指定当顺序栈底层数组的长度不够时,底层数组每次增加的长度
  */
 public SequenceStack(T element , int initSize
  , int capacityIncrement)
 {
  this.capacity = initSize;
  this.capacityIncrement = capacityIncrement;
  elementData = new Object[capacity];
  elementData[0] = element;
  size++;
 }
 //获取顺序栈的大小
 public int length()
 {
  return size;
 }
 //入栈
 public void push(T element)
 {
  ensureCapacity(size + 1);
  elementData[size++] = element;
 }
 //很麻烦,而且性能很差
 private void ensureCapacity(int minCapacity)
 {
  //如果数组的原有长度小于目前所需的长度
  if (minCapacity > capacity)
  {
   if (capacityIncrement > 0)
   {
    while (capacity < minCapacity)
    {
     //不断地将capacity长度加capacityIncrement,
     //直到capacity大于minCapacity为止
     capacity += capacityIncrement;
    }
   }
   else
   {
    //不断地将capacity * 2,直到capacity大于minCapacity为止
    while (capacity < minCapacity)
    {
     capacity <<= 1;
    }
   }
   elementData = Arrays.copyOf(elementData , capacity);
  }
 }
 //出栈
    public T pop()
 {
  T oldValue = (T)elementData[size - 1];
  //释放栈顶元素
  elementData[--size] = null;
  return oldValue;
 }
 //返回栈顶元素,但不删除栈顶元素
    public T peek()
 {
  return (T)elementData[size - 1];
 }
 //判断顺序栈是否为空栈
 public boolean empty()
 {
  return size == 0;
 }
 //清空顺序栈
 public void clear()
 {
  //将底层数组所有元素赋为null
  Arrays.fill(elementData , null);
  size = 0;
 }
 public String toString()
 {
  if (size == 0)
  {
   return "[]";
  }
  else
  {
   StringBuilder sb = new StringBuilder("[");
   for (int i = size - 1  ; i > -1 ; i-- )
   {
    sb.append(elementData[i].toString() + ", ");
   }
   int len = sb.length();
   return sb.delete(len - 2 , len).append("]").toString();
  }
 }
 
 public static void main(String[] args)
 {
  SequenceStack<String> stack =
   new SequenceStack<String>();
  //不断地入栈
  stack.push("aaaa");
  stack.push("bbbb");
  stack.push("cccc");
  stack.push("dddd");
  System.out.println(stack);
  //访问栈顶元素
  System.out.println("访问栈顶元素:" + stack.peek());
  //弹出一个元素
  System.out.println("第一次弹出栈顶元素:" + stack.pop());
  //再次弹出一个元素
  System.out.println("第二次弹出栈顶元素:" + stack.pop());
  System.out.println("两次pop之后的栈:" + stack);
 }
}

链式栈的实现

public class LinkStack<T>
{
 //定义一个内部类Node,Node实例代表链栈的节点。
 private class Node
 {
  //保存节点的数据
  private T data;
  //指向下个节点的引用
  private Node next;
  //无参数的构造器
  public Node()
  {
  }
  //初始化全部属性的构造器
  public Node(T data , Node next)
  {
   this.data = data;
   this.next = next;
  }
 }
 //保存该链栈的栈顶元素
 private Node top;
 //保存该链栈中已包含的节点数
 private int size;
 //创建空链栈
 public LinkStack()
 {
  //空链栈,top的值为null
  top = null;
 }
 //以指定数据元素来创建链栈,该链栈只有一个元素
 public LinkStack(T element)
 {
  top = new Node(element , null);
  size++;
 }
 //返回链栈的长度 
 public int length()
 {
  return size;
 }
 //进栈
 public void push(T element)
 {
  //让top指向新创建的元素,新元素的next引用指向原来的栈顶元素
  top = new Node(element , top);
  size++;
 }
 //出栈
    public T pop()
 {
  Node oldTop = top;
  //让top引用指向原栈顶元素的下一个元素
  top = top.next;
  //释放原栈顶元素的next引用
  oldTop.next = null;
  size--;
  return oldTop.data;
 }
 //访问栈顶元素,但不删除栈顶元素
 public T peek()
 {
  return top.data;
 }
 //判断链栈是否为空栈
 public boolean empty()
 {
  return size == 0;
 }
 //清空链栈
 public void clear()
 {
  //将底层数组所有元素赋为null
  top = null;
  size = 0;
 }
 public String toString()
 {
  //链栈为空链栈时
  if (empty())
  {
   return "[]";
  }
  else
  {
   StringBuilder sb = new StringBuilder("[");
   for (Node current = top ; current != null
    ; current = current.next )
   {
    sb.append(current.data.toString() + ", ");
   }
   int len = sb.length();
   return sb.delete(len - 2 , len).append("]").toString();
  }
 }
 
 public static void main(String[] args)
 {
  LinkStack<String> stack =
   new LinkStack<String>();
  //不断地入栈
  stack.push("aaaa");
  stack.push("bbbb");
  stack.push("cccc");
  stack.push("dddd");
  System.out.println(stack);
  //访问栈顶元素
  System.out.println("访问栈顶元素:" + stack.peek());
  //弹出一个元素
  System.out.println("第一次弹出栈顶元素:" + stack.pop());
  //再次弹出一个元素
  System.out.println("第二次弹出栈顶元素:" + stack.pop());
  System.out.println("两次pop之后的栈:" + stack);
 }
}

顺序队列的实现

import java.util.Arrays;
public class SequenceQueue<T>
{
 private int DEFAULT_SIZE = 10;
 //保存数组的长度。
 private int capacity;
 //定义一个数组用于保存顺序队列的元素
 private Object[] elementData;
 //保存顺序队列中元素的当前个数
 private int front = 0;
 private int rear = 0;
 //以默认数组长度创建空顺序队列
 public SequenceQueue()
 {
  capacity = DEFAULT_SIZE;
  elementData = new Object[capacity];
 }
 //以一个初始化元素来创建顺序队列
 public SequenceQueue(T element)
 {
  this();
  elementData[0] = element;
  rear++;
 }
 /**
  * 以指定长度的数组来创建顺序队列
  * @param element 指定顺序队列中第一个元素
  * @param initSize 指定顺序队列底层数组的长度
  */
 public SequenceQueue(T element , int initSize)
 {
  this.capacity = initSize;
  elementData = new Object[capacity];
  elementData[0] = element;
  rear++;
 }
 //获取顺序队列的大小
 public int length()
 {
  return rear - front;
 }
 //插入队列
 public void add(T element)
 {
  if (rear > capacity - 1)
  {
   throw new IndexOutOfBoundsException("队列已满的异常");
  }
  elementData[rear++] = element;
 }
 //移除队列
    public T remove()
 {
  if (empty())
  {
   throw new IndexOutOfBoundsException("空队列异常");
  }
  //保留队列的rear端的元素的值
  T oldValue = (T)elementData[front];
  //释放队列的rear端的元素
  elementData[front++] = null;
  return oldValue;
 }
 //返回队列顶元素,但不删除队列顶元素
    public T element()
 {
  if (empty())
  {
   throw new IndexOutOfBoundsException("空队列异常");
  }
  return (T)elementData[front];
 }
 //判断顺序队列是否为空队列
 public boolean empty()
 {
  return rear == front;
 }
 //清空顺序队列
 public void clear()
 {
  //将底层数组所有元素赋为null
  Arrays.fill(elementData , null);
  front = 0;
  rear = 0;
 }
 public String toString()
 {
  if (empty())
  {
   return "[]";
  }
  else
  {
   StringBuilder sb = new StringBuilder("[");
   for (int i = front  ; i < rear ; i++ )
   {
    sb.append(elementData[i].toString() + ", ");
   }
   int len = sb.length();
   return sb.delete(len - 2 , len).append("]").toString();
  }
 }
 public static void main(String[] args)
 {
  SequenceQueue<String> queue = new SequenceQueue<String>();
  //依次将4个元素加入队列
  queue.add("aaaa");
  queue.add("bbbb");
  queue.add("cccc");
  queue.add("dddd");
  System.out.println(queue);
  System.out.println("访问队列的front端元素:"
   + queue.element());
  System.out.println("移除队列的front端元素:"
   + queue.remove());
  System.out.println("移除队列的front端元素:"
   + queue.remove());
  System.out.println("两次调用remove方法后的队列:"
   + queue);

 }
}

循环队列的实现

import java.util.Arrays;
public class LoopQueue<T>
{
 private int DEFAULT_SIZE = 10;
 //保存数组的长度。
 private int capacity;
 //定义一个数组用于保存循环队列的元素
 private Object[] elementData;
 //保存循环队列中元素的当前个数
 private int front = 0;
 private int rear = 0;
 //以默认数组长度创建空循环队列
 public LoopQueue()
 {
  capacity = DEFAULT_SIZE;
  elementData = new Object[capacity];
 }
 //以一个初始化元素来创建循环队列
 public LoopQueue(T element)
 {
  this();
  elementData[0] = element;
  rear++;
 }
 /**
  * 以指定长度的数组来创建循环队列
  * @param element 指定循环队列中第一个元素
  * @param initSize 指定循环队列底层数组的长度
  */
 public LoopQueue(T element , int initSize)
 {
  this.capacity = initSize;
  elementData = new Object[capacity];
  elementData[0] = element;
  rear++;
 }
 //获取循环队列的大小
 public int length()
 {
  if (empty())
  {
   return 0;
  }
  return rear > front ? rear - front
   : capacity - (front - rear);
 }
 //插入队列
 public void add(T element)
 {
  if (rear == front
   && elementData[front] != null)
  {
   throw new IndexOutOfBoundsException("队列已满的异常");
  }
  elementData[rear++] = element;
  //如果rear已经到头,那就转头
  rear = rear == capacity ? 0 : rear;
 }
 //移除队列
 public T remove()
 {
  if (empty())
  {
   throw new IndexOutOfBoundsException("空队列异常");
  }
  //保留队列的rear端的元素的值
  T oldValue = (T)elementData[front];
  //释放队列的rear端的元素
  elementData[front++] = null;
  //如果front已经到头,那就转头
  front = front == capacity ? 0 : front;
  return oldValue;
 }
 //返回队列顶元素,但不删除队列顶元素
 public T element()
 {
  if (empty())
  {
   throw new IndexOutOfBoundsException("空队列异常");
  }
  return (T)elementData[front];
 }
 //判断循环队列是否为空队列
 public boolean empty()
 {
  //rear==front且rear处的元素为null
  return rear == front
   && elementData[rear] == null;
 }
 //清空循环队列
 public void clear()
 {
  //将底层数组所有元素赋为null
  Arrays.fill(elementData , null);
  front = 0;
  rear = 0;
 }
 public String toString()
 {
  if (empty())
  {
   return "[]";
  }
  else
  {
   //如果front < rear,有效元素就是front到rear之间的元素
   if (front < rear)
   {
    StringBuilder sb = new StringBuilder("[");
    for (int i = front  ; i < rear ; i++ )
    {
     sb.append(elementData[i].toString() + ", ");
    }
    int len = sb.length();
    return sb.delete(len - 2 , len).append("]").toString();
   }
   //如果front >= rear,有效元素为front->capacity之间、0->front之间的
   else
   {
    StringBuilder sb = new StringBuilder("[");
    for (int i = front  ; i < capacity ; i++ )
    {
     sb.append(elementData[i].toString() + ", ");
    }
    for (int i = 0 ; i < rear ; i++)
    {
     sb.append(elementData[i].toString() + ", ");
    }
    int len = sb.length();
    return sb.delete(len - 2 , len).append("]").toString();
   }
  }
 }
 public static void main(String[] args)
 {
  LoopQueue<String> queue
   = new LoopQueue<String>("aaaa" , 3);
  //添加两个元素
  queue.add("bbbb");
  queue.add("cccc");
  //此时队列已满
  System.out.println(queue);
  //删除一个元素后,队列可以再多加一个元素
  queue.remove();
  System.out.println("删除一个元素后的队列:" + queue);
  //再次添加一个元素,此时队列又满
  queue.add("dddd");
  System.out.println(queue);
  System.out.println("队列满时的长度:" + queue.length());
  //删除一个元素后,队列可以再多加一个元素
  queue.remove();
  //再次加入一个元素,此时队列又满
  queue.add("eeee");
  System.out.println(queue);
 }
}

链式队列的实现

public class LinkQueue<T>
{
 //定义一个内部类Node,Node实例代表链队列的节点。
 private class Node
 {
  //保存节点的数据
  private T data;
  //指向下个节点的引用
  private Node next;
  //无参数的构造器
  public Node()
  {
  }
  //初始化全部属性的构造器
  public Node(T data ,  Node next)
  {
   this.data = data;
   this.next = next;
  }
 }
 //保存该链队列的头节点
 private Node front;
 //保存该链队列的尾节点
 private Node rear;
 //保存该链队列中已包含的节点数
 private int size;
 //创建空链队列
 public LinkQueue()
 {
  //空链队列,front和rear都是null
  front = null;
  rear = null;
 }
 //以指定数据元素来创建链队列,该链队列只有一个元素
 public LinkQueue(T element)
 {
  front = new Node(element , null);
  //只有一个节点,front、rear都指向该节点
  rear = front;
  size++;
 }
 //返回链队列的长度 
 public int length()
 {
  return size;
 }
 //将新元素加入队列
 public void add(T element)
 {
  //如果该链队列还是空链队列
  if (front == null)
  {
   front = new Node(element , null);
   //只有一个节点,front、rear都指向该节点
   rear = front;
  }
  else
  {
   //创建新节点
   Node newNode = new Node(element , null);
   //让尾节点的next指向新增的节点
   rear.next = newNode;
   //以新节点作为新的尾节点
   rear = newNode;
  }
  size++;
 }
 //删除队列front端的元素
 public T remove()
 {
  Node oldFront = front;
  front = front.next;
  oldFront.next = null;
  size--;
  return oldFront.data;
 }
 //访问链式队列中最后一个元素
 public T element()
 {
  return rear.data;
 }
 //判断链式队列是否为空队列
 public boolean empty()
 {
  return size == 0;
 }
 //清空链队列
 public void clear()
 {
  //将front、rear两个节点赋为null
  front = null;
  rear = null;
  size = 0;
 }
 public String toString()
 {
  //链队列为空链队列时
  if (empty())
  {
   return "[]";
  }
  else
  {
   StringBuilder sb = new StringBuilder("[");
   for (Node current = front ; current != null
    ; current = current.next )
   {
    sb.append(current.data.toString() + ", ");
   }
   int len = sb.length();
   return sb.delete(len - 2 , len).append("]").toString();
  }
 }
 
 public static void main(String[] args)
 {
  LinkQueue<String> queue
   = new LinkQueue<String>("aaaa");
  //添加两个元素
  queue.add("bbbb");
  queue.add("cccc");
  System.out.println(queue);
  //删除一个元素后
  queue.remove();
  System.out.println("删除一个元素后的队列:" + queue);
  //再次添加一个元素
  queue.add("dddd");
  System.out.println("再次添加元素后的队列:" + queue);
  //删除一个元素后,队列可以再多加一个元素
  queue.remove();
  //再次加入一个元素
  queue.add("eeee");
  System.out.println(queue);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值