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入门到精通教程
查看: 578|回复: 0

[算法学习]双向循环链表和单链表(java)

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

    [LV.1]初来乍到

    发表于 2014-11-19 00:10:01 | 显示全部楼层 |阅读模式
    java实现双向循环链表和单链表.
    1.双向循环链表
    1. public class LinkedList
    2.    
    3.       {
    4.     private int size = 0;
    5.     private Node
    6.      
    7.        head = new Node
    8.       
    9.        (null, null, null);
    10.     public LinkedList() {
    11.         head.next = head.previous = head;
    12.     }
    13.     public void add(E date) {
    14.         //核心 循环双向链表
    15.                 //新节点的prev指向头结点的prev 新节点的next指向头结点
    16.         Node< E> newNode = new Node< E>(head.previous, date, head);
    17.         newNode.previous.next = newNode;    //调整,新节点的前一个的后一个
    18.         newNode.next.previous = newNode;    //调整,新节点的后一个的前一个
    19.         size++;
    20.     }
    21.     public E get(int index) {
    22.         if (index < 0 || index >= size) {
    23.             throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
    24.         }
    25.         Node< E> node = head;
    26.         if (index < (size >> 1)) {           
    27.             for (int i = 0; i<= index; i++){//head是哑元,i<=index当index=0时,返回head.next
    28.                 node = node.next;           //对头结点进行迭代
    29.             }
    30.         }else{
    31.             for(int i=size;i>index;i--){
    32.                 node=node.previous;
    33.             }
    34.         }
    35.         return node.getData();
    36.     }
    37.     public int size() {
    38.         return size;
    39.     }
    40.     public static void main(String[] args) {
    41.         LinkedList< Integer> list = new LinkedList< Integer>();
    42.         list.add(1);
    43.         list.add(2);
    44.         list.add(3);
    45.         list.add(4);
    46.         list.add(5);
    47.         list.add(6);
    48.         list.add(7);
    49.         list.add(8);
    50.         System.out.println("list.get(3)="+list.get(3));
    51.         for (int i = 0; i < list.size(); i++) {
    52.             System.out.println(list.get(i));
    53.         }
    54.     }
    55. class Node< E> {
    56.     E data;
    57.     Node< E> next;
    58.     Node< E> previous;
    59.     public Node(Node< E> previous, E data, Node< E> next) {
    60.         this.data = data;
    61.         this.next = next;
    62.         this.previous = previous;
    63.     }
    64.     public E getData(){
    65.        return data;
    66.     }
    67.     public void setData(E data){
    68.         this.data=data;
    69.     }
    70. }
    71. }
    72. 2、单链表:
    73. public class SingleLinkedList< T> {
    74.     private int size = 0;
    75.     private Iterator ite;//迭代器
    76.     private Node< T> head, tail;
    77.   
    78.     public SingleLinkedList() {
    79.         head = tail = null;
    80.     }
    81.     public void add(T data) {
    82.         if (size == 0) {
    83.             head = tail = new Node< T>(data, null);
    84.         } else {
    85.             tail.next = new Node< T>(data, null);
    86.             tail = tail.next;
    87.         }
    88.         size++;
    89.     }
    90.     public T get(int index) {
    91.         if (index < 0 || index >= size) {
    92.             throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
    93.         }
    94.         Node< T> node = head;
    95.                 //当index=0时,i不小于index(零不小于零),于是直接return头结点,与linkedList不同,head不是哑元
    96.         for (int i = 0; i < index; i++) {
    97.             node = node.next;           //对头结点进行迭代
    98.         }
    99.         return node.getData();
    100.     }
    101.     public int size(){
    102.         return size;
    103.     }
    104.     public Iterator< T> iterator(){
    105.        return new SingleLinkedListIterator();
    106.     }
    107.     public static void main(String[] args){
    108.         SingleLinkedList< Integer> list=new SingleLinkedList< Integer>();
    109.         list.add(1);
    110.         list.add(2);
    111.         list.add(3);
    112.          list.add(4);
    113.         list.add(5);
    114.         list.add(6);
    115.         for(int i=0;i< list.size();i++){
    116.             System.out.println(list.get(i));
    117.         }
    118.         System.out.println("====================================");
    119.       Iterator< Integer> it=list.iterator();
    120.        while(it.hasNext())
    121.           System.out.println(it.next());
    122.      
    123.     }
    124.   class SingleLinkedListIterator implements Iterator< T>{
    125.    
    126.     private Node< T> current;
    127.    
    128.     public SingleLinkedListIterator(){
    129.         if(size==0) current=null;
    130.         else current=head;
    131.      }
    132.      public boolean hasNext(){
    133.        return current!=null;
    134.      }
    135.       public T next(){
    136.         if(!hasNext()) {
    137.                System.out.println("没有元素了");
    138.                return null;
    139.         }
    140.         else{
    141.              T t=current.getData();
    142.               current=current.next;
    143.             return t;
    144.          }
    145.        }
    146.     }
    147. class Node< T> {
    148.     T data;
    149.     Node< T> next;
    150.     public Node(T data, Node< T> next) {
    151.         this.data = data;
    152.         this.next = next;
    153.     }
    154.    public T getData(){
    155.        return data;
    156.     }
    157.     public void setData(T data){
    158.         this.data=data;
    159.     }
    160. }
    161. interface Iterator< T>{
    162.     boolean hasNext();
    163.     T next();
    164. }
    165. }
    166.       
    167.      
    168.    
    复制代码
    这两个链表最大的不同就是头结点是否是哑元,以及取出元素(get函数)的时候for循环变量i的不同: 双向循环链表(和java.util包的设计一样):由于head是哑元,因此取元素从head的下一个结点取 单向链表:head不是哑元,第一次必须取head头结点的元素,因此循环上和双向循环链表不同。也就是第一次get并没有进入for循环,直接返回了头结点,从第二次才开始进入for循环,这里要特别注意。

       
         
         
          
          

            
          

            
          
         
       

      


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

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2025-2-25 04:45 , Processed in 0.328685 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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