Stack介绍以及设计一个有getMin 功能的栈
2017-08-30 10:01
567 查看
Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来
Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用
[plain] view
plain copy
E push(E item)
把项压入堆栈顶部。
E pop()
移除堆栈顶部的对象,并作为此函数的值返回该对象。
E peek()
查看堆栈顶部的对象,但不从堆栈中移除它。
boolean empty()
测试堆栈是否为空。
int search(Object o)
返回对象在堆栈中的位置,以 1 为基数。
Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组( a growable array of objects)那么这个数组的哪里作为Stack的栈顶,哪里作为Stack的栈底?
答案只能从源代码中寻找,jdk1.6:
[java] view
plain copy
public class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}
/**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item);
return item;
}
/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
/**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
/**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
}
/**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}
通过peek()方法注释The object at the top of this stack (the last item of the Vector object,可以发现数组(Vector)的最后一位即为Stack的栈顶
pop、peek以及search方法本身进行了同步
push方法调用了父类的addElement方法
empty方法调用了父类的size方法
Vector类为线程安全类
综上,Stack类为线程安全类(多个方法调用而产生的数据不一致问题属于原子性问题的范畴)
设计一个有getMin 功能的栈
Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用
[plain] view
plain copy
E push(E item)
把项压入堆栈顶部。
E pop()
移除堆栈顶部的对象,并作为此函数的值返回该对象。
E peek()
查看堆栈顶部的对象,但不从堆栈中移除它。
boolean empty()
测试堆栈是否为空。
int search(Object o)
返回对象在堆栈中的位置,以 1 为基数。
Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组( a growable array of objects)那么这个数组的哪里作为Stack的栈顶,哪里作为Stack的栈底?
答案只能从源代码中寻找,jdk1.6:
[java] view
plain copy
public class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}
/**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item);
return item;
}
/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
/**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
/**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
}
/**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}
通过peek()方法注释The object at the top of this stack (the last item of the Vector object,可以发现数组(Vector)的最后一位即为Stack的栈顶
pop、peek以及search方法本身进行了同步
push方法调用了父类的addElement方法
empty方法调用了父类的size方法
Vector类为线程安全类
综上,Stack类为线程安全类(多个方法调用而产生的数据不一致问题属于原子性问题的范畴)
设计一个有getMin 功能的栈
private Stack <Integer> stackData ; private Stack <Integer> stackMin ; public getMinStack(){ this.stackData = new Stack <Integer> (); this.stackMin = new Stack <Integer> (); } public void push(int newNum){ if(stackMin.isEmpty()){ stackMin.push(newNum); }else if(newNum>this.getMin()){ stackMin.push(newNum); } stackData.push(newNum); } public int pop(){ if(stackMin.isEmpty()){ throw new RuntimeException("stack is empty"); } int value= stackData.pop(); if(value==this.getMin()){ stackMin.pop(); } return value; } public int getMin(){ if(stackMin.isEmpty()){ throw new RuntimeException("stack is empty"); } return stackMin.peek(); }
相关文章推荐
- 算法--设计一个有getMin功能的栈
- 设计一个带有getMin功能的栈
- 栈和队列——设计一个有getMin功能的栈(二)
- 栈——设计一个有getmin功能的栈
- 001设计一个有GetMin功能的栈
- 设计一个有getMin功能的栈 程序员代码面试指南
- 设计一个具有getMin()功能的栈
- Stack(篇2)设计一个栈能在O(1)的时间和O(1)的空间getMin()
- 设计一个有getMin功能的栈
- 1_1栈和队列_设计一个有Getmin功能的栈
- (一)栈和队列--设计一个有getMin功能的栈 java实现
- 分步骤介绍如何设计一个带选择和提示功能的检索框
- 设计一个有getMin功能的栈
- 完善一个iOS程序的界面功能:使用StackView以及其他UI元素
- 设计一个有getMin功能的栈
- 设计一个有getMin(),push(),pop().功能的栈
- 左神的书——《程序员代码面试指南》之设计一个有getmin功能的栈 c++实现
- 设计一个有getMin功能的栈(C++版)
- Stack(篇3)设计一个栈能够在O(1)的时间内getMin