链表是一种物理存储单元上非连续、非顺序的存储结构。
链表是由那几个部分组成的呢?
是由N个节点组成的
每一个节点分为两部分:
1.数据域
2.指针域
数据域用来存储数据,指针域用来链接各个链表。
public class Node<E> { private E e;// 数据域 private Node<E> next;// 引用域 public Node() { } public Node(E e) { this.e = e; } public E getE() { return e; } public void setE(E e) { this.e = e; } public Node<E> getNext() { return next; } public void setNext(Node<E> next) { this.next = next; }
public class MyLinkedList<E> { //声明头节点 private Node<E> root; private int size;//声明单链表中存储的节点数 public MyLinkedList(){ root = new Node<E>();//实例化头节点 } /** * 向链表中添加元素的方法 * @param e要添加的元素 */ public void add(E e){ //根据e实例化了一个新的节点对象 Node<E> node = new Node<E>(e); //获取root的下一个节点 Node<E> tnode = root.getNext(); root.setNext(node);//将新的节点作为root的下一个节点 node.setNext(tnode);//将root原来的下一个节点作为新增加节点的下一个节点 size++;//记录添加的节点数 } /** * 删除指定索引位置的元素 * @param index索引位置 * @return 返回删除的元素 */ public E remove(int index){ if(index <= 0 || index > size) return null; //获取要删除节点的前一个节点 Node<E> node = select(index-1); //获取要删除的节点 Node<E> dNode = node.getNext(); //获取要删除节点的后一个节点 Node<E> nNode = dNode.getNext(); //先建立删除节点的前一个节点和删除节点的后一个节点的关系 node.setNext(nNode); //清除dNode的下一个节点 dNode.setNext(null); size--;//计数器减一 return dNode.getE();//返回删除节点中的数据域 } /** * 获取指定索引位置的元素 * @param index索引位置 * @return 返回节点中的数据域 */ public E get(int index){ if(index <= 0 || index > size) return null; //查找指定索引位置的节点对象 Node<E> node = select(index); //获取节点中的数据域元素并返回 return node.getE(); } /** * 获取单链表中存储的元素总数 * @return 返回size属性 */ public int size(){ return size; } /** * 获取指定索引位置的节点对象 * @param index索引位置 * @return 返回获取到的节点对象 */ private Node<E> select(int index){ Node<E> node = root.getNext();//将头节点的下一个节点赋给node if(index==1)//如果index是1表示是头结点的下一个节点 return node;//直接返回node for(int i=1;i<index;i++){ node = node.getNext();//获取node的下一个节点 } return node; } }
上面所述的是我们的单向链表,但是同时也具有双向的链表。
public class Node<E> { private E e;// 数据域 private Node<E> next;// 引用域 private Node<E> last;//引用域 public Node() { } public Node(E e) { this.e = e; } public E getE() { return e; } public void setE(E e) { this.e = e; } public Node<E> getNext() { return next; } public void setNext(Node<E> next) { this.next = next; } public Node<E> getLast(){ return last; } public void setLast(Node<E> last){ this.last = last; } }
这上面就是双向链表的链表结构,我们也就是仅仅的添加了两个功能和一个变量而已,这个变量就是节点的上一个节点。而新的功能就是查找节点的上一节点的能力。