数据结构之JavaScript实现List

版权声明: https://blog.csdn.net/wlk2064819994/article/details/79951473
利用数组实现列表。
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>JavaScript实现列表</title>
</head>
<body>
	
	<script>
		/*定义List类及其拥有的属性和方法*/
		function List(){
			this.listSize = 0;/*列表长度*/
			this.pos = 0;/*列表位置*/
			this.dataStore = [];/*初始化一个空数组来保存列表*/
			this.clear = clear;
			this.find = find;
			this.toString = toString;
			this.insert = insert;
			this.append = append;
			this.remove = remove;
			this.front = front;
			this.end = end;
			this.prev = prev;
			this.next = next;
			this.hasNext;
			this.hasPrev;
			this.length = length;
			this.currPos = currPos;
			this.moveTo = moveTo;
			this.getElement = getElement;
			this.contains = contains;
		}


		/*JavaScript实现List的方法*/

		/*append:给列表添加元素*/
		function append(element){
			this.dataStore[this.listSize++] = element;
		}

		/*find:从列表中找到某个元素*/
		function find(element){
			for (var i = 0; i < this.dataStore.length; i++) {
				if(this.dataStore[i]==element){
					return i;
				}
			}
			return -1;
		}

		/*remove:从列表中删除元素*/
		function remove(element){
			var findAt =this.find(element);
			if(findAt>-1){
				this.dataStore.splice(findAt,1);
				this.listSize--;
				return true;
			}
			return false;
		}

		/*length:返回列表长度*/
		function length(){
			return this.listSize;
		}

		/*toString:显示列表中的元素*/
		function toString(){
			return this.dataStore;
		}

		/*insert:插入元素*/
		function insert(element,after){
			if(typeof after == 'number'){
				this.dataStore.splice(after,0,element);
				this.listSize++;
				return true;
			}else{
				var insertAt = this.find(after);
				if(insertAt>-1){
					this.dataStore.splice(insertAt+1,0,element);
					this.listSize++;
					return true;
				}
				return false;
			}
		}

		/*clear:清空列表*/
		function clear(){
			this.dataStore = [];
			this.listSize = 0;
			this.pos = 0;
		}

		/*contains:判断给定值是否在列表中*/
		function contains(element){
			for(var i=0;i<this.dataStore.length;i++){
				if(this.dataStore[i]==element){
					return true;
				}
				return false;
			}
		}

		/*getElement:返回列表当前元素*/
		function getElement(){
			return this.dataStore[this.pos];
		}

		/*front:到列表头部*/
		function front(){
			this.pos = 0;
			return this;
		}

		/*end:回到尾部*/
		function end(){
			this.pos = this.listSize-1;
			return this;
		}

		/*prev:前一个*/
		function prev(){
			this.pos--;
			return this;
		}

		/*next:后一个*/
		function next(){
			if(this.pos<this.listSize-1){
				this.pos++;
				return this;
			}
		}

		/*currPos:返回当前下标*/
		function currPos(){
			return this.dataStore[pos];
		}

		/*moveTo:移动到指定下标*/
		function moveTo(postion){
			this.pos = postion;
		}

		/*hasNext:判断是否还有下一个*/
		function hasNext(){
			return this.pos<this.listSize-1;
		}

		/*hasPrev:判断是否还有前一个*/
		function hasPrev(){
			return this.pos>0;
		}

		/*测试*/
		var names = new List();
		names.append("wlk");
		names.append("wlh");
		names.append("wkz");
		console.log(names.toString());
		//其它方法这里不予测试,作者在本地调试全部通过。

	</script>
</body>
</html>

阅读更多
换一批

没有更多推荐了,返回首页