LinkedList和单双链表。

王柳  论坛元老 | 2024-11-12 23:24:28 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1892|帖子 1892|积分 5676

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
java中提供了双向链表的动态数据结构 --- LinkedList,它同时也实现了List接口,可以看成平凡的列表来使用。也可以自界说实现链表。
单向链表:一个节点=本节点数据+下个节点地点
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。
  1. public class Lee {
  2.     private Node head1;
  3.     private Node head2;
  4.     Lee(){
  5.         this.head1= new Node(0);
  6.         this.head2= new Node(0);
  7.     }
  8.     public void  insert1(int data){
  9.         Node newNode = new Node(data);
  10.         Node curNode = head1;
  11.         while (curNode.next!=null){
  12.             curNode=curNode.next;
  13.         }
  14.         curNode.next=newNode;
  15.     }
  16.     public void  insert2(int data){
  17.         Node newNode = new Node(data);
  18.         Node curNode = head2;
  19.         while (curNode.next!=null){
  20.             curNode=curNode.next;
  21.         }
  22.         curNode.next=newNode;
  23.     }
  24.      static class Node{
  25.         public int value;
  26.         public Node next;
  27.          Node(int data){
  28.             this.value=data;
  29.             this.next=null;
  30.         }
  31.     }
  32.     public void Plink(Node head1,Node head2){
  33.         while (head1!=null&&head2!=null){
  34.             if (head1.value<head2.value)
  35.                 head1=head1.next;
  36.             else if (head1.value>head2.value)
  37.                 head2=head2.next;
  38.             else{
  39.                 System.out.println(head1.value+" ");
  40.                 head1=head1.next;
  41.                 head2=head2.next;
  42.             }
  43.         }
  44.     }
  45.     public static void main(String[] args) {
  46.         Lee lee = new Lee();
  47.         lee.insert1(1);
  48.         lee.insert1(3);
  49.         lee.insert1(4);
  50.         lee.insert2(4);
  51.         lee.insert2(5);
  52.         lee.insert2(6);
  53.         lee.Plink(lee.head1, lee.head2);
  54.     }
  55. }
复制代码

双向链表:一个节点=上个节点地点+本节点数据+下个节点地点
如:界说两个函数,实如今双向链表的头部及尾部插入节点
  1. public class Lee {
  2.     private Node head;
  3.     Lee(){
  4.         this.head= new Node(0);
  5.     }
  6.     public void  insertHead(int data){
  7.         Node newNode = new Node(data);
  8.         newNode.next=head;
  9.         head.pre=newNode;
  10.         head=newNode;
  11.     }
  12.     public void  insertTail(int data){
  13.         Node newNode = new Node(data);
  14.         Node current = head;
  15.         while (current.next!=null){
  16.             current=current.next;
  17.         }
  18.         current.next=newNode;
  19.         newNode.pre=current;
  20.     }
  21.     public void printList(Node head) {
  22.         Node current = head;
  23.         // 从头到尾打印链表
  24.         while (current != null) {
  25.             System.out.print(current.value + " -> ");
  26.             current = current.next;
  27.         }
  28.         System.out.println("null"); // 表示链表结尾
  29.     }
  30.     static class Node{
  31.         public int value;
  32.         public Node pre;
  33.         public Node next;
  34.          Node(int data){
  35.             this.value=data;
  36.             this.pre=null;
  37.             this.next=null;
  38.         }
  39.     }
  40.     public static void main(String[] args) {
  41.         Lee lee = new Lee();
  42.         lee.insertTail(2);
  43.         lee.insertTail(3);
  44.         lee.insertTail(4);
  45.         lee.insertHead(4);
  46.         lee.printList(lee.head);
  47.     }
  48. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

王柳

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表