码迷,mamicode.com
首页 > 编程语言 > 详细

算法之 栈的顺序结构

时间:2015-03-10 15:18:39      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

package bin;

import java.util.ArrayList;
import java.util.List;

import org.omg.CORBA.SystemException;

/**
 * @author bin
 * target 实现栈的顺序结构
 *	摘要:栈  栈顶  栈底 LIFO 结构  last In First Out 
 */


public class Bin_stack {

	private int StackSize;
	private int current;
	private List list;
	
	public int getStackSize() {
		return StackSize;
	}

	public void setStackSize(int stackSize) {
		StackSize = stackSize;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	
	public Bin_stack(int size){
		this.setList(new ArrayList<>());
		this.setCurrent(0);
		this.setStackSize(size);
	}
	
	public void push(Object o){
		if(this.current == this.StackSize){
			 throw new RuntimeException("栈满");
		}else{
			list.add(o);
			current = ++current; 
		}
	}
	
	public Object pop(){
		if(current == 0){
			throw new RuntimeException("空栈");
		}
		Object o = list.get(current-1);
		list.remove(current-1);
		current = --current;
		return o;
	
	}
	
	public static void main(String[] args) {
		Bin_stack b = new Bin_stack(4);
		b.push(123);
		b.push(43);
		System.out.println(b.pop());
		System.out.println(b.pop());
		b.push(4312);
	}
	
}

  

算法之 栈的顺序结构

标签:

原文地址:http://www.cnblogs.com/bin-pureLife/p/4326062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!