zoukankan      html  css  js  c++  java
  • [数据结构]-单链表实现栈

    单链表实现栈,是实现了,但是好像思想不是那么对。

    栈的思想。

    我再想想!~~~~

    package com.cn.jichu.day08;
    
    public class LinkedStack<E> {
    
        private class Node{
            private Node next;
            private E data;
    
            public Node(E data) {
                this.data = data;
            }
    
        }
    
        private Node head;
    
        public LinkedStack() {
        }
    
        /**
         * 入栈
         * @param e
         */
        public void push(E e){
            Node node = new Node(e);
            if(head == null){
                head = node;
            }else{
                node.next = head;
                head = node;
            }
        }
    
        /**
         * 出栈
         * @return
         */
        public E pop(){
            if(head == null){
                throw  new IllegalArgumentException("栈中没有元素");
            }
            Node prev = head.next;
            E e = head.data;
            head = prev;
            return e;
        }
    
        /**
         * 获取栈顶元素
         * @return
         */
        public E peek(){
            if(head == null){
                throw  new IllegalArgumentException("栈中没有元素");
            }
            return head.data;
        }
    
    
        @Override
        public String toString(){
            StringBuilder stringBuilder = new StringBuilder();
            Node cur = head;
            stringBuilder.append("stock:[");
            while (cur!=null){
                stringBuilder.append(cur.data + " ");
                cur = cur.next;
            }
            stringBuilder.append("]");
            return stringBuilder.toString();
        }
    
    
    }
  • 相关阅读:
    黄金连分数
    第39级台阶
    四、绘图可视化之Seaborn
    三、绘图和可视化之matplotlib
    二、pandas入门
    python_111_动态导入模块
    爬虫_python3_抓取猫眼电影top100
    爬虫_python3_urllib
    python_112_网络编程 Socket编程
    python_111_异常处理
  • 原文地址:https://www.cnblogs.com/anny0404/p/10653325.html
Copyright © 2011-2022 走看看