java 链表模型

首先给出一个链表模型:



第一步:  创建空链表   



第二步:创建头节点



第三部:创建尾节点





到此为止 一个比较有完整意义的链表已经构造出 

增加节点




删除节点:



总结:我们可以看到链表在增加节点和删除节点的时候,只需要改变next指针的指向,就能达到我们想要的操作目的。而对于数组来说,增加和删除一个元素都要将目标删除元素后面的每个元素往前移动一位。 如果增删很频繁的话,链表会比数组会更快。对于遍历操作而言,链表需要通过next指针遍历每一个元素,相对于数组遍历直接访问下标来说,会比较慢。  所以在增删比较频繁的时候我们应该考虑是否使用链表这种数据结构。

贴出代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package test;  
  2.   
  3. public class LinkList<T> {   
  4.       
  5.     private Node<T> head; //链表的头节点  
  6.     private Node<T> tail; //链表的尾节点  
  7.       
  8.     /** 
  9.      * 构造一个空链表 
  10.      */  
  11.     public LinkList() {   
  12.         head = tail = null;      
  13.     }    
  14.       
  15.     /** 
  16.      * 链表内部的节点类 
  17.      */  
  18.     private static class Node<T> {    
  19.         T data;//节点的数据  
  20.         Node<T> next;//该节点的指向下一个节点的指针  
  21.           
  22.         Node(T data) {   
  23.             this.data = data;    
  24.             this.next = null;     
  25.         }    
  26.     
  27.     }    
  28.       
  29.     public void addHead(T point) {//为空链表增加头结点      
  30.         this.head = new Node<T>(point);    
  31.         if(tail == null) {    
  32.             tail = head;    
  33.         }    
  34.     }    
  35.       
  36.     public void addTail(T point){//为链表增加尾节点    
  37.         tail = new Node<T>(point);    
  38.         head.next = tail;    
  39.     }    
  40.       
  41.     public void insert(T point) {  
  42.         if (this.head == null) {  
  43.             addHead(point);  
  44.               
  45.         } else if (this.tail == this.head) {  
  46.             addTail(point);  
  47.               
  48.         } else {  
  49.             Node<T> preNext = head.next;    
  50.             @SuppressWarnings({ "unchecked""rawtypes" })  
  51.             Node<T> newNode = new Node(point);    
  52.             preNext = head.next;    
  53.             this.head.next = newNode;    
  54.             newNode.next = preNext;    
  55.         }  
  56.            
  57.     }    
  58.       
  59.     public void printLinkList() {    //打印链表  
  60.         Node<T> curr = this.head;    
  61.         if (isEmpty()) {    
  62.             try {  
  63.                 throw new Exception("linklist is empty");  
  64.             } catch (Exception e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.           
  69.         while(curr != null){    
  70.             System.out.print(curr.data+" ");        
  71.             curr = curr.next;    
  72.         }     
  73.     }    
  74.       
  75.     public void delete(T data){//删除某一节点  
  76.         Node<T> curr = head, prev = null;  
  77.         boolean suc = false;//是否删除成功标志  
  78.         while (curr != null) {  
  79.             if (curr.data.equals(data)) {  
  80.                 //判断是什么节点  
  81.                 if (curr == head) {   //如果删除的是头节点  
  82.                     System.out.println('\n'+"delete head node");  
  83.                     head = curr.next;  
  84.                     suc = true;  
  85.                     return;  
  86.                 }  
  87.                 if (curr == tail) { //如果删除的是尾节点  
  88.                     System.out.println('\n'+"delete tail node");  
  89.                     tail = prev;  
  90.                     prev.next = null;  
  91.                     suc = true;  
  92.                     return;  
  93.                 } else {//如果删除的是中间节点(即非头节点或非尾节点)  
  94.                     System.out.println('\n'+"delete center node");  
  95.                     prev.next = curr.next;  
  96.                     suc = true;  
  97.                     return;  
  98.                 }  
  99.             }  
  100.   
  101.             prev = curr;  
  102.             curr = curr.next;     
  103.         }  
  104.           
  105.         if(suc == false) {  
  106.             System.out.println('\n'+"没有这个数据");  
  107.         }     
  108.       
  109.     }  
  110.       
  111.     public boolean isEmpty(){//判断链表是否为空  
  112.         return this.head == null || this.tail == null;  
  113.     }   
  114.   
  115.     public static void main(String[] args) {    
  116.         LinkList<Integer> mylist = new LinkList<Integer>();//构造一个空链表    
  117.         mylist.insert(5);    
  118.         mylist.insert(6);    
  119.         mylist.insert(7);    
  120.         mylist.insert(3);    
  121.         mylist.printLinkList();    
  122.         mylist.delete(1);  
  123.         mylist.printLinkList();    
  124.         mylist.delete(5);  
  125.         mylist.printLinkList();  
  126.         mylist.delete(6);  
  127.         mylist.printLinkList();    
  128.     }    
  129.     
  130. }   




输出结果:

5 3 7 6 
没有这个数据
5 3 7 6 
delete head node
3 7 6 
delete tail node
3 7 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值