标签:需要 blog ast tom tle line ott 反转 插入数据
public class ArrayStack{
private static final String TAG = "ArrayStack";
private Object[] contents;
private int top = -1;
private int bottom = -1;
private int SIZE = 10;//有一个初始值大小
public ArrayStack(){
contents = new Object[SIZE];
top = -1;
}
public int push(Object obj) throws Exception {
if (top > SIZE) throw new Exception("小杨逗比,栈已经满了!");
top++;
contents[top] = obj;
return top;
}
public Object pop() throws Exception{
if (top == bottom) throw new Exception("小杨逗比,栈已经空了!");
Object obj = contents[top];
contents[top] = null;
top--;
return obj;
}
public boolean isEmpty(){
return top == bottom;
}
public int getSize(){
return top + 1;
}
public void display() throws Exception{
if (getSize() == 0) throw new Exception("空栈!");
for (int i=getSize()-1;i>=0;i--){
System.out.print(contents[i].toString() + "->");
}
System.out.println("");
}
}
public void test{
ArrayStack as = new ArrayStack();
//as.display();
as.push("小杨逗比");
as.push("潇湘剑雨");
as.push("yc");
as.push("逗比");
as.push("aaa");
as.push("ertte");
as.push("hello");
as.display();
as.pop();
System.out.println(as.getSize());
as.pop();
as.display();
}
class DynArrayStack{
private int top;
private int capacity;
private int[] array;
private void doubleStack(){
int[] newArray=new int[capacity*2];
System.arraycopy(array,0,newArray,0,capacity);
capacity=capacity*2;
array=newArray;
}
public DynArrayStack(){
top=-1;
capacity=1;
array=new int[capacity];
}
public boolean isEmpty(){
return (top==-1);
}
public boolean isStackFull(){
return (top==capacity-1);
}
public void push(int date){
if(isStackFull()){
doubleStack();
}
array[++top]=date;
}
public int pop(){
if(isEmpty()){
System.out.println("Stack Empty");
return 0;
}else {
return array[top--];
}
}
public void deleteStack(){
top=-1;
}
}
public class Main {
public static void main(String[] args) {
// write your code here
DynArrayStack dynArrayStack=new DynArrayStack();
dynArrayStack.push(1);
dynArrayStack.push(2);
dynArrayStack.push(3);
System.out.println(dynArrayStack.pop());
System.out.println(dynArrayStack.pop());
System.out.println(dynArrayStack.pop());
}
}
public class MyStack<T> {
private T data;
private MyStack<T> next;
public MyStack(T data, MyStack<T> next) {
this.data = data;
this.next = next;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public MyStack<T> getNext() {
return next;
}
public void setNext(MyStack<T> next) {
this.next = next;
}
}
public class LinkStack<N> {
private MyStack<N> head;
private MyStack<N> tail;
private Integer size=0;
public MyStack<N> getHead() {
return head;
}
public void setHead(MyStack<N> head) {
this.head = head;
}
public MyStack<N> getTail() {
return tail;
}
public void setTail(MyStack<N> tail) {
this.tail = tail;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public void addStack(N data){
MyStack<N> node = new MyStack<>(data,null);
if(headIsNull()){
head = node;
tail = node;
size++;
}else{
//新加入的node是:(data,null) 让这个新的node的next指向初始的head节点 head变为(data,head))
node.setNext(head);
head = node;
size++;
}
}
public N outStack(){
if(size>0){
N outData = head.getData();
head = head.getNext();
return outData;
}else{
throw new RuntimeException("栈里无元素!");
}
}
public boolean headIsNull(){
if(head == null){
return true;
}
return false;
}
}
public void test() {
LinkStack<Integer> linkStack = new LinkStack<>();
linkStack.addStack(1);
linkStack.addStack(2);
linkStack.addStack(3);
linkStack.addStack(4);
linkStack.addStack(5);
for(int i=0;i<linkStack.getSize();i++){
System.out.println(linkStack.outStack());
}
}
public class Stack<E> extends Vector<E> {
/**
* 创建一个空的栈对象
*/
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;
}
public class ArrayStack {
//存储元素的数组,声明为Object类型能存储任意类型的数据
private Object[] elementData;
//指向栈顶的指针
private int top;
//栈的总容量
private int size;
//默认构造一个容量为10的栈
public ArrayStack(){
this.elementData = new Object[10];
this.top = -1;
this.size = 10;
}
public ArrayStack(int initialCapacity){
if(initialCapacity < 0){
throw new IllegalArgumentException("栈初始容量不能小于0: "+initialCapacity);
}
this.elementData = new Object[initialCapacity];
this.top = -1;
this.size = initialCapacity;
}
//压入元素
public Object push(Object item){
//是否需要扩容
isGrow(top+1);
elementData[++top] = item;
return item;
}
//弹出栈顶元素
public Object pop(){
Object obj = peek();
remove(top);
return obj;
}
//获取栈顶元素
public Object peek(){
if(top == -1){
throw new EmptyStackException();
}
return elementData[top];
}
//判断栈是否为空
public boolean isEmpty(){
return (top == -1);
}
//删除栈顶元素
public void remove(int top){
//栈顶元素置为null
elementData[top] = null;
this.top--;
}
/**
* 是否需要扩容,如果需要,则扩大一倍并返回true,不需要则返回false
* @param minCapacity
* @return
*/
public boolean isGrow(int minCapacity){
int oldCapacity = size;
//如果当前元素压入栈之后总容量大于前面定义的容量,则需要扩容
if(minCapacity >= oldCapacity){
//定义扩大之后栈的总容量
int newCapacity = 0;
//栈容量扩大两倍(左移一位)看是否超过int类型所表示的最大范围
if((oldCapacity<<1) - Integer.MAX_VALUE >0){
newCapacity = Integer.MAX_VALUE;
}else{
newCapacity = (oldCapacity<<1);//左移一位,相当于*2
}
this.size = newCapacity;
int[] newArray = new int[size];
elementData = Arrays.copyOf(elementData, size);
return true;
}else{
return false;
}
}
}
标签:需要 blog ast tom tle line ott 反转 插入数据
原文地址:https://www.cnblogs.com/yc211/p/10676402.html