Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 495|回复: 0

[算法学习]循环链表(java实现)

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-19 00:10:01 | 显示全部楼层 |阅读模式
    循环链表是与单链表一样,是一种链式的存储结构,所不同的是,循环链表的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而构成一个环形的链。  循环链表的运算与单链表的运算基本一致。所不同的有以下几点:  1、在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。   2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。  代码:
    1. public class CircularLinkedList< E> {
    2.     private Node< E> head;
    3.     private int length;        //the length of the list
    4.    
    5.     public CircularLinkedList() {
    6.         head = new Node< E>(null, head);
    7.         length = 0;
    8.     }
    9.    
    10.    
    11.     public void insertAtPrior(E item) {//在头节点之后加一个节点
    12.         Node< E> node = new Node< E>(item, null);    //encpsule the item to an node
    13.         node.setNext(head.getNext());    //node.next = head.next
    14.         head.setNext(node);    //head.next = node
    15.         length ++;
    16.     }
    17.    
    18.     public void add(E item) {//在最后节点上添加一个节点
    19.         
    20.         Node< E> tmp = head;
    21.         
    22.         if (isEmpty()) {    // if the list is null
    23.             Node< E> node = new Node< E>(item, head);    // .. if next == null ?
    24.             head.setNext(node);
    25.         } else {
    26.             Node< E> node = new Node< E>(item, head);
    27.             // find the end node of the list
    28.             while (head != tmp.getNext()) {
    29.                 tmp = tmp.getNext();
    30.             }
    31.             tmp.setNext(node);
    32.         }
    33.         
    34.         length++;
    35.     }
    36.    
    37.      public E get(int index) { //获取索引处的节点的数据
    38.        // 先判断索引正确性   
    39.         if (index >length || index < 0) {   
    40.           throw new RuntimeException("索引值有错:" + index);   
    41.         }else if(isEmpty()){
    42.             return null;
    43.         }else{
    44.           Node< E> tmp =head;   
    45.           int i= 1;   
    46.           while (head != tmp.getNext() && i <= index) {
    47.                 tmp = tmp.getNext();
    48.                 i++;
    49.             }
    50.            E e = tmp.getData();   
    51.           return e;   
    52.        }
    53.     }   
    54.     public void insert(int index, E item) {//在索引处后添加节点
    55.         Node< E> node = new Node< E>(item, null);
    56.         Node< E> tmp = head;
    57.         int i = 1;
    58.         
    59.         if (index > length || index < 0) {
    60.             System.out.println("the index is out of bounds");
    61.         } else if (0 == length && 1 == index) {
    62.             node.setNext(head);
    63.             head.setNext(node);
    64.             length++;
    65.         } else {
    66.             //find the node index
    67.             while (head != tmp.getNext() && i <= index) {
    68.                 tmp = tmp.getNext();
    69.                 i++;
    70.             }
    71.             node.setNext(tmp.getNext());
    72.             tmp.setNext(node);
    73.             length++;
    74.         }
    75.     }
    76.    
    77.     public void removeFromFront() {//删除头节点之后的第一个节点
    78.         Node< E> tmp = head;
    79.         if (length < 1) {
    80.             System.out.println("The list is null and you can not delete any node!");
    81.         } else if (1 == length) {
    82.             head.setNext(head);
    83.             length--;
    84.         } else {
    85.             head.setNext(tmp.getNext().getNext());
    86.             length--;
    87.         }
    88.     }
    89.    
    90.     public void remove(int index) {//删除索引处的节点
    91.         if (length < 1 || index > length) {
    92.             System.out.println("index is out of bounds");
    93.         } else if (1 == length && 1 == index) {
    94.             head.setNext(head);
    95.             length--;
    96.         } else {
    97.             Node< E> tmp = head;
    98.             int i = 1;
    99.             
    100.             //get the node before index
    101.             while (head != tmp.getNext() && i < index) {
    102.                 tmp = tmp.getNext();
    103.                 i++;
    104.             }
    105.             tmp.setNext(tmp.getNext().getNext());
    106.             length--;
    107.         }
    108.     }
    109.    
    110.    
    111.     public void removeFromLast() {//删除最后一个节点
    112.         if (length < 1) {     // if the list is null
    113.             System.out.println("The list is null and you can not delete");
    114.         } else if (1 == length) {
    115.             head.setNext(head);
    116.             length--;
    117.         } else {
    118.             Node< E> tmp1 = head;
    119.             Node< E> tmp2 = head.getNext(); //set tmp2 -tmp1 = 1
    120.             
    121.             while (head != tmp2.getNext()) {
    122.                 tmp2 = tmp2.getNext();
    123.                 tmp1 = tmp1.getNext();
    124.             }
    125.             tmp1.setNext(head);
    126.             length--;
    127.         }
    128.     }
    129.    
    130.    
    131.     public int getLength() {
    132.         return length;
    133.     }
    134.    
    135.     public boolean isEmpty() {
    136.           return length==0;
    137.     }
    138.    
    139.    
    140.     public void display() {
    141.         if (length < 1) {
    142.             System.out.println("The list is null");
    143.         } else {
    144.             Node< E> tmp = head;
    145.             while (head != tmp.getNext()) {
    146.                 tmp = tmp.getNext();
    147.                 System.out.print(tmp.getData() + " ");
    148.             }
    149.         }
    150.     }
    151.     //test the list
    152.     public static void main(String[] args) {
    153.         CircularLinkedList< Integer> l = new CircularLinkedList< Integer>();
    154.         System.out.println(l.isEmpty());
    155.         l.add(1);
    156.         l.add(2);
    157.         l.insertAtPrior(3);
    158.         l.insert(2, 4);
    159.         l.add(5);
    160.         System.out.println("the list is : ");
    161.         l.display();
    162.         System.out.println();
    163.         System.out.println("the length is :" + l.getLength());
    164.         l.removeFromFront();
    165.         l.removeFromLast();
    166.         l.display();
    167.         
    168.        // System.out.println(l.get(3));
    169.     }
    170. }
    171. public class Node< E> {
    172.     private Node< E> next;
    173.     private E data;
    174.    
    175.     public Node(E data) {
    176.         this.data = data;
    177.         next = null;
    178.     }
    179.    
    180.     public Node(E data, Node< E> nextNode) {
    181.         this.data = data;
    182.         next = nextNode;
    183.     }
    184.    
    185.    
    186.     public void setData(E data) {
    187.         this.data = data;
    188.     }
    189.    
    190.    
    191.     public void setNext(Node< E> next) {
    192.         this.next = next;
    193.     }
    194.    
    195.    
    196.     public E getData() {
    197.         return data;
    198.     }
    199.    
    200.    
    201.     public Node< E> getNext() {
    202.         return next;
    203.     }
    204. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/19/001001562.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:52 , Processed in 0.333441 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表