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();
            
        }
    
    }
  • 相关阅读:
    REST API注意事项
    Javascript addEventListener dispatchEvent
    Javascript常见操作
    MySql运算符
    Mysql数据类型
    MySql基本命令
    php学习
    javascript学习
    如何快速掌握一种技术
    站在K2角度审视流程--任务的独占与释放
  • 原文地址:https://www.cnblogs.com/S-Mustard/p/8084905.html
Copyright © 2011-2022 走看看