java-模拟存放String类型数据的栈

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xllfy123/article/details/79606922

package com.sc;
/**
 * 
 * 用来模拟一个存放String数据的栈
 */
class Node{
    private String str;

    public Node(){}
    public Node(String str){
        this.str=str;
    }

    public String getstr(){
        return this.str;
    }

}
class Stack1{
    private Node node []=new Node[20];
    private int size;
    //构造函数初始化
     public Stack1(){        
         this.size=0;
     }

     public void push(Node no){

         if(getSize()>=node.length){
             //进行扩容
             Node node_new[]=new Node[node.length*2];
             System.arraycopy(node, 0, node_new, 0, node.length);
             node=node_new;
         }
         node[size++]=no;
     }
     public String pop(){
        if(size<=0)return null;
        else return node[--size].getstr();
     }
     public int getSize(){
         return this.size;
     }
}


public class stackOfString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Node node=new Node("zhangsan");
        Stack1 st=new Stack1();
        st.push(node);
        System.out.println(st.pop());
        System.out.println(st.pop());
    }

}

猜你喜欢

转载自blog.csdn.net/xllfy123/article/details/79606922