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();
            
        }
    
    }
  • 相关阅读:
    redis复制
    redis发布订阅
    redis事务
    redis持久化
    redis.conf 常见配置介绍
    redis数据类型
    redis集群安装
    redis单机版安装
    eclipse下svn的使用
    算法五最长回文子串
  • 原文地址:https://www.cnblogs.com/S-Mustard/p/8084905.html
Copyright © 2011-2022 走看看