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 FirstLastList {
            public Link first;
            public Link last;
            public FirstLastList() {
                first=null;
                last=null;
            }
            //是否为空
            public boolean isEmpty() {
                return first==null;//last也行。可以选其中一个
            }
            //从last节点位置处 插入
            public void insertLast(long dd) {
                Link newLink=new Link(dd);
                if(isEmpty()) {
                    //如果是空的,first节点需要指向当前插入的节点
                    first=newLink;
                }else
                    last.next=newLink;
                last=newLink;
                    
            }
            //从first节点处 删除
            public long deleteFirst() {
                long temp=first.dData;
                if(first.next==null) {
                    //如果只有一个节点,需要将last为null
                    last=null;
                }
                first=first.next;//改变头(如果只有一个节点,该操作也是null)
                return temp;
                
            }
            //显示所有数据
            public void displayList() {
                Link current=first;
                while(current!=null) {
                    current.displayLink();
                    current=current.next;
                }
                System.out.println();
            }
    
    
    
    
    }
    public class LinkQueue {
        private FirstLastList theList;
        public LinkQueue() {
            theList=new FirstLastList();
        }
        public boolean isEmpty() {
            return theList.isEmpty();
        }
        public void insert(long j) {
            theList.insertLast(j);
        }
        public long remove() {
            return theList.deleteFirst();
        }
        public void displayQueue() {
            System.out.print("QUeue(front-->rear):");
            theList.displayList();
        }
    
    }
    public class Test {
    
        public static void main(String[] args) {
            LinkQueue theQueue =new LinkQueue();
            theQueue.insert(20);
            theQueue.insert(40);
            theQueue.displayQueue();
            theQueue.insert(60);
            theQueue.insert(80);
            theQueue.displayQueue();
            theQueue.remove();
            theQueue.remove();
            theQueue.displayQueue();
    
        }
    
    }
  • 相关阅读:
    Warning: 执行完毕, 但带有警告 trigger trigger_EqPic_insert 已编译。
    c#生成cad缩略图或者图片
    ORACLE ROWNUM解析[转]
    集合已修改;可能无法执行枚举操作。
    JS 变量是否有值的判断
    简单方法解决bootstrap3 modal异步加载只一次的问题
    System.Data.DbType映射关系
    sql zhuan ORACLE
    Enterprise Library
    sql server转oracle需要注意的几点
  • 原文地址:https://www.cnblogs.com/S-Mustard/p/8084892.html
Copyright © 2011-2022 走看看