機能の配列:
- 典型的には連続したメモリ空間に適用する必要のアレイ(メモリのブロック)を作成し、サイズは(ほとんどのプログラミング言語は、配列が固定されている)が固定され、
- 現在のアレイ容量の需要を満たすことができない場合は、追加の容量が必要とされています。(通常の状況下では、アレイ内の例えば2倍の要素が次にコピーされ、より大きなアレイを適用することです)。
機能のリスト:
- 要素のリストは、メモリ空間内で連続している必要はない、あなたは、柔軟な動的なメモリ管理のためのコンピュータのメモリをフルに活用することができます。
- リストの各要素は、ノード自身の記憶素子によって次の要素へのポインタを参照します。メモリ空間は連続比ではありません。
- 作成したときには、リストのサイズを決定する必要がなく、大きさは無限にダウン拡張することができます。
- 挿入したデータを削除する場合、リスト、時間複雑度は、O(1)を達成することができる。非常に高い相対アレイ効率です。
- リンクリストの要素は任意の場所にアクセスしたとき、我々はゼロからのアクセスを開始する必要があります。(最初の要素へのアクセスに任意の要素をスキップすることはできません)。
//封装链表
function LinkedList(){
this.headNode = null ;//设置头部结点
this.length = 0;
function NewNode(data){
this.data = data;
this.next = null;
}
/*
向链表尾部追加数据可能有两种情况:
1.链表本身为空, 新添加的数据时唯一的节点.
2.链表不为空, 需要向其他节点后面追加节点.
*/
LinkedList.prototype.appendNode = function (data) {
let newNode = new NewNode(data);
if(this.length == 0){
this.headNode = newNode; //如果链表为空,将插入的节点设置为头部节点
}else{
//链表不为空 从头部节点开始遍历一直找到链表的最后一个节点 然后将要插入的节点设置为最后一个节点的next
let currentNode = this.headNode;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = newNode;
}
this.length +=1; //链表长度加1
}
/*
根据传递的位置获取对应位置的元素
*/
LinkedList.prototype.getData = function(position){
//position判断,防止越界
if(position <0 || position > this.length - 1) return false;
let index =0 ;//从头节点开始遍历
let currentNode = this.headNode;
while (index < position){
currentNode = currentNode.next;
index++;
}
return currentNode.data;
}
/*
* 根据传递的位置和新数据更新
*/
LinkedList.prototype.updateData = function(position,newData){
//位置越界判断
if(position < 0 || position > this.length-1) return false;
let index = 0;
let currentNode = this.headNode;
while(index < position){
index ++;
currentNode = currentNode.next;
}
currentNode.data = newData;
}
/*
* 根据传递的数据获取对应的位置
*/
LinkedList.prototype.getIndex = function(data){
let index = 0;
let currentNode = this.headNode;
while (currentNode.data !== data){
currentNode = currentNode.next;
index ++;
}
if(index > this.length-1) return -1;
return index;
}
/*
向任意位置插入元素的两种情况:
1.向头节点插入
2.向其他位置插入
*/
LinkedList.prototype.insertNode = function (data,position) {
//需要传递两个参数,一个是要插入的节点的数据,一个是要插入的位置
//position越界判断
if(position < 0 || position > this.length) return false;
//positon == this.length 表示要向链表的尾部插入数据
let currentNode = this.headNode;
let newNode = new NewNode(data);//创建要插入的节点
let index = 0;
let previousNode = null;
//1.判断是否要向头节点插入
if(position == 0){
newNode.next = this.headNode;
this.headNode = newNode;
}else{
while(index < position){
previousNode = currentNode;
currentNode = currentNode.next;
index++;
}
previousNode.next = newNode;
newNode.next = currentNode;
}
}
/*
根据传递的索引删除对应位置的节点 两种情况
1.删除头节点
2.删除其他位置的节点
*/
LinkedList.prototype.removeAt = function(position){
//位置越界判断
if(position<0 || position > this.length -1) return -1;
let index = 0;
let currentNode = this.headNode;
let previousNode = null;
if(position == 0){
this.headNode = currentNode.next
}else{
while(index < position){
previousNode = currentNode;
currentNode = currentNode.next;
index++;
}
previousNode.next = currentNode.next;
}
this.length -=1;
}
/*
根据传递的数据删除对应位置的节点
*/
LinkedList.prototype.removeNode = function (data) {
let position = this.getIndex(data);
this.removeAt(position);
}
}