Сегодня я хочу показать, как реализовать связанный список, который является полезной структурой данных для решения многих проблем алгоритмов.
Что такое связанный список?
Связный список — это структура данных, в которой элементы хранятся в объекте. Этот объект имеет атрибуты: данные и ссылку на следующий объект, если он добавляется после этого объекта, т.е. каждый объект связан с помощью указателей, как показано на изображении ниже:
Эти объекты известны как узлы связанного списка.
Наконец, это моя собственная реализация связанного списка в javascript, которая содержит реализацию нодлиста и следующие методы:
добавить: добавить элемент в связанный список.
addAt: добавить элемент pos в связанный список.
length: длина связанного списка.
valueAt: возвращаемое значение в узле pos.
удалить: удалить определенную позицию из связанного списка.
reverse: инвертировать связанный список.
middle: возвращает средний элемент в связанном списке.
function NodeList(value, ref) { this.value = value; this.ref = ref; } function LinkedList() { this._head = null; this._length = 0; LinkedList.prototype.add = (value) => { let node = new NodeList(value); if (!this._head) { this._head = node; } else { var currentlynode =this._head; while (currentlynode.ref) currentlynode = currentlynode.ref; currentlynode.ref = node; } this._length++; } LinkedList.prototype.length=()=>{ return this._length; } LinkedList.prototype.addAt = (pos, value)=>{ if (pos >= this._length) { console.log("You can't add this element at pos"); } else if(pos==0){ let node = new NodeList(value,this._head); this._head=node; this._length++; } else { let currentlynode = this._head; for (let i = 0; i < pos - 1; i++) { currentlynode = currentlynode.ref; } let node = new NodeList(value, currentlynode.ref); currentlynode.ref = node; this._length++; } } LinkedList.prototype.valueAt= (pos)=>{ if(pos>=this._length) return "There is no value at this pos"; else{ let currentlynode=this._head; if(pos==0) return currentlynode.value; else{ for(let i=1;i<=pos;i++){ currentlynode=currentlynode.ref; } return currentlynode.value; } } } LinkedList.prototype.remove = (pos) => { if (this._length > 0) { if (pos< this._length) { if (pos === 0) { let cab = this._head; this._head = cab.ref; this._length--; } else { let currentlynode = this._head; for (let i = 1; i < pos; i++) { currentlynode = currentlynode.ref; } let deletenode = currentlynode.ref; currentlynode.ref = deletenode.ref; this._length--; } } else { console.log("There is no value at pos"); } } else{ console.log("LinkedList is empty"); } } LinkedList.prototype.reverse=()=>{ let currentlynode=this._head; let beforenode=null; while(currentlynode){ let temp= currentlynode.ref; currentlynode.ref=beforenode; beforenode=currentlynode; currentlynode = temp; } return beforenode; } LinkedList.prototype.middle=()=>{ let pos=0; if(this._length % 2===0) pos=(this._length/2)-1; else{ pos=this._length/2; } let currentlynode=this._head; if(pos==0) return currentlynode.value; else{ for(let i=1;i<=pos;i++){ currentlynode=currentlynode.ref; } return currentlynode.value; } } }
Спасибо за чтение.