zoukankan      html  css  js  c++  java
  • 链表_双端链表(插入,遍历)

    双端链表的表中跟单链表比较。多了一个last节点。其last节点指向first节点指向的对立面。

    //链结点
    public class Link {
        public long dData;
        public Link next;
        public Link(long d) {
            dData=d;    
        }
        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也行。可以选其中一个
            }
            //从first节点位置处 插入
            public void insertFirst(long dd) {
                Link newLink=new Link(dd);
                if(isEmpty()) {
                    //如果当前链表是空的,将last指向dd节点(在方法的结尾处会综合将first指向新增的节点)
                    last=newLink ;
                }else
                //如果不是空的,只用操作first
                newLink.next=first;//插入头部,需要将next值指向以前的first
                //不是空和是空的情况都需要进行的操作
                first=newLink;//改变frist指向,为现在的插入的节点
                
            }
            //从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() {
                System.out.println("List(First-->last):");
                Link current=first;
                while(current!=null) {
                    current.displayLink();
                    current=current.next;
                }
                System.out.println();
            }
    
    
    
    
    }
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            FirstLastList theList=new FirstLastList();
            theList.insertFirst(22);
            theList.insertFirst(44);
            theList.insertFirst(66);
            theList.insertLast(11);
            theList.insertLast(33);
            theList.insertLast(55);
            theList.displayList();
        
    
        }
    
    }
  • 相关阅读:
    php -- php数组相关函数
    php -- 数组排序
    php -- in_array函数
    php -- 魔术方法 之 删除属性:__unset()
    无符号整型与有符号整型相运算规则
    N个节点的二叉树有多少种形态
    getopt_long
    typedef
    约瑟夫环问题算法(M)
    C语言基础
  • 原文地址:https://www.cnblogs.com/S-Mustard/p/8066566.html
Copyright © 2011-2022 走看看