zoukankan      html  css  js  c++  java
  • 数据结构-链表

      链表:

      简单链表:是一个非连续性的存贮结构,数据元素上的逻辑循序是通过链表上的指针按次序实现的;简单来说,就像是个火车一样,每节车厢代表一个节点,链接车厢的钩子就是所谓的指针,车厢有数据域和指针域;

      图解:指针域和数据域分别连接后一个节点;

      

      上代码:

      创建节点对象;

     1 package link;
     2 /**
     3  * 链表
     4  * @author caizhou
     5  *
     6  */
     7 public class Node {
     8     
     9     /**
    10      * 链结点;
    11      */
    12     
    13     public long data;
    14     public Node next;
    15     
    16     
    17     
    18     public Node(long value){
    19         data = value;
    20     }
    21     
    22     public void display(){
    23         System.out.println(data + " ");
    24     }
    25 }

      节点的插入,删除,显示操作;

     1 package link;
     2 
     3 public class linkList {
     4     
     5     private Node first;
     6     
     7     
     8     public linkList(){
     9         first = null;
    10         
    11     }
    12     /**
    13      * 插入节点,从头节点插入;
    14      * @param value
    15      */
    16     public void insertFirst(long value){
    17         Node me = new Node(value);
    18         if(first == null){
    19             first = me;
    20         }else{
    21             me.next = first.next;
    22             first.next = me; 
    23         }
    24         
    25     }
    26     /**
    27      * 删除节点,从头节点删除;
    28      * @return
    29      */
    30     public Node deletNode(){
    31         Node me = first;
    32         first = me.next;
    33         return me;
    34         
    35     }
    36     
    37     public void display(){
    38         Node current = first;
    39         while(current != null){
    40             System.out.print(current.data + " ");
    41             current = current.next;
    42         }
    43     }
    44 }

      测试链表:

      

     1 package link;
     2 
     3 public class testLink {
     4     public static void main(String[] args) {
     5         
     6         linkList lik = new linkList();
     7         lik.insertFirst(23);
     8         lik.insertFirst(32);
     9         lik.insertFirst(21);
    10         lik.insertFirst(26);
    11         lik.insertFirst(40);
    12         lik.display();
    13         System.out.println();
    14        
    15         lik.deletNode();
    16         lik.display();
    17         
    18     }
    19 }

      显示结果:

    23 40 26 21 32 
    40 26 21 32

      双端链表和双向链表:

      双端链表:有头节点和尾节点的链表,叫做双端链表;

      双端链表实现插入,删除操作代码:

      

     1 package link;
     2 
     3 /**
     4  * 双端列表:有头尾节点;
     5  * @author caizhou
     6  *
     7  */
     8 public class linkLastList {
     9     
    10     
    11 
    12     
    13         
    14         private Node first;
    15         private Node last;
    16         
    17         
    18         public linkLastList(){
    19             first = null;
    20             last = null;
    21             
    22         }
    23         /**
    24          * 从头节点插入;
    25          * @param value
    26          */
    27         public void insertFirst(long value){
    28             Node me = new Node(value);
    29             if(isEmpty()){
    30                 last = me;
    31             }else{
    32                 me.next = first;
    33                 
    34             }
    35             first = me;
    36             
    37         }
    38         /**
    39          * 从尾节点进行插入;
    40          */
    41         public void insertLast(long value){
    42             Node me = new Node(value);
    43             if(isEmpty()){
    44                 first = me;
    45             }
    46             else{
    47                 last.next = me;
    48                 
    49             }
    50             last = me;
    51         }
    52         /**
    53          * 删除头节点;
    54          * @return
    55          */
    56         public Node deleteFirstNode(){
    57             Node me = first;
    58             if(first == null ){
    59                 last = null;
    60             }else{
    61                 first = me.next;
    62             }
    63             
    64             return me;
    65             
    66         }
    67         
    68         
    69         public void display(){
    70             Node current = first;
    71             while(current != null){
    72                 System.out.print(current.data + " ");
    73                 current = current.next;
    74             }
    75         }
    76         public Boolean isEmpty(){
    77             return (first == null);
    78         }
    79     }
     

     测试双端链表:
     

     1 package link;
     2 
     3 public class testLink {
     4     public static void main(String[] args) {
     5         
     6         linkLastList lik = new linkLastList();
     7         lik.insertFirst(23);
     8         lik.insertFirst(32);
     9         lik.insertFirst(21);
    10         lik.insertFirst(26);
    11         lik.insertFirst(40);
    12         lik.display();
    13         System.out.println();
    14         
    15         lik.deleteFirstNode();
    16         lik.display();
    17         
    18     }
    19 }
     

      测试结果:

    40 26 21 32 
    26 21 32 

      双向链表:指的是每个车厢节点除了包含对下一节车厢的引用,还包含对上一节车厢的引用;

       创建双向链表的next和privous;

      

     1 package link;
     2 
     3 public class DoubleNode {
     4 
     5     /**
     6      * 链结点;
     7      */
     8     
     9     public long data;
    10     public DoubleNode next;
    11     public DoubleNode privous;
    12     
    13     
    14     
    15     public DoubleNode(long value){
    16         data = value;
    17     }
    18     
    19     public void display(){
    20         System.out.println(data + " ");
    21     }
    22 }

      双向链表的插入规则:从头节点插入,如果头节点为空,则设置插入节点为尾节点,如果不为空,那么头节点的privous指向要插入的节点,要插入的节点的next指向头节点,新的头节点指向新插入的节点;从尾节点插入,同样判断如果first节点为空,则设置插入节点为头节点,如果不为空,那么设置尾节点的next为要插入的节点,同时设置插入节点的privous为尾节点,新的尾节点指向新插入的节点;

      图解操作:我觉得这张图应该很好理解了;

      

      直接上代码:

      

     1 package link;
     2 
     3 import javax.swing.border.EmptyBorder;
     4 
     5 /**
     6  * 双向列表:除了对有下一个节点的引用,也有对前一个结点的引用;
     7  * @author caizhou
     8  *
     9  */
    10 public class DoubleLinkList {
    11     
    12     
    13     private DoubleNode first;
    14     private DoubleNode last;
    15     
    16     public DoubleLinkList(){
    17         
    18         first = null;
    19         last = null;
    20     }
    21     /**
    22      * 插入链表;从头节点插入;
    23      */
    24     public void insertfirst(long value ){
    25         
    26         DoubleNode me = new DoubleNode(value);
    27         if(Empty()){
    28             last = me;
    29         }else{
    30             first.privous = me;
    31             me.next = first;
    32         }
    33         
    34         first = me;
    35     }
    36     /*
    37      * 插入一个节点,从尾节点插入;后一个节点指向插入节点,同时设置插入节点的privous指向前一个节点
    38      */
    39     public void insertLast(long value){
    40         DoubleNode me = new DoubleNode(value);
    41         if(Empty()){
    42             first = me;
    43         }else{
    44             last.next = me;
    45             me.privous = last;
    46         }
    47         last = me;
    48     }
    49     
    50     public void display(){
    51         DoubleNode current = first;
    52         while(current != null){
    53             System.out.print(current.data + " ");
    54             current = current.next;
    55         }
    56     }
    57     public boolean Empty(){
    58         return (first == null);
    59     }
    60 }

       测试双向链表的插入操作:

      

     1 package link;
     2 
     3 public class testDoubleLink {
     4     
     5     public static void main(String[] args) {
     6         DoubleLinkList li = new DoubleLinkList();
     7         li.insertLast(23);
     8         li.insertLast(32);
     9         li.insertLast(34);
    10         li.insertLast(15);
    11         li.display();
    12     }
    13 }

      测试结果:

      

    23 32 34 15 

      从头节点插入也是一样,直接测试即可;

      双向链表的删除操作:从头节点删除,同样是判断节点是否为空,如果为空则设置尾节点为null,如果不为空,则设置头节点的next的privous为null,然后新的头节点指向当前删除节点的下一个节点,则表示删除成功;从尾节点删除,同样是判断头节点是否为空,如果为空,则设置尾节点为null,如果不为空,则设置尾节点的privous的next为null,同时把新的尾节点设置成尾节点的next;

      图解删除操作:

      

      直接上代码:

      

     1 package link;
     2 
     3 import javax.swing.border.EmptyBorder;
     4 
     5 /**
     6  * 双向列表:除了对有下一个节点的引用,也有对前一个结点的引用;
     7  * @author caizhou
     8  *
     9  */
    10 public class DoubleLinkList {
    11     
    12     
    13     private DoubleNode first;
    14     private DoubleNode last;
    15     
    16     public DoubleLinkList(){
    17         
    18         first = null;
    19         last = null;
    20     }
    21         /*
    22      * 从头节点删除;
    23      */
    24     public DoubleNode deleteFirst(){
    25         DoubleNode me = first;
    26         if(first.next == null){
    27             last = null;
    28         }else{
    29             first.next.privous = me;
    30         }
    31         first = me.next;
    32         return me;
    33     }
    34 public DoubleNode deleteLast(){ 35 DoubleNode me = last; 36 if(first.next == null){ 37 last = null; 38 }else{ 39 last.privous.next = null; 40 } 41 last = me.privous; 42 return me; 43 } 44 45 public void display(){ 46 DoubleNode current = first; 47 while(current != null){ 48 System.out.print(current.data + " "); 49 current = current.next; 50 } 51 } 52 public boolean Empty(){ 53 return (first == null); 54 } 55 } 56

      测试代码:

      

     1 package link;
     2 
     3 public class testDoubleLink {
     4     
     5     public static void main(String[] args) {
     6         DoubleLinkList li = new DoubleLinkList();
     7 
     8         li.insertLast(23);
     9         li.insertLast(32);
    10         li.insertLast(34);
    11         li.insertLast(15);
    12         li.display();
    13         System.out.println();
    14         li.deleteFirst();
    15         li.display();
    16     }
    17 }

      测试结果:

    23 32 34 15 
    32 34 15 

      至此,链表的基本应用已经讲完了,希望能够帮到你,谢谢!

      

      

      

       

  • 相关阅读:
    void及void指针含义的深刻解析
    对个人站长职业前景的探讨之路在何方?
    Swift编程语言学习4.3—— 控制语句
    二分查找
    分布式文件系统
    常见浏览器兼容性问题与解决方式
    OutputDebugString()
    眼睛的颜色
    SVM-支持向量机算法概述
    Android学习笔记(四十):Preference的使用
  • 原文地址:https://www.cnblogs.com/caizhou520/p/12608291.html
Copyright © 2011-2022 走看看