zoukankan      html  css  js  c++  java
  • 顺序栈,链栈,队列java实现

    顺序栈

    /**
     * 顺序栈
     * */
    public class SqStack {
        //栈的大小
        private int maxSize;
        //栈顶指针
        private int top;
    
        private char[] stack;
    
        public SqStack(int size){
            maxSize = size;
            top = -1;
            stack = new char[maxSize];
        }
        //压栈
        public void push(char value){
            stack[++top] = value;
        }
        //出栈
        public char pop(){
            return stack[top--];
        }
        //返回栈顶指针
        public char peek(){
            return stack[top];
        }
        //栈是否满
        public boolean idFull(){
            return maxSize-1==top;
        }
        //栈是否为空
        public boolean isEmpty(){
            return top==-1;
        }
    }
    

      

    测试

    SqStack sqStack = new SqStack(10);
            sqStack.push('a');
            sqStack.push('b');
            System.out.println(sqStack.pop());
            System.out.println(sqStack.pop());
    

      

    输出

    链栈

    import sun.awt.image.ImageWatched;
    
    /**
     * 链栈
     * */
    public class LinkStack {
        /**
         * @param size 栈的大小
         * @param top 栈顶指针
         * */
        private int size;
        private Node top =null;
    
        class Node{
            int data;
            Node next = null;
            public Node(int data){
                this.data = data;
            }
        }
    
        /**
         * 压栈
         * @param data 节点数据
         * */
        public void push(int data){
            Node node = new Node(data);
            node.next = top;
            top = node;
            size++;
        }
    
        /**
         * 出栈
         * */
        public int pop()throws Exception{
            if(top==null)
                throw new Exception("空的");
            else {
                int data = top.data;
                top = top.next;
                size--;
                return data;
            }
        }
        public int getSize(){
            return size;
        }
        public boolean isEmpty(){
            return size==0;
        }
        public void showAllNode() throws Exception{
            if(top==null)
                throw new Exception("空栈");
            else {
                Node node = top;
                while (node!=null){
                    System.out.println(node.data);
                    node = node.next;
                }
            }
        }
    }
    

      

    测试

    LinkStack stack = new LinkStack();
            stack.push(2);
            stack.push(3);
            stack.showAllNode();
    

      

    结果

     队列

    点类

    public class Node<T> {
        //存储的数据
        private T data;
        //下一个节点
        private Node<T> next;
    
        public Node(T data){
            this.data = data;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public Node<T> getNext() {
            return next;
        }
    
        public void setNext(Node<T> next) {
            this.next = next;
        }
    }
    

      

    队列类

    public class LinkQuene {
        //头
        private Node<Integer> front;
        //尾
        private Node<Integer> rear;
        //大小
        private int size;
    
        /**
         * 创建队列
         * */
        public LinkQuene(){
            front = rear = null;
        }
        /**
         * 入列
         * @param data 节点数据
         * */
        public void enter(Integer data){
            Node<Integer> node = new Node<>(data);
            if(isEmpty()){
                front = rear = node;
            }else{
                rear.setNext(node);
                rear = node;
            }
            size++;
        }
        /**
         * 出列
         * */
        public Integer out(){
            Node node = new Node(-1);
            if(isEmpty()){
                System.out.println("队列是空的");
                return (Integer) node.getData();
            }else {
                node = front;
                front = node.getNext();
                node.setNext(null);
                size--;
            }
            if(size==0){
                front = null;
                rear  = null;
            }
            return (Integer) node.getData();
        }
    
        /**
         * 判断是否为空
         * */
        public boolean isEmpty(){
            return front==null&&rear==null?true:false;
        }
        /**
         * 得到个数
         * */
        public int getSize(){
            return this.size;
        }
    }
    

      

    测试:

    LinkQuene quene = new LinkQuene();
            quene.enter(1);
            quene.enter(2);
            System.out.println(quene.getSize());
            System.out.println(quene.out());
            System.out.println(quene.getSize());
    

      

    结果

  • 相关阅读:
    CF163E e-Government
    P2336 [SCOI2012]喵星球上的点名
    数据结构
    数字逻辑
    建筑制图与识图
    建筑施工
    电力系统分析
    现代物流基础
    电子商务网站设计与管理
    数字电子技术基础
  • 原文地址:https://www.cnblogs.com/Yintianhao/p/9971571.html
Copyright © 2011-2022 走看看