候戴玥
2021-07-14
每一个节点有一个成员变量引用下一个节点就行了。 大致实现了一下单向链表 没有加入异常也没有仔细考虑实现的代码的效率,可以参考下。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 public class LinkListTest {     public static void main(String[] args) {         LinkList<String> ll=new LinkList<String>();         ll.add("a");         ll.add("b");         ll.add("c");         ll.add("d");         ll.remove(1);         System.out.println(ll.get(0));         System.out.println(ll.get(1));         System.out.println(ll.get(2));         System.out.println(ll.get(3));         System.out.println(ll.size());     } } class LinkList<T>{     private Node<T> frist=null;     private Node<T> last=null;     private int size=0;     public void add(T t){         if(frist==null){             Node<T> node=new Node<T>();             node.setT(t);             size++;             frist=node;             last=node;         }else{                     Node<T> node=new Node<T>();                    node.setT(t);             last.setNextNode(node);             size++;             last=node;         }     }     public T get(int i){               if(i>=0&&i<size){             Node<T> nod=null;             for(int n=0;n<=i;n++){                 if(n==0)                    nod=frist;                 else                    nod=nod.getNextNode();                              if(i==n){                     return nod.getT();                 }                      }                  }          return null;     }      public void remove(int i){         if(i>=0&&i<size){            if(size<2){                frist=null;                last=null;                size=0;            }else{                size--;               if(i==0){                  frist=frist.getNextNode();                       }else{                           Node<T> nod1=null;                     Node<T> nod2=null;                     for(int n=0;n<=i;n++){                         if(n==0){                            nod1=frist;                            nod2=frist;                         }else{                            nod2=nod1;                            nod1=nod1.getNextNode();                                               }                          if(i==n){                             if(nod1!=null)                                 nod2.setNextNode(nod1.getNextNode());                             else{                                 nod2.setNextNode(null);                                 last=nod2;                             }                         }                                  }                }                }         }     }     public int size(){         return size;     } } class Node <T>{     public T getT() {         return t;     }     public void setT(T t) {         this.t = t;     }     public Node<T> getNextNode() {         return nextNode;     }     public void setNextNode(Node<T> nextNode) {         this.nextNode = nextNode;     }     private T t;     private Node<T> nextNode=null; }
0条评论
取 消 确 定
相关问题互动
沈姓的祖先是谁?
锺文懿
赞0
微信
QQ空间
QQ
微博