声明:基本实现,细节不过多追究。。。java
数组须要在建立的时候分配好空间使用,根据索引查询便可;而链表则不需提早分配空间,须要使用的时候动态分配便可。链表中数据的访问是经过指针实现,每一个元素都包含下一个元素的一个索引。经过数组实现链表,那么思路以下:node
数组中的每一个元素是一个对象,包含两个字段:游标和data。数组
第一次初始化时的数据以下:数据结构
如今看具体方法:this
public void add(T data) { Node node = nodeArray[maxSize - 1]; int cur = node.getCur(); int index = 1; if (cur == 0) { //空数组 } else { while (node.getCur() > 0) { node = nodeArray[node.getCur()]; } index = nodeArray[0].getCur(); } //获取要设置数据的节点 Node currentNode = nodeArray[index]; //上个节点下个游标为设置数据的节点的索引 node.setCur(index); //备用链表的起始节点的游标改成设置数据节点的游标 nodeArray[0].setCur(currentNode.getCur()); //设置游标为0表示链表结束 currentNode.setCur(0); currentNode.setData(data); ++length; }
简单理解:先找到最后一个数据节点(node),而后找到该节点的游标指向的节点(currentNode),nodeArray[0]的游标指向currentNode的游标指向的节点(备用链表),node的游标指向currentNode,currentNode的游标修改成0表示结尾,设置数据。指针
public void insert(int i, T data) { Node node = nodeArray[maxSize - 1]; int j = 0; while (node.getCur() > 0 && i> j) { node = nodeArray[node.getCur()]; j++; } //获取要插入数据的位置 int cur = nodeArray[0].getCur(); Node n = nodeArray[cur]; //设置备用列表指针 nodeArray[0].setCur(n.getCur()); n.setCur(node.getCur()); n.setData(data); node.setCur(cur); ++length; }
public void delete(int i){ Node node = nodeArray[maxSize - 1]; int j = 0; while (node.getCur() > 0 && i> j) { node = nodeArray[node.getCur()]; j++; } int cur = node.getCur(); Node n = nodeArray[cur]; node.setCur(n.getCur()); n.setCur(nodeArray[0].getCur()); nodeArray[0].setCur(cur); --j; }
以上即为经过数组实现的简单链表,完整代码以下:code
package com.vincent.array; /** * Vincent 建立于 2016/7/26. */ public class LinkedList<T> { private int maxSize = Integer.MAX_VALUE; private int length = 0; private Node[] nodeArray = null; public LinkedList() { nodeArray = new Node[this.maxSize]; init(); } public LinkedList(int maxSize) { this.maxSize = maxSize; nodeArray = new Node[maxSize]; init(); } private void init() { for (int i = 0; i < maxSize; i++) { Node node = new Node(i + 1); nodeArray[i] = node; } nodeArray[maxSize - 1].setCur(0); } public void add(T data) { Node node = nodeArray[maxSize - 1]; int cur = node.getCur(); int index = 1; if (cur == 0) { //空数组 } else { while (node.getCur() > 0) { node = nodeArray[node.getCur()]; } index = nodeArray[0].getCur(); } //获取要设置数据的节点 Node currentNode = nodeArray[index]; //上个节点下个游标为设置数据的节点的索引 node.setCur(index); //备用链表的起始节点的游标改成设置数据节点的游标 nodeArray[0].setCur(currentNode.getCur()); //设置游标为0表示链表结束 currentNode.setCur(0); currentNode.setData(data); ++length; } public void insert(int i, T data) { Node node = nodeArray[maxSize - 1]; int j = 0; while (node.getCur() > 0 && i> j) { node = nodeArray[node.getCur()]; j++; } //获取要插入数据的位置 int cur = nodeArray[0].getCur(); Node n = nodeArray[cur]; //设置备用列表指针 nodeArray[0].setCur(n.getCur()); n.setCur(node.getCur()); n.setData(data); node.setCur(cur); ++length; } public void delete(int i){ Node node = nodeArray[maxSize - 1]; int j = 0; while (node.getCur() > 0 && i> j) { node = nodeArray[node.getCur()]; j++; } int cur = node.getCur(); Node n = nodeArray[cur]; node.setCur(n.getCur()); n.setCur(nodeArray[0].getCur()); nodeArray[0].setCur(cur); --j; } private class Node<T> { private T data; private int cur; public Node() { } public Node(int cur) { this.cur = cur; } public Node(T data, int cur) { this.data = data; this.cur = cur; } public T getData() { return data; } public void setData(T data) { this.data = data; } public int getCur() { return cur; } public void setCur(int cur) { this.cur = cur; } } }
经过数组实现链表加深了二者的理解,数组和链表各有优缺点,在不一样条件下选择合适的数据结构有利于提升效率。对象