zoukankan      html  css  js  c++  java
  • 栈_单链表实现

    //链结点
    public class Link {
        public long dData;
        public Link next;
        public Link(long dd) {
            dData=dd;    
        }
        public void displayLink() {
            System.out.print(dData+" ");
        }
    
    }
    public class LinkList {
            public Link first;
            public LinkList() {
                first=null;
            }
            //是否为空
            public boolean isEmpty() {
                return first==null;
            }
            //插入
            public void insertFirst(long dd) {
                Link newLink=new Link(dd);
                newLink.next=first;//插入头部,需要将next值为以前的first
                first=newLink;//改变frist为现在的插入
                
            }
            //删除头部
            public long deleteFirst() {
                Link temp=first;
                first=first.next;//改变头
                return temp.dData;
                
            }
            //遍历
            public void displayList() {
                Link current=first;
                while(current!=null) {
                    current.displayLink();
                    current=current.next;
                }
                System.out.println();
            }
            
        
    }
    public class LinkStack {
        private LinkList theList;
        public LinkStack() {
            theList=new LinkList();
        }
        public void push(long j) {
            theList.insertFirst(j);
        }
        public long pop() {
            return theList.deleteFirst();
        }
        public boolean isEmpty() {
            return theList.isEmpty();
        }
        public void displayStack() {
            System.out.print("Stack(top-->bottom):");
            theList.displayList();
        }
    
    }
    public class Test {
    
        public static void main(String[] args) {
            LinkStack theStack =new LinkStack();
            theStack.push(20);
            theStack.push(40);
            theStack.displayStack();
            theStack.push(60);
            theStack.push(80);
            theStack.displayStack();
            theStack.pop();
            theStack.pop();
            theStack.displayStack();
            
        }
    
    }
  • 相关阅读:
    系统学习前端
    电脑上的图标拖不动
    js 给 input的value赋值
    js forEach的坑
    h5兼容性问题总结
    行内元素与块级元素
    百度搜索指令
    微信h5监听页面显示隐藏
    跨浏览器事件处理函数
    鼠标事件分析(onmouseenter、onmouseover、onmouseleave和onmouoseout的区别)
  • 原文地址:https://www.cnblogs.com/S-Mustard/p/8084905.html
Copyright © 2011-2022 走看看