目录
比较LinkedListStack和ArrayStack的性能:
相关文章:
毕向东视频有关于链表(17集合框架)的内容
什么是链表?
链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。
链表这种数据结构的特点:
优缺点:
- 优点:真正的动态,不需要处理固定容量的问题。
- 缺点:丧失了随机访问的能力。
怎么创建一个链表?
以下为自己手写的简单的LinkedList:
public class LearnLinkedList<E> {
/**
* 私有内部类:对于用户来说,不需要了解链表的底层内部实现是怎样的,也不需要知道链表内部使用了
* node这样一种结构。用户只需要知道链表是一种线性的数据结构,它可以存储、添加、删除、修改、查询数据。
* 我们需要对用户屏蔽底层的实现细节
*/
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);
}
/**
* 打印节点Node的信息
*/
@Override
public String toString() {
return e.toString();
}
}
//虚拟头结点,为了编程方便,在处理第一个元素的时候更加方便
private Node dummyHead;
private int size;
public LearnLinkedList() {
dummyHead = new Node(null, null);
size = 0;
}
//获取链表的元素个数
public int getSize() {
return size;
}
//返回链表是否为空
public boolean isEmpty() {
return size == 0;
}
//在链表的index(0-based)位置添加新的元素e
//这种使用index的方式不是一个常用的操作,只作为练习使用,
// 因为在日常的链表使用中,我们是不使用index的
public void add(int index, E e) {
//首先判断index的合法性
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++;
}
//在链表头部添加新的元素
public void addFirst(E e) {
add(0, e);
}
//在链表末尾添加新的元素e
public void addLast(E e) {
add(size, e);
}
//在链表的index(0-based)位置添加新的元素e
//这种使用index的方式不是一个常用的操作,只作为练习使用,
public E get(int index) {
//首先判断index的合法性
if (index < 0 || index > size)
throw new IllegalArgumentException("Get failed, Illegal 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);
}
//修改链表的第index(0-based)个位置的元素e
//这种使用index的方式不是一个常用的操作,只作为练习使用,
public void set(int index, E e) {
//首先判断index的合法性
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;
}
//删除链表的第index(0-based)个位置的元素e
//这种使用index的方式不是一个常用的操作,只作为练习使用,
public E remove(int index) {
//首先判断index的合法性
if (index < 0 || index > size)
throw new IllegalArgumentException("Set failed, Illegal index");
Node prev = dummyHead;
for (int i = 0; i < index; i++) {
prev = prev.next;
}
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size--;
return delNode.e;
}
//从链表中删除第一个元素,返回删除的元素
public E removeFirst(){
return remove(0);
}
//从链表中删除最后一个元素,返回删除的元素
public E removeLast(){
return remove(size-1);
}
//查找链表中是否有元素e
public boolean contains(E e) {
Node cur = dummyHead.next;
while (cur != null) {
if (cur.e.equals(e))
return true;
cur = cur.next;
}
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
// Node cur = dummyHead.next;
// while (cur != null) {
// sb.append(cur + " --> ");
// cur = cur.next;
// }
//一下for循环等价于while循环
for (Node cur = dummyHead.next; cur != null; cur = cur.next)
sb.append(cur + " --> ");
sb.append("NULL");
return sb.toString();
}
}
其中,在某个位置添加元素需要注意:
顺序要正确:
node.next = prev.next;
prev.next = node;
如果写反了,就得不到我们想要的结果了。
链表的时间复杂度:
- 增:O(n)
- 删:O(n)
- 改:O(n)
- 查:O(n)
使用链表实现栈:
public class LinkedListStack<E> implements LearnStack<E> {
private LearnLinkedList<E> list;
public LinkedListStack() {
list = new LearnLinkedList<>();
}
@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();
}
public static void main(String[] args) {
LinkedListStack<Integer> stack = new LinkedListStack<>();
for (int i = 0; i < 5; i++) {
stack.push(i);
System.out.println(stack);
}
stack.pop();
System.out.println(stack);
}
}
比较LinkedListStack和ArrayStack的性能:
public class Test {
//测试使用stack运行opCount个push和pop操作所需要的时间,单位:秒
private static double testStack(LearnStack<Integer> stack, int opCount) {
long startTime = System.nanoTime();
Random random = new Random();
for (int i = 0; i < opCount; i++) {
stack.push(random.nextInt(Integer.MAX_VALUE));
}
for (int i = 0; i < opCount; i++) {
stack.pop();
}
long endTime = System.nanoTime();
return (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int opCount = 1000000;
ArrayStack<Integer> arrayStack = new ArrayStack<>();
double time1 = testStack(arrayStack, opCount);
System.out.println("ArrayStack, time: " + time1 + "s");
LinkedListStack<Integer> linkedListStack = new LinkedListStack<>();
double time2 = testStack(linkedListStack, opCount);
System.out.println("LinkedListStack, time: " + time2 + "s");
//其实这个时间比较很复杂,因为LinkedListStack中包含更多的new操作
}
}
对比结果:
ArrayStack, time: 0.048407867s
LinkedListStack, time: 0.281817246s
用链表实现队列,图示:
对于用链表实现Queue存在这样的问题,我们不能直接复用之前写的LinkedList,因此需要设置一个tail的Node节点,用于进行队尾的入队操作(最好不要进行出队操作,因为比较耗时)。另一端设置一个head节点,用于处理队首的出队操作。
代码实现:
public class LinkedListQueue<E> implements LearnQueue<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);
}
/**
* 打印节点Node的信息
*/
@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();
}
public static void main(String[] args) {
LinkedListQueue<Integer> queue = new LinkedListQueue<>();
for (int i = 0; i < 10; i++) {
queue.enqueue(i);
System.out.println(queue);
if (i % 3 == 2){
queue.dequeue();
System.out.println(queue);
}
}
}
}
性能对比:
ArrayQueue, time: 3.450372407s
LoopQueue, time: 0.01176366s
LinkedListQueue, time: 0.013566522s
ArrayList和LinkedList的区别?
接口List<E> 常用的实现类 Vector、ArrayList、LinkedList
- Vector:内部是数组数据结构,是同步的。数组大小可变,可增长,增长幅度为100%。增删、查询都很慢。
- ArrayList:内部是数组数据结构,是不同步的(效率高)。替代了Vector。数组大小可变,可增长,增长幅度为50%。查询的速度快。
- LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。
那么有没有身么结构能够结合两者的优势呢?
有,就是Hash表