java自定义List链表

第一步:定义一个List接口,规定一些基本操作

[java]  view plain copy
  1. package my.stack;  
  2.   
  3. public class Node<T> {  
  4.     private T data;  
  5.       
  6.     private Node<T> next;  
  7.       
  8.     public Node(){  
  9.         data = null;  
  10.         next = null;  
  11.     }  
  12.       
  13.     public Node(T data){  
  14.         this.data = data;  
  15.         this.next = null;  
  16.     }  
  17.       
  18.     public Node(T data, Node<T> next){  
  19.         this.data = data;  
  20.         this.next = next;  
  21.     }  
  22.       
  23.     public void setData(T data){  
  24.         this.data = data;  
  25.     }  
  26.       
  27.     public T getData(){  
  28.         return this.data;  
  29.     }  
  30.       
  31.     public void setNext(Node<T> next){  
  32.         this.next = next;  
  33.     }  
  34.       
  35.     public Node<T> getNext(){  
  36.         return this.next;  
  37.     }  
  38. }  


第二步:实现该接口定义的函数

1)由于是链表,首先要定义一个节点类(可以作为内部类实现)

[java]  view plain copy
  1. package my.list;  
  2.   
  3. public class Node<T> {  
  4.     private T data;  
  5.       
  6.     private Node<T> next;  
  7.       
  8.     public Node(){  
  9.         data = null;  
  10.         next = null;  
  11.     }  
  12.       
  13.     public Node(T data){  
  14.         this.data = data;  
  15.         this.next = null;  
  16.     }  
  17.       
  18.     public Node(T data, Node<T> next){  
  19.         this.data = data;  
  20.         this.next = next;  
  21.     }  
  22.       
  23.     public void setData(T data){  
  24.         this.data = data;  
  25.     }  
  26.       
  27.     public T getData(){  
  28.         return this.data;  
  29.     }  
  30.       
  31.     public void setNext(Node<T> next){  
  32.         this.next = next;  
  33.     }  
  34.       
  35.     public Node<T> getNext(){  
  36.         return this.next;  
  37.     }  
  38. }  

2)实现接口

[java]  view plain copy
  1. package my.list;  
  2.   
  3. public class MyLinkedList<T> implements MyList<T> {  
  4.       
  5.     private Node<T> head;  
  6.     private Node<T> tail;  
  7.     private int size;  
  8.       
  9.     public MyLinkedList(){  
  10.         this.head = null;  
  11.         this.tail = null;  
  12.         this.size = 0;  
  13.     }  
  14.       
  15.     public MyLinkedList(T data){  
  16.         this.head = new Node<T>(data);  
  17.         this.tail = null;  
  18.         this.size = 0;  
  19.     }  
  20.       
  21.     @Override  
  22.     //添加元素  
  23.     public void add(T element) {  
  24.         Node<T> node = new Node<T>(element);  
  25.         if(this.head == null){  
  26.             this.head = node;  
  27.             this.tail = node;  
  28.         }else{  
  29.             this.tail.setNext(node);  
  30.             this.tail = node;  
  31.         }  
  32.         this.size++;  
  33.     }  
  34.   
  35.     @Override  
  36.     //清空链表  
  37.     public void clear() {  
  38.         this.head = null;  
  39.         this.tail = null;  
  40.         System.gc();  
  41.     }  
  42.   
  43.     @Override  
  44.     //删除链表最后一个节点  
  45.     public int delete() {  
  46.         Node<T> point = head;  
  47.         while(point.getNext() != this.tail){  
  48.             point = point.getNext();  
  49.         }  
  50.         tail.setNext(null);  
  51.         tail = point;  
  52.         size--;  
  53.         return 0;  
  54.     }  
  55.   
  56.     @Override  
  57.     public boolean delete(int location) {  
  58.         if(location > size-1){  
  59.             System.out.println("out of range");  
  60.         }else{  
  61.             Node<T> point = head;  
  62.             int count = 1;  
  63.             while(point.getNext() != this.tail){  
  64.                 point = point.getNext();  
  65.                 count++;  
  66.                 if(count == location){  
  67.                     System.out.println("finl this element:"+point.getData());  
  68.                 }  
  69.                 break;  
  70.                   
  71. //              Node<T>  temp = point.getNext();  
  72. //              point.setData(temp.getData());  
  73. //              point.setNext(temp.getNext());  
  74. //              temp = null;  
  75.             }  
  76.             Node<T>  temp = point.getNext();  
  77.             point.setData(temp.getData());  
  78.             point.setNext(temp.getNext());  
  79.             temp = null;  
  80.             return true;  
  81.         }  
  82.         return false;  
  83.     }  
  84.   
  85.     @Override  
  86.     //查找链表中是否包含某元素  
  87.     public boolean find(T element) {  
  88.         // TODO Auto-generated method stub  
  89.         Node<T> point = head;  
  90.         while(point.getNext() != null){  
  91.             if(point.getData().equals(element)){  
  92.                 return true;  
  93.             }  
  94.             point = point.getNext();  
  95.         }  
  96.         return false;  
  97.     }  
  98.   
  99.     @Override  
  100.     public int size() {  
  101.         return this.size;  
  102.     }  
  103.       
  104. }  


第三部:编写客户端进行测试

此处暂未实现使用location位置进行查询,链表随即查询效率较低

[java]  view plain copy
  1. package my.list;  
  2.   
  3. public class MyLinkedListClient {  
  4.     public static void main(String[] args){  
  5. //      System.out.println("hello world");  
  6. //      Node<String> node = new Node<String>("hello");  
  7. //      MyLinkedList<Node<String>> list = new MyLinkedList<Node<String>>();  
  8.         MyLinkedList<String> list = new MyLinkedList<String>();  
  9.         list.add("1");  
  10.         list.add("2");  
  11.         list.add("3");  
  12.         list.add("4");  
  13.         list.add("5");  
  14. //      list.delete();  
  15.         System.out.println(list.size());  
  16. //      System.out.println(list.delete(2));  
  17. //      list.clear();  
  18. //      list.clear();  
  19.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值