《数据结构与算法JavaScript描述》----javascript 列表的实现

”为了设计列表的抽象数据类型,需要给出列表的定义,包括列表应该拥有哪些属性,应该 在列表上执行哪些操作。
列表是一组有序的数据。每个列表中的数据项称为元素。在 JavaScript 中,列表中的元素 可以是任意数据类型。列表中可以保存多少元素并没有事先限定,实际使用时元素的数量 受到程序内存的限制。

不包含任何元素的列表称为空列表。

列表中包含元素的个数称为列表的 length

在内部实 现上,用一个变量 listSize 保存列表中元素的个数

。可以在列表末尾 append 一个元素

, 也可以在一个给定元素后或列表的起始位置 insert 一个元素。

使用 remove 方法从列表中 删除元素,

使用 clear 方法清空列表中所有的元素。

还可以使用 toString() 方法显示列表中所有的元素,

使用 getElement() 方法显示当前元素“


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.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.contains = contains;
}

function append(element) {
    this.dataStore[this.listSize++] = element;
}
/*
	如皋找到返回位置,否则返回-1
*/
function find(element) {
    for (var i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i] == element) {
            return i;
        }
    }
    return -1;
};
/*
		依赖find
*/
function remove(element) {
    var fondAt = this.find(element);
    if (fondAt > -1) {
        this.dataStore.splice(fondAt, 1);
        --THIS.listSize;
        return true;
    }
    return false;
}

function length() {
    return this.listSize;
}
//返回一个数组
function toString() {
    return this.dataStore;
}
///在指定位置插入元素
function insert(element, after) {
    var insertPos = this.find(element);
    console.log("find",insertPos)
    if (insertPos > -1) {
        this.dataStore.splice(insertPos + 1, 0, after);
        ++this.listSize;
        return true;
    }
    return false;
}

function clear() {
    delete this.dataStore; //没必要上次的,多此一举,下面一行把引用地址替换就可以了
    this.dataStore = [];
    this.listSize = this.pos = 0;
}

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

一下方法运行用户在列表上只有移动
//当前位置指向最前面
function front() {
    this.pos = 0;
}
//当前位置指向最后
function end() {
    this.pos = this.listSize - 1;
}

function prev() {
    if (this.pos > 0) {
        --this.pos;
    }
}

function next() {
    if (this.pos < this.listSize - 1) {
        ++this.pos;
    }
};

function currPos() {
    return this.pos;
}

function moveTo(position) {
    this.pos = position;
};
//返回当前元素
function getElement() {
    return this.dataStore[this.pos];
}

使用:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script src="List.js"></script>
	<script>
			var names=new List();
			names.append("01");
			names.append("01");
			names.append("02");
			names.append("03");
			console.log("show",names.toString())
			names.insert("02","04");
			console.log("show afte insert",names.toString())
			console.log("默认当前是第一个",names.getElement());
			names.front();
			console.log("afer front之后也是第一个",names.getElement());
			names.end();
			console.log("afer end之后也是最后一个",names.getElement());
			console.log("使用迭代器------------->")
			for(names.end();names.currPos()>0;names.prev()){
			  	console.log("使用迭代器:",names.getElement())
			};
			

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆康永

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值