您的位置:首页 > 理论基础 > 数据结构算法

Java数据结构----栈(Stack)源码分析和用链表简单实现

2017-06-04 16:11 891 查看

一、Stack源码分析

1.继承结构

  栈是数据结构中一种很重要的数据结构类型,因为栈的后进先出功能是实际的开发中有很多的应用场景。Java API中提供了栈(Stacck)的实现。

  Stack类继承了Vector类,而Vector类继承了AbstractList抽象类,实现了List接口,Cloneable接口,RandomAcces接口以及Serializable接口,需要指出的Vector内部还有两个内部类ListItr和Itr,Itr在继承Vector的同时实现了Iterator接口,而ListItr在继承了Itr类的同时实现了ListIterator接口。



2、源码分析

Stack类里的方法:

public class Stack<E> extends Vector<E> {
/**
* 一个无参构造方法,能直接创建一个Stack.
*/
public Stack() {
}

/**
* 向栈顶压入一个项
*/
public E push(E item) {
addElement(item);

return item;
}

/**
* 移走栈顶对象,将该对象作为函数值返回
*/
public synchronized E pop() {
E obj;
int len = size();

obj = peek();
removeElementAt(len - 1);

return obj;
}

/**
* 查找栈顶对象,而不从栈中移走。
*/
public synchronized E peek() {
int len = size();

if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}

/**
* 测试栈是否为空
*/
public boolean empty() {
return size() == 0;
}

/**
* 返回栈中对象的位置,从1开始。
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o);

if (i >= 0) {
return size() - i;
}
return -1;
}

private static final long serialVersionUID = 1224463164541339165L;
}


其他值的方法是从Vector类继承而来,通过源码可以发现Vector有几个属性值:

protected Object[] elementData   //用于保存Stack中的每个元素;
protected int elementCount   //用于动态的保存元素的个数,即实际元素个数
protected int capacityIncrement  //用来保存Stack的容量(一般情况下应该是大于elementCount)
private static final int MAX_ARRAY_SIZE = 2147483639 ; // 用于限制Stack能够保存的最大值数量


通过这几属性我们可以发现,Stack底层是采用数组来实现的

1. push(E item)

  向栈顶压入一个项

  注意的是:push里的方法是在Vector里实现的。

public E push(E item) {
addElement(item);

return item;
}

public synchronized void addElement(E obj) {
//通过记录modCount参数来实现Fail-Fast机制
modCount++;
//确保栈的容量大小不会使新增的数据溢出
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}

private void ensureCapacityHelper(int minCapacity) {
//防止溢出。超出了数组可容纳的长度,需要进行动态扩展!!!
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

//数组动态增加的关键所在
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
//如果是Stack的话,数组扩展为原来的 !两倍!
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);

//扩展数组后需要判断两次
//第1次是新数组的容量是否比elementCount + 1的小(minCapacity;)
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;

//第1次是新数组的容量是否比指定最大限制Integer.MAX_VALUE - 8 大
//如果大,则minCapacity过大,需要判断下
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);

elementData = Arrays.copyOf(elementData, newCapacity);
}

//检查容量的int值是不是已经溢出
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();

return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}


System.arraycopy和Arrays.copyOf()详解

2.peek()

  查找栈顶对象,而不从栈中移走

public synchronized E peek() {
int len = size();

if (len == 0)
throw new EmptyStackException();

return elementAt(len - 1);
}

//Vector里的方法,获取实际栈里的元素个数
public synchronized int size() {
return elementCount;
}

public synchronized E elementAt(int index) {
if (index >= elementCount) {
//数组下标越界异常
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}

//返回数据下标为index的值
return elementData(index);
}

@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}


3.pop()

  移走栈顶对象,将该对象作为函数值返回

public synchronized E pop() {
E obj;
int len = size();

obj = peek();

//len-1的得到值就是数组最后一个数的下标
removeElementAt(len - 1);

return obj;
}

//Vector里的方法
public synchronized void removeElementAt(int index) {
modCount++;
//数组下标越界异常出现的情况
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
} else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}

//数组中index以后的元素个数,由于Stack调用的该方法,j始终为0
int j = elementCount - index - 1;
if (j > 0) {
// 数组中index以后的元素,整体前移,(这个方法挺有用的!!)
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}


System.arraycopy和Arrays.copyOf()详解

4.empty()

  测试栈是否为空

public boolean empty() {
return size() == 0;
}


5.search(Object o)

  返回栈中对象的位置,从1开始。如果对象o作为项在栈中存在,方法返回离栈顶最近的距离。需要注意底层实现的时候,要查找的对象需要把null和正常的单独进行处理

//栈中最顶部的项被认为距离为1。
public synchronized int search(Object o) {
//lastIndexOf返回一个指定的字符串值最后出现的位置,
//在一个字符串中的指定位置从后向前搜索
int i = lastIndexOf(o);

if (i >= 0) {
//所以离栈顶最近的距离需要相减
return size() - i;
}
return -1;
}

//Vector里的方法
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}

public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);

//Vector、Stack里可以放null数据
if (o == null) {
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}

return -1;
}


二、个人简单实现

栈单链表实现:没有长度限制,并且出栈和入栈速度都很快

/**
* 用单链表实现栈
*
* @author changwen on 2017/6/4.
*/
public class StackByLinkedList {

public StackByLinkedList() {
head = null;
}

public Node head; // 头结点  测试时用private,为了看这个值

/**
* 定义单链表数据结构
* 栈单链表实现:没有长度限制,并且出栈和入栈速度都很快
*/
public class Node {
Node next; //下一个结点的引用
Object data;  //结点元素

public Node(Object data) {
this.data = data;
}
}

/* ------------------实现相关的方法---------------*/
public void push(Object data) {
Node node = new Node(data);
node.next = head;
head = node;
}

public Object pop() throws Exception {
if (head == null)
throw new Exception("Stack is empty!");
Node temp = head;
//head = temp.next;也行
head = head.next;
return temp.data;
}

public void display() {
if (head == null)
System.out.println("empty");

System.out.print("top -> bottom : | ");
Node cur = head;
while (cur != null) {
System.out.print(cur.data.toString() + " | ");
cur = cur.next;
}

System.out.print("\n");
}

}


@org.junit.Test
public void test3() throws Exception {
StackByLinkedList stack = new StackByLinkedList();
stack.push(1);
stack.push(2);
stack.push(3);

System.out.println(stack.head.data);

stack.pop();

stack.display();
}


3

top -> bottom : | 2 | 1 |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息