js实现单向链表与双向链表

<!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;
						//找到原来链表的最后一个节点,其next指向新加的节点。
						while(current.next) { //当前节点不是最后一个节点
							//当前节点指向下一个节点,直到最后一个节点
							current = current.next;
						}
						current.next = newNode;
					}
					this.length += 1;
				}

				LinkedList.prototype.toString = function() {
					var current = this.head; //指向第一个元素
					//					console.log(current,111)
					var listString = '';
					while(current) {
						listString += current.data + "  ";
						current = current.next;
					}
					//					console.log(listString);
					return listString;
				}
				LinkedList.prototype.insert = function(position, data) {
					//越界判断
					if(position < 0 || position > this.length) {
						return false;
					}
					var newNode = new Node(data);
					//分情况插入-1.头部插入
					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;
					}
					//长度加1
					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;
					}
					//到最后没有找到该元素,返回-1
					return -1;
				}
				//类似get
				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;
						}
						//前一个节点的next 指向  当前节点的next
						previous.next = current.next;
						
					}
					this.length -= 1;
					return current.data;
					//前一个节点的next 指向  当前节点的next
					//					previous.next = current.next;
					//前一个节点 就是 当前节点的下一个节点
					//					previous=current.next;
				}
				LinkedList.prototype.remove=function(data){
					var position=this.indexOf(data);
//					return this.removeAt(position);
					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))
			//			alert(linkedList)
			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 {
						//						新节点的prev指向原来最后的节点;newNode.prev=this.tail;
						newNode.prev = this.tail;
						//						原来最后节点的next指向新节点 ;this.tail.next=newNode;
						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;
							}
							//此时,current指向要插入的位置
							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) { //position在链表前半部分
						var index = 0;
						var current = this.head;
						while(index++ < position) {
							current = current.next;
						}
						return current.data;
					} else { //position在链表后半部分
						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 = null;
							//							this.tail = null;
							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);
			//			alert(list);
			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());
			//list.removeAt(3),
			console.log(list.toString());
//			console.log(list.remove(8),list.remove(89),list.remove(2),list.remove(18),list.toString());
			console.log(list.isEmpty(),list.getHead(),list.getTail(),list.size());
		</script>
	</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值