一:單連結串列
//建立連結串列
function LinkedList() {
let Node = function(element) {
this.element = element;
this.next = null;
};
let length = 0;
let head = null;
//向連結串列尾部追加元素
this.append = function(element) {
let node = new Node(element), current;
if(head == null){
head = node;
}else{
current = head;
//迴圈列表,直到找到最後一項
while(current.next){
current = current.next;
}
//找到最後一項,將其next賦為node,建立連結
current.next = node;
}
length++; //更新長度
}
//從連結串列中移除元素,兩種場景:第一種是移除第一個元素;第二種是移除第一個以外的任一元素
//從特定位置移除一個元素:
this.removeAt = function(position) {
//檢查越界值
if(position > -1 && position < length) {
let current = head, previous, index = 0;
//移除第一項
if(position == 0){
head = current.next;
}else {
while(index++ < position){
previous = current;
current = current.next;
}
//將previous與current的下一項鍊接起來:跳過current,從而移除它
previous.next = current.next;
}
length--;
return current.element;
}else{
return null;
}
}
//在任意位置插入元素
this.insert = function(position, element){
//檢查越界值
if(position >= 0 && position < length){
let node = new Node(element), current = head, previous, index = 0;
if(position == 0){
node.next = current;
head = node;
}else {
while(index++ <position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
length++;
return true;
}else {
return false;
}
}
//轉換成字串
this.toString = function() {
let current = head, string = '';
while (current) {
string += current.element;
current = current.next;
}
return string;
}
//元素的位置
this.indexOf = function(element) {
let current = head, index = -1;
while(current) {
if(element == current.element){
return index;
}
index++;
current = current.next;
}
return -1;
}
//是否為空
this.isEmpty = function() {
return length == 0;
}
//大小
this.size = function() {
return length;
}
//獲取頭部
this.getHead = function() {
return head;
}
}複製程式碼
二:雙連結串列
function DoublyLinkedList() {
let Node = function(element) {
this.element = element;
this.next = null;
this.prev = null;
};
let length = 0;
let head = null;
let tail = null;
//插入元素
this.insert = function(position, element){
if(position >= 0 && position <= length){
let node = new Node(element),
current = head,
previous,
index = 0;
if(position == 0){
if(!head){
head = node;
tail = head;
}else{
node.next = current;
current.prev = node;
head = node;
}
}else if(position == length){
current = tail;
current.next = node;
node.prev = current;
tail = node;
}else{
while(index++ < position){
previous = current;
current = current.next;
}
previous.next = node;
node.prev = previous;
node.next = current;
current.prev = node;
}
length++;
return true;
}else{
return false;
}
}
//移除元素
this.removeAt = function(position){
if(position > -1 && position < length){
let current = head,
previous,
index = 0;
if(position == 0){
head = current.next;
if(length == 1){
tail = null;
}else{
head.prev = null;
}
}else if(position == length-1){
current = tail;
tail = current.prev;
tail.next =null;
}else{
while(index++ < position){
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;
}
length--;
return current.element;
}else{
return null;
}
};
}
複製程式碼