共享栈的实现

import javax.crypto.Mac;
    
    /**
     *   两栈共享空间
     * @author wydream
     *
     */
    
    public class DoubleStatk {
    
        private final static int MAXSIZE=  ;
        private int[] stackElem;
        private int top ; //将top 设置为指向栈 栈顶元素的存储位置即数组下标 
        private int top ; //将top 设置为指向栈 栈顶元素的存储位置即数组下标n- 
        
        public DoubleStatk() {
            top =- ;
            top =MAXSIZE;
            stackElem=new int[MAXSIZE];
        }
        
        //是否是空栈
        public boolean isEmptyStack() {
            if(top ==- &&top ==MAXSIZE) {
                return true;
            }
            return false;
        }
        
        //清空栈
        public void clearStack() {
            top =- ;
            top =MAXSIZE;
        }
        
        //栈的长度
        public int lengthStak() {
            return (top + )+(MAXSIZE-top );
        }
        
        //获取top 的元素
        public int getTop Elem() {
            if(top ==- ) {
                return - ;
            }
            return stackElem[top ];
        }
        
        //获取top 的元素
        public int getTop Elem() {
            if(top ==MAXSIZE) {
                return - ;
            }
            return stackElem[top ];
        }
        
        //压栈
        public void stackPush(int stackNumber,int e) {
            //如果栈已经满了
            if(top + ==top ) {
                System.out.println("栈已满");
                return;
            }
            if(stackNumber== ) {
                top += ;
                stackElem[top ]=e;
                return;
            }
            if(stackNumber== ) {
                top -= ;
                stackElem[top ]=e;
                return;
            }
            
        }
        
        //出栈
        public int stackPop(int stackNumber) {
            int rs;
            if(isEmptyStack()) {
                System.out.println("栈为空");
                return - ;
            }
            if(stackNumber== ) {
                rs= stackElem[top ];
                top --;
            }else if(stackNumber== ) {
                rs=stackElem[top ];
                top ++;
            }else {
                System.out.println("输入数据有误");
                return - ;
            }
            return rs;
        }
        
        public void stackTraverse() {
            System.out.println("此时,栈中的元素为:");
            int i= ;
            while(i<=top ) {
                System.out.println(stackElem[i++]+" ");
            }
            i=top ;
            while(i<MAXSIZE) {
                System.out.println(stackElem[i++]+" ");
            }
            System.out.println();
        }
        
        
        public static void main(String[] args) {
            DoubleStatk seqStack=new DoubleStatk();
            
            // 压栈
            for(int j= ;j<= ;j++) {
                seqStack.stackPush( ,j);
            }
            // 压栈
            for(int i=MAXSIZE;i>=MAXSIZE- ;i--) {
                seqStack.stackPush( , i);
            }
            //输出
            seqStack.stackTraverse();
            System.out.println("栈的长度为:"+seqStack.lengthStak());
            
            seqStack.stackPop( );
            seqStack.stackTraverse();
            System.out.println("栈 的栈顶元素为: " + seqStack.getTop Elem());
            System.out.println("栈 的栈顶元素为: " + seqStack.getTop Elem());
            System.out.println("栈的长度为: " + seqStack.lengthStak());
            
            for (int i =  ; i <= MAXSIZE- ; i++) {
                seqStack.stackPush( ,i);
            }
            seqStack.stackTraverse();
            System.out.println("栈 的栈顶元素为: " + seqStack.getTop Elem());
            System.out.println("栈 的栈顶元素为: " + seqStack.getTop Elem());
            System.out.println("栈顶元素为: " + seqStack.getTop Elem());
            System.out.println("栈的长度为: " + seqStack.lengthStak());
            
            System.out.println("栈是否为空: " + seqStack.isEmptyStack());
            seqStack.clearStack();
            System.out.println("栈是否为空: " + seqStack.isEmptyStack());
        }
            
    
    }

链表栈

import org.junit.jupiter.api.Test;
    
    /**
     *   基于链表实现的栈
     * @author wydream
     *
     */
    
    public class NodeStack<T> {
        
        private Node<T> top=null;//栈顶
        public NodeStack() {
            this.top=null;
        }
        
        //判断栈是否为空
        public boolean isEmpty() {
            if(top!=null) {
                return false;
            }
            return true;
        }
        
        //压栈
        public boolean push(T value) {
            Node<T> node=new Node<T>(value);
            node.setNext(top);
            top=node;
            return true;
        }
        
        //出栈
        public T pop() {
            if(top==null) {
                return null;
            }
            T tmp=top.data;
            top=top.getNext();
            return tmp;
        }
        //取出栈顶的值
        public T peek() {
            if(isEmpty()) {
                return null;
            }
            return top.data;
        }
        
        
        class Node<T>{
            private T data;//数据
            private Node<T> next;//指向下一个节点的指针
            //初始化链表
            public Node(T data) {
                this.data=data;
            }
            //获取下一个节点
            public Node<T> getNext(){
                return this.next;
            }
            //设置下一个节点
            public void setNext(Node<T> n) {
                this.next=n;
            }
            //获取节点数据
            public T getData() {
                return this.data;
            }
            //设置节点数据
            public void setData(T d) {
                this.data=d;
            }
            
        }
        
        public static void main(String[] args) {
            
            NodeStack<String> ns=new NodeStack<String>();
            
            //测试是否为空
            System.out.println("=======是否为空======");
            System.out.println(ns.isEmpty());
            System.out.println("=============");
            //压栈测试
            System.out.println("=======压栈======");
            ns.push("北京");
            ns.push("上海");
            ns.push("深证");
            ns.push("广州");
            //是否为空
            System.out.println("=======是否为空======");
            System.out.println(ns.isEmpty());
            System.out.println("=============");
    
            System.out.println("=======出栈=======");
            //出栈
            System.out.println(ns.pop());
            System.out.println(ns.pop());
            System.out.println(ns.pop());
            System.out.println(ns.pop());
            System.out.println(ns.pop());
            
            //是否为空
            System.out.println("=======是否为空======");
            System.out.println(ns.isEmpty());
            System.out.println("=============");
            
            
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值