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

    双向链表不一定是双端的,不用必须有last节点

    //链结点
    public class Link {
        public long dData;
        public Link next;
        public Link previous;
        public Link(long d) {
            dData=d;    
        }
        public void displayLink() {
            System.out.print(dData+" ");
        }
    
    }
    public class DoublyLinkedList {
        private Link first;
        private Link last;
        public DoublyLinkedList() {
            first=null;
            last=null;
        }
        //判断是否为空
        public boolean isEmpty() {
            return first==null;
        }
        //从first插入
        public void insertFirst(long dd) {
            Link newLink=new Link(dd);
            if(isEmpty()) {
                last=newLink;
            }else
                first.previous=newLink;
            newLink.next=first;
            first=newLink;
        }
        //从last处增加
        public void insertLast(long dd) {
            Link newLink=new Link(dd);
            if(isEmpty()) {
                first=newLink;
            }else {
                last.next=newLink;
                newLink.previous=last;
            }
            last=newLink;
                
        }
        //从first删除
        public Link deleteFirst() {
            Link temp=first;
            if(first.next==null) {
                last=null;
            }else
                first.next.previous=null;
            first=first.next;
            return temp;
        }
        //从last删除
        public Link deleteLast() {
            Link temp=last;
            if(first.next==null) {
                first=null;
            }else
                last.previous.next=null;
            last=last.previous;
            return temp;
        }
        //从中间插入(指定位置)
        public boolean insertAfter(long key,long dd) {
            Link current=first;
            while(current.dData!=key) {
                current=current.next;
                if(current==null)
                    return false;//插入不成功
            }
            //找到需要插入的点
            Link newLink=new Link(dd);
           //如果是最后一个
            if(current==last) {
                newLink.next=null;
                last=newLink;
            }else {
                newLink.next=current.next;
                current.next.previous=newLink;
            }
            newLink.previous=current;
            current.next=newLink;
            return true;
            
            
        }
        //delete key
        public Link deleteKey(long key) {
            Link current=first;
            //找到key的位置
            while(current.dData!=key) {
                current=current.next;
                if(current==null)
                    return null;
            }
            if(current==first) {
                first=current.next;
            }else current.previous.next=current.next;
            
            if(current==last) {
                last=current.previous;
            }else
                current.next.previous=current.previous;
            
            return current;//返回要删除的数据项
        }
        //从前向后
        public void displayForward() {
            System.out.print("List(first-->last):");
            Link current=first;
            while(current!=null) {
                current.displayLink();
                current=current.next;
            }
            System.out.println();
        }
        //从后向前
        public void displayBackward() {
            System.out.print("List(last-->first):");
            Link current=last;
            while(current!=null) {
                current.displayLink();
                current=current.previous;
            }
            System.out.println();
        }
    
    }
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            DoublyLinkedList theList=new DoublyLinkedList();
            theList.insertFirst(22);
            theList.insertFirst(44);
            theList.insertFirst(66);
            theList.insertLast(11);
            theList.insertLast(33);
            theList.insertLast(55);
            theList.displayForward();
            theList.displayBackward();
            theList.deleteFirst();
            theList.deleteLast();
            theList.deleteKey(11);
            theList.displayForward();
        
    
        }
    
    }
  • 相关阅读:
    年度总结会议
    2018年度移动创新竞赛组总结
    移动竞赛组2019计划
    task ':app:mergeDebugResources' property 'aapt2FromMaven'
    SQL SERVER : 'GO' 附近有语法错误问题
    Visual C# 访问 SQLserver 数据库
    数据库系统概论 第三章 课后作业
    数据库系统概论 第三章 建表&插入SQL语句
    【pyqt5 QtDesigner SQLserver2017】 Python3.6 Demo
    【Win10 + PyCharm + Python + PyQt5 + pymssql】 Python 3.6 访问 SQLserver 2017(对话框界面)1
  • 原文地址:https://www.cnblogs.com/S-Mustard/p/8067094.html
Copyright © 2011-2022 走看看