<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function LinkedList() {
function Node(data) {
this.data = data;
this.next = null;
}
this.head = null;
this.length = 0;
LinkedList.prototype.append = function(data) {
var newNode = new Node(data);
if(this.length == 0) {
this.head = newNode;
} else {
var current = this.head;
while(current.next) {
current = current.next;
}
current.next = newNode;
}
this.length += 1;
}
LinkedList.prototype.toString = function() {
var current = this.head;
var listString = '';
while(current) {
listString += current.data + " ";
current = current.next;
}
return listString;
}
LinkedList.prototype.insert = function(position, data) {
if(position < 0 || position > this.length) {
return false;
}
var newNode = new Node(data);
if(position == 0) {
newNode.next = this.head;
this.head = newNode;
} else {
var current = this.head;
var previous = null;
var index = 0;
while(index++ < position) {
previous = current;
current = current.next;
}
newNode.next = current;
previous.next = newNode;
}
this.length += 1;
return true;
}
LinkedList.prototype.get = function(position) {
if(position < 0 || position >= this.length) {
return false;
} else {
var index = 0;
var current = this.head;
while(index++ < position) {
current = current.next;
}
return current.data;
}
}
LinkedList.prototype.indexOf = function(element) {
if(!element) {
return false;
}
var current = this.head;
var index = 0;
while(element != current.data && index < this.length) {
current = current.next;
index++;
}
if(index >= this.length) {
return false;
}
return index;
}
LinkedList.prototype.indexOf = function(data) {
var index = 0;
var current = this.head;
while(current) {
if(current.data == data) {
return index;
}
current = current.next;
index += 1;
}
return -1;
}
LinkedList.prototype.update = function(position, data) {
var index = 0;
if(position < 0 || position > this.length - 1) {
return false;
}
var current = this.head;
while(index++ < position) {
current = current.next;
}
current.data = data;
return true;
}
LinkedList.prototype.removeAt = function(position) {
if(position < 0 || position >= this.length) {
return null;
}
var current = this.head;
var previous = null;
if(position == 0) {
this.head = this.head.next;
} else {
var index = 0;
while(index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
this.length -= 1;
return current.data;
}
LinkedList.prototype.remove=function(data){
var position=this.indexOf(data);
if (position<0||position>this.length-1) {
return false ;
}
if (position==0) {
this.head=this.head.next;
} else{
var current=this.head;
var previous=null;
var index=0;
while (index++<position){
previous=current;
current=current.next;
}
previous.next=current.next;
}
this.length-=1;
return true;
}
LinkedList.prototype.isEmpty=function(){
return this.length==0;
}
LinkedList.prototype.size=function(){
return this.length;
}
}
var linkedList = new LinkedList();
linkedList.append(12);
linkedList.append(13);
linkedList.insert(2, 15);
linkedList.append(14);
linkedList.insert(0, 18);
linkedList.insert(-1, 100);
linkedList.insert(10, 17);
linkedList.insert(5, 19);
console.log(linkedList.toString());
console.log('0--', linkedList.get(0), '2--', linkedList.get(2), '5--', linkedList.get(5), linkedList.get(-1), linkedList.get(8))
console.log(linkedList);
console.log(linkedList.indexOf(12), linkedList.indexOf(15), linkedList.indexOf(19), linkedList.indexOf(17))
console.log(linkedList.indexOf(12), linkedList.indexOf(19), linkedList.indexOf(20));
linkedList.update(0, 12);
linkedList.update(5, 18);
linkedList.update(19, 1);
console.log(linkedList.toString());
console.log(linkedList.removeAt(0));
console.log(linkedList.removeAt(2));
console.log(linkedList.remove(14))
console.log(linkedList.toString(),linkedList.isEmpty(),linkedList.size());
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function DoublyLinkedList() {
this.length = 0;
this.head = null;
this.tail = null;
function Node(data) {
this.prev = null;
this.next = null;
this.data = data;
}
DoublyLinkedList.prototype.append = function(data) {
var newNode = new Node(data);
if(this.length == 0) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
this.length += 1;
}
DoublyLinkedList.prototype.backwardString = function() {
var result = '';
var current = this.head;
while(current) {
result += current.data + " ";
current = current.next;
}
return result;
}
DoublyLinkedList.prototype.forwardString = function() {
var result = "";
var current = this.tail;
while(current) {
result += current.data + " ";
current = current.prev;
}
return result;
}
DoublyLinkedList.prototype.toString = function() {
return this.backwardString();
}
DoublyLinkedList.prototype.insert = function(position, data) {
if(position < 0 || position > this.length) {
return false;
}
var newNode = new Node(data);
if(this.length == 0) {
this.head = newNode;
this.prev = newNode;
} else {
if(position == 0) {
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
} else if(position == this.length) {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
} else {
var index = 0;
var current = this.head;
while(index++ < position) {
current = current.next;
}
newNode.next = current;
newNode.prev = current.prev;
current.prev.next = newNode;
current.prev = newNode;
}
}
this.length += 1;
return true;
}
DoublyLinkedList.prototype.get = function(position) {
if(position < 0 || position >= this.length) {
return false;
}
var middle = this.length / 2;
if(middle > position) {
var index = 0;
var current = this.head;
while(index++ < position) {
current = current.next;
}
return current.data;
} else {
var index = this.length - 1;
var current = this.tail;
while(index-- > position) {
current = current.prev;
}
return current.data;
}
}
DoublyLinkedList.prototype.indexOf = function(data) {
if(!data) {
return -1;
}
var current = this.head;
var index = 0
while(current) {
if(current.data == data) {
return index;
}
current = current.next;
index++;
}
return -1;
}
DoublyLinkedList.prototype.update = function(position, data) {
if(position < 0 || position >= this.length) {
return false;
}
var current = this.head;
var index = 0;
while(index++ < position) {
current = current.next;
}
current.data = data;
return true;
}
DoublyLinkedList.prototype.removeAt = function(position) {
if(position < 0 || position >= this.length) {
return null;
}
var current = this.head;
if(this.length == 1) {
this.head = null;
this.tail = null;
} else {
if(position == 0) {
this.head.next.prev = null;
this.head = this.head.next;
} else if(position == this.length - 1) {
current = this.tail;
this.tail.prev.next = null;
this.tail = this.tail.prev;
} else {
var index = 0;
while(index++ < position) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
this.length -= 1;
return current.data;
}
DoublyLinkedList.prototype.remove=function(data){
var index=this.indexOf(data);
return this.removeAt(index);
}
DoublyLinkedList.prototype.isEmpty=function(){
return this.length==0;
}
DoublyLinkedList.prototype.size=function(){
return this.length;
}
DoublyLinkedList.prototype.getHead=function(){
return this.head.data;
}
DoublyLinkedList.prototype.getTail=function(){
return this.tail.data;
}
}
var list = new DoublyLinkedList();
list.append(12);
list.append(9);
list.append(18);
list.insert(0, 10);
list.insert(3, 8);
list.insert(5, 5);
list.insert(10, 82);
console.log(list)
console.log(list.toString());
console.log(list.backwardString());
console.log(list.forwardString());
console.log(list.get(0), list.get(5), list.get(7), list.get(3), list.get(2));
console.log(list.indexOf(-1), list.indexOf(9), list.indexOf(5), list.indexOf(8));
list.update(1, 2);
list.update(5, 7);
list.update(-1, 45);
list.update(7, 8);
console.log(list.toString());
console.log(list.removeAt(-1), list.removeAt(6), list.removeAt(0),list.removeAt(4) ,list.removeAt(1),list.toString());
console.log(list.toString());
console.log(list.isEmpty(),list.getHead(),list.getTail(),list.size());
</script>
</body>
</html>