数据结构学习笔记-栈(JAVA)

栈是限制在表的一端,进行插入和删除操作的线性表,栈是后进先出
栈的操作:
构造栈
销毁栈
清空栈
计算栈长度
取栈顶元素
元素压栈
元素弹栈
代码:
package com.alg.stack;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
// 栈是限制在表的一端,进行插入和删除操作的线性表,栈是后进先出
//栈的操作:构造栈 销毁栈 清空栈 计算栈长度 取栈顶元素 元素压栈 元素弹栈
// Iterable实现这个可以使用for(T t:stack)循环
public class Stack<T> implements Iterable<T>
{
// 统计数组内保存的元素数量
private int elementCount;
// 保存数据的数组
protected Object[] elementData = null;
// 数组的每次增长幅度
protected int capacityIncrement;
// 默认数组大小
private static final int DEFAULT_SIZE = 10;
// 操作数
private int modCount;
public Stack()
{
this(DEFAULT_SIZE, 0);
}
public Stack(int capacity)
{
this(capacity, 0);
}
public Stack(int capacity, int capacityIncrment)
{
elementData = newElementArray(capacity);
elementCount = 0;
this.capacityIncrement = capacityIncrment;
}
// 取栈顶元素
@SuppressWarnings("unchecked")
public T peek()
{
return (T) elementData[elementCount - 1];
}
// 出栈
@SuppressWarnings("unchecked")
public T pop()
{
final int index = --elementCount;
T t = (T) elementData[index];
elementData[index] = null;
modCount++;
return t;
}
// 入栈
public void push(T object)
{
// 栈满,增加栈容量
if (elementCount == elementData.length)
{
growByOne();
}
elementData[elementCount++] = object;
modCount++;
}
// 清空栈
public void clear()
{
for (int i = 0; i < elementCount; i++)
{
elementData = null;
}
modCount++;
elementCount = 0;
}
@SuppressWarnings( {"unused", "unchecked"})
private T[] newElementArray(int size)
{
return (T[]) new Object[size];
}
// 如果从节省空间角度考虑capacityIncrement最好设置
private void growByOne()
{
int adding = 0;
// 没有设置增量的情况
if (capacityIncrement <= 0)
{ // 如果elementData长度为0,就加1,否则就增加elementData.length的一倍
if ((adding = elementData.length) == 0)
{
adding = 1;
}
}
else
{
adding = capacityIncrement;
}
T[] newData = newElementArray(elementData.length + adding);
System.arraycopy(elementData, 0, newData, 0, elementCount);
elementData = newData;
}
public int size()
{
return elementCount;
}
@SuppressWarnings("unchecked")
public synchronized T remove(int location)
{
if (location < elementCount)
{
T result = (T) elementData[location];
elementCount--;
int size = elementCount - location;
// 把location + 1到最后整个拷贝到从location开始到最后
if (size > 0)
{
System.arraycopy(elementData, location + 1, elementData, location, size);
}
// 因为中间的某个删除的位置被覆盖,所以需要把最后一位直空
elementData[elementCount] = null;
modCount++;
return result;
}
throw new ArrayIndexOutOfBoundsException(location);
}
@SuppressWarnings("unchecked")
public synchronized T elementAt(int location)
{
if (location < elementCount)
{
return (T) elementData[location];
}
throw new ArrayIndexOutOfBoundsException(location);
}
// 有这个可以对java新特性for循环的支持
public Iterator<T> iterator()
{
return new SimpleIterator();
}
// 简单了实现迭代器
private class SimpleIterator implements Iterator<T>
{
int pos = -1;
int expectedModCount;
int lastPosition = -1;
SimpleIterator()
{
super();
expectedModCount = modCount;
}
public boolean hasNext()
{
return pos + 1 < size();
}
public T next()
{
if (expectedModCount == modCount)
{
try
{
T result = elementAt(pos + 1);
lastPosition = ++pos;
return result;
}
catch (IndexOutOfBoundsException e)
{
throw new NoSuchElementException();
}
}
throw new ConcurrentModificationException();
}
public void remove()
{
if (this.lastPosition == -1)
{
throw new IllegalStateException();
}
if (expectedModCount != modCount)
{
throw new ConcurrentModificationException();
}
try
{
Stack.this.remove(lastPosition);
}
catch (IndexOutOfBoundsException e)
{
throw new ConcurrentModificationException();
}
expectedModCount = modCount;
if (pos == lastPosition)
{
pos--;
}
lastPosition = -1;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值