MinStack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.html

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

题目的意思是实现一个栈,可以实现推入,推出,返回顶端元素,得到最小元素的功能java

 

public class MinClass {
	
	private Stack<Integer> a = new Stack<Integer>() ;
	private Stack<Integer> minStack = new Stack<Integer>(); 
	
    public void push(int x) {
    	/**
    	 * 判断最小栈,若是栈为空或者压入的栈值小于栈顶元素,将x压入最小栈
    	 */
    	if(minStack.isEmpty() || x<minStack.peek()){
    		minStack.push(x);
    	}
    	a.push(x);
        
    }
    public void pop() {
    	/**
    	 * 若是不相等,会把minStack中保存的最小值给推出。因此必须保证minStack中推出
    	 * 的值必须和a中的值同样
    	 */
    	if(minStack.peek().equals(a.peek())){
    		minStack.pop();
    	}
    	a.pop(); 
    }
    
    public int top() {
    	return a.peek();   
    }
    
    public int getMin() {	
    	return minStack.peek();   
    }
    
    /**
     * 测试
     */
    public static void main(){
    	MinClass obj = new MinClass();
    	obj.push(10);
    	obj.push(20);
    	obj.pop();
    	obj.push(30);
    	System.out.println(obj.getMin());
    }
}

  

 

拓  展node


 

图片来自于维基程序员

堆栈(英语stack),也可直接称栈 因为堆栈数据结构只容许在一端进行操做,于是按照后进先出(LIFO, Last In First Out)的原理运做。栈空间有操做系统自动分配,使用完成后有编译器自动释放,一般用来存放函数的参数值、局部变量的值等。api

栈使用的是靠近ALU的一级缓存,他们一般是在调用时处于存储空间中,调用完毕后就会当即释放。与“堆(Heap)"不一样,堆内存有程序员释放那个,存放于二级缓存中,结构相似于链表,程序员分配时内存变量的值不必定是连续的,所以,堆处理的速度相比与栈来讲速度要慢一些!数组

 

(截图来自 计算机系统概论)缓存

如图所示,a)有5个连续的存储空间,每一个存储单元为1个字节(这也是目前操做系统内存结构的排列方式,相似于一个大的字节数组)。R6表明栈顶寄存器,用来存储栈顶元素的指针。b)当往栈中push一个元素18之后,栈顶元素指针加1,其余地址不变。c)往栈中压入3个元素后的结果,能够看到不断修改栈顶指针的大小。d)弹出2个元素的结果安全

下面咱们用Java代码实现栈,前文已经说明,实现栈的方式能够有数组,也能够有链表来实现(由于这两个数据结构都是线性结构)数据结构

Java的SDK中封装好的有一个Stack,他的经常使用方法有oracle

empty()

Tests if this stack is empty
peek()
Looks at the object at the top of this stack without removing it from the stack.
pop()
Removes the object at the top of this stack and returns that object as the value of this function.
push(E item)
Pushes an item onto the top of this stack.
栈的数组实现:
/**
 * 栈的数组实现方式
 * @author CYW
 */
public class ArrayStack {
	private long[] stackArray;//数组
	private int stackSize;//记录存储记录的最大的条数
	private int top = -1;//记录栈顶位置
	
	// 记录分配存储空间的大小
	public ArrayStack(int s){
		stackArray = new long[s];
		top  = -1;
		stackSize = s;
	}
	
	public long pop(){
		return stackArray[top--];
	}
	
	public void push(int x){
		stackArray[++top] = x;
	}

	public long peek(){
		return stackArray[top];
	}
	
	public boolean isEmpty(){
		return (stackArray.length == 0);		
	}
	
	// 测试
	public static void main(){
		ArrayStack obj = new ArrayStack(100);
		obj.push(100);
		obj.push(20);
		obj.push(10);
		obj.push(200);
		obj.push(1000);
		obj.pop();
		/**
		 * 
		 */
		for (int i = 0;i<obj.top;i++){
			System.out.println(obj.stackArray[i]);
		}
	}
}

 须要注意的一点是:咱们在输出数组元素的时候不能根据数组的长度来判断,而必须根据站顶的top来判断!

 

C++代码

struct Node{
	struct Node* pPre;
	int data;
};

class ListStack{
private:
	//定义指向栈顶元素的指针
	struct Node *top;

public:

	void push(int data){
		//建立新的变量
		struct Node *node = new struct Node();
		node->data = data;
		node->pPre = top;
		//从新定义top的指向
		top = node;
	}

	int pop(){

		//从新定义top的指向
		int temp = top->data;
		top =top->pPre;
		//返回输出结果
		return temp;
	}

	int peek(){
		return top->data;
	}
	//判断是否为空
	bool isEmpty(){
		if(top){
			return true;
		}else{
			return false;
		}

	}

	
};


int _tmain(int argc, _TCHAR* argv[])
{
	ListStack stack;
	stack.push(10);
	stack.push(20);
	stack.push(30);
	stack.push(40);
	stack.push(50);

	cout<<stack.pop()<<endl;
	cout<<stack.peek()<<endl;
	cout<<stack.pop()<<endl;
	cout<<stack.pop()<<endl;
	cout<<stack.pop()<<endl;
	cout<<stack.peek()<<endl;
	cout<<stack.pop()<<endl;

	if(stack.isEmpty()){
		cout<<"栈中数据为空!"<<endl;
	}else{
		stack.pop();
	}
	return 0;
}

 固然,上面代码中有不少不安全的地方,仅仅是学习的目的,http://shmilyaw-hotmail-com.iteye.com/blog/1825171 从Java给出了比较好的解释。

相关文章
相关标签/搜索