数据结构之Java实现底层LinkedList

链接表是一种真正的动态数据结构,由一个个节点相互连接而构成,每一个节点包含数据和引用。相比于数组,查询速度慢,删除头元素方便,下面就是链表的实现过程

public class LinkedList<E> {
 
 private class Node {  //定义一个私有内部类代表每一个结点
  public E e;       //声明数据
  public Node next; //声明结点引用,用于指向下一个结点
  public Node(E e, Node next) {
   this.e = e;
   this.next = next;
  }
  
  public Node(E e) {
   this(e, null);
  }
  
  public Node() {
   this(null, null);
  }
  @Override
  public String toString() {
   return e.toString();
  }
 }
 
 private Node dummyHead;  //声明一个虚拟头结点,便于插入操作
 private int size;   //声明链表此寸大小
 
 public LinkedList() {       //无参构造函数生成一个数据和引用为空的结点
  dummyHead = new Node(null, null);
  size = 0;
 }
 
 public int getSize() {  //获取链表尺寸大小
  return size;
 }
 
 public boolean isEmpty() { //判断链表是否为空
  return size==0;
 }
 
 //向链表中添加元素,对于链表来说是没有索引概念,这个index是对于用户来说的相当于数组中index
 public void add(int index, E e) {
  //判断索引合法性
  if(index < 0 || index > size)
   throw new IllegalArgumentException("Add failed.Illegal index.");
  //从虚拟头结点开始遍历,一直遍历到要插入位置前一个结点
  Node prev = dummyHead;
  for(int i = 0; i < index; i++) {
   prev = prev.next;
  }
  prev.next = new Node(e, prev.next); //将插入结点与下一个结点来连接,其上一个结点来连接此结点
  size++;        //维护size,自增加1
 }
 
 public void addFirst(E e) {    //向链表头部添加结点
  add(0, e);  
 }
 
 public void addLast(E e) {    //向链表尾部添加结点
  add(size, e);
 }
 
 //根据索引获取元素
 public E get(int index) {
  //判断索引合法性
  if(index < 0 || index >= size)
   throw new IllegalArgumentException("Get failed.Illegal index.");
  //从虚拟头结点下一个结点开始遍历,直到找到对应index结点
  Node cur = dummyHead.next;
  for(int i = 0; i < index; i++) {
   cur = cur.next;
  }
  return cur.e;
 }
 
 public E getFirst() { //获取头部元素
  return get(0);
 }
 
 public E getLast() { //获取尾部元素
  return get(size - 1);
 }
 
 //修改指定位置的元素
 public void set(int index, E e) {
  //判断索引合法性
  if(index < 0 || index >= size)
   throw new IllegalArgumentException("Set failed.Illegal index.");
  //从虚拟头结点开始遍历,找到相应节点
  Node cur = dummyHead.next;
  for(int i = 0; i < index; i++) {
   cur = cur.next;
  }
  cur.e = e; //修改元素值
 }
 
 //判断是否包含相应元素
 public boolean contain(E e) {
  //从虚拟头结点开始循环整个链表,判断是否有相应的值
  Node cur = dummyHead.next;
  while(cur != null) {
   if(cur.e.equals(e))
    return true;
   cur = cur.next;
  }
  return false;
 }
 
 //根据索引删除相应结点
 public E remove(int index) {
  //判断索引合法性
  if(index < 0 || index >= size)
   throw new IllegalArgumentException("Remove failed.Illegal index.");
  //从虚拟头结点开始遍历,找到删除位置前一个节点,即前驱结点
  Node prev = dummyHead;
  for(int i = 0; i < index; i++) {
   prev = prev.next;
  }

  Node retNode = prev.next; //保存这个删除结点,便于后面返回其值
  prev.next = retNode.next; //将前驱结点指向删除结点下一个结点
  retNode.next = null;  //断开删除结点与下一个结点的连接
  size--;      //维护size,自减
  
  return retNode.e;
 }
 
 public E removeFirst() {  //头部删除
  return remove(0);
 }
 
 public E removeLast() {   //尾部删除
  return remove(size - 1);
 }

    public void removeElement(E e){ //根据指定元素删除

     //从虚拟头结点开始遍历,找到待删除元素的前驱系欸点
        Node prev = dummyHead;
        while(prev.next != null){
            if(prev.next.e.equals(e)) //如果成立表明当前结点是前驱结点
                break;
            prev = prev.next;
        }

        if(prev.next != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            size --;
        }
    }

 //重写toString方法,便于观察和打印
 @Override
 public String toString() {
  StringBuilder res = new StringBuilder();
  Node cur = dummyHead.next;
  while(cur != null) {
   res.append(cur + "->");
   cur = cur.next;
  }
  res.append("NULL");
  return res.toString();
 }
}

下面是测试程序

public class Main {

 public static void main(String[] args) {
  LinkedList<Integer> linkedList = new LinkedList<>();
  
  for(int i = 0; i < 5; i++) {
   linkedList.addFirst(i);
   System.out.println(linkedList);
  }
  linkedList.add(2, 666);
  System.out.println(linkedList);
  
  linkedList.remove(2);
  System.out.println(linkedList);
  
  linkedList.removeFirst();
  System.out.println(linkedList);
  
  linkedList.removeLast();
  System.out.println(linkedList);
 }

}

结果为

0->NULL
1->0->NULL
2->1->0->NULL
3->2->1->0->NULL
4->3->2->1->0->NULL
4->3->666->2->1->0->NULL
4->3->2->1->0->NULL
3->2->1->0->NULL
3->2->1->NULL

可以用这个链表来实现前面几篇中的栈和队列,实现代码如下

1.实现Stack

public class LinkedListStack<E> implements Stack<E> {
 private LinkedList<E> list;

 public LinkedListStack() {
  list = new LinkedList<>();
 }
 @Override
 public int getSize() {
  return list.getSize();
 }

 @Override
 public boolean isEmpty() {
  return list.isEmpty();
 }

 @Override
 public void push(E e) {
  list.addFirst(e);
 }

 @Override
 public E pop() {
  return list.removeFirst();
 }

 @Override
 public E peek() {
  return list.getFirst();
 }
 
 @Override
 public String toString() {
  StringBuilder res = new StringBuilder();
        res.append("Stack: Top ");
        res.append(list);
        return res.toString();
 }
}

1.实现Queue

public class LinkedListQueue<E> implements Queue<E> {
 private class Node {
  public E e;
  public Node next;

  public Node(E e, Node next) {
   this.e = e;
   this.next = next;
  }

  public Node(E e) {
   this(e, null);
  }

  public Node() {
   this(null, null);
  }

  @Override
  public String toString() {
   return e.toString();
  }
 }

 private Node head, tail;
 private int size;

 public LinkedListQueue() {
  head = null;
  tail = null;
  size = 0;
 }

 @Override
 public int getSize() {
  return size;
 }

 @Override
 public boolean isEmpty() {
  return size == 0;
 }

 @Override
 public void enqueue(E e) {
  if (tail == null) {
   tail = new Node(e);
   head = tail;
  } else {
   tail.next = new Node(e);
   tail = tail.next;
  }
  size++;
 }

 @Override
 public E dequeue() {
  if (isEmpty())
   throw new IllegalArgumentException("Cannot dequeue from an empty queue.");

  Node retNode = head;
  head = head.next;
  retNode.next = null;
  if (head == null)
   tail = null;
  size--;
  return retNode.e;
 }

 @Override
 public E getFront() {
  if (isEmpty())
   throw new IllegalArgumentException("Queue is empty.");
  return head.e;
 }

 @Override
 public String toString() {
  StringBuilder res = new StringBuilder();
  res.append("Queue: front ");

  Node cur = head;
  while (cur != null) {
   res.append(cur + "->");
   cur = cur.next;
  }
  res.append("NULL tail");
  return res.toString();
 }
}

以上整个过程是关于实现LinkedList基本功能,其实链表还有双链表和循环链表,还可以继续开发,一起努力。

猜你喜欢

转载自blog.csdn.net/zhangjun62/article/details/82758823