當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript数据结构与算法——列表详解(上)
生活随笔
收集整理的這篇文章主要介紹了
JavaScript数据结构与算法——列表详解(上)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
列表是一組有序的數據,每個數組中的數據項稱為元素。數組相關知識不夠了解的伙伴可以閱讀本人上篇博客在JavaScript中,列表的元素可以是任意數據類型。列表中可以保存不定數量的元素,實際使用時元素的數量受到程序內存的限制。
本文將介紹如何創建一個簡單的列表類,將根據給出的列表抽象數據類型定義,實現該抽象數據類型,在列表詳解(下)中,將分析幾個適合解決的實際問題。
1、列表的抽象數據類型定義
| listSize(屬性)? ? ? ? ? ? ? ? ? ? | ?列表的元素個數???????????????????????????????????????????????????? |
| pos(屬性) | ?列表當前位置 |
| length(屬性) | ?返回列表中元素的個數 |
| clear(方法) | ?清空列表中所有的元素 |
| toString(方法) | ?返回列表的字符串形式 |
| getElement(方法) | ?返回當前位置的元素 |
| insert(方法) | ?在現有元素后插入新元素 |
| append(方法) | ?在列表的末尾添加新元素 |
| remove(方法) | ?從列表中刪除元素 |
| front(方法) | ?將列表的當前位置移動到第一個元素 |
| end(方法) | ?將列表的當前位置移動到最后一個元素 |
| prev(方法) | ?將當前位置后移一位 |
| next(方法) | ?將當前位置前移一位 |
| hasNext(方法) | ?判斷是否存在后一位 |
| hasPrev(方法) | ?判斷是否存在前一位 |
| currPos(方法) | ?返回列表的當前位置 |
| moveTo(方法) | ?將當前位置移動到指定的位置 |
2、實現列表類
根據上邊定義的列表抽象數據類型,可以直接實現一個List類。首先我們實現一個構造函數,并聲明屬性、方法。
function List() {this.listSize = 0this.pos = 0this.dataStore = [] // 初始化一個用于保存列表元素的空數組this.clear = clearthis.find = findthis.toString = toStringthis.insert = insertthis.append = appendthis.remove = removethis.front = frontthis.end = endthis.prev = prevthis.next = nextthis.hasNext = hasNextthis.hasPrev = hasPrevthis.length = lengththis.currPos = currPosthis.moveTo = moveTothis.getElement = getElementthis.contains = contains}2.1 實現append(),給列表添加元素
function append(element) {console.log(this)this.dataStore[this.listSize++] = element}當調用此方法添加元素時,變量listSize加1。
2.2、實現find(),查找列表中的元素
function find(element) {for (var i = 0; i < this.dataStore.length; i++) {if (this.dataStore[i] == element) {return i}}return -1}如果找到指定元素則返回該元素的索引,如果沒找到,則返回-1
2.3、實現remove(),從列表中刪除元素
實現原理:借助2.1中實現的find(),查找如需要刪除的元素的索引,找到后借助數組方法splice(),刪除指定的元素。
function remove(element) {var foundAt = this.find(element)if (foundAt > -1) {this.dataStore.splice(foundAt, 1)this.listSize--return true}return false}2.4、實現length(),統計列表中的元素
function length() {return this.listSize}2.5、實現toString(),顯示列表中的元素
function toString() {return this.dataStore.toString()}2.6、實現insert(),向列表插入一個元素
function insert(element, after) {var insertPos = this.find(after)if (insertPos > -1) {this.dataStore.splice(insertPos + 1, 0, element)this.liseSize++return true}return false}2.7、實現clear(),實現清空列表元素
function clear() {delete this.dataStorethis.dataStore.length = 0this.listSize = 0this.pos = 0}2.8、實現contains(),判斷給定值是否包含于列表中
function contains(element) {for (var i = 0; i < this.dataStore.length; i++) {return true}return false}2.9、實現遍歷列表的各個方法
function front() {this.pos = 0}function end() {this.pos = this.listSize - 1}function prev() {this.pos--}function next() {if (this.pos < this.listSize) {this.pos++}}function currPos() {return this.pos}function moveTo(position) {this.pos = position}function hasNext() {return this.pos < this.listSize}function hasPrev() {return this.pos >= 0}function getElement() {return this.dataStore[this.pos]}測試:
在控制臺新建一個List對象,往列表添加元素
開始測試:
3、使用迭代器訪問列表
(1)從前到后遍歷列表
var tList = new List()tList.append('哈哈1')tList.append('哈哈2')tList.append('哈哈3')tList.append('哈哈4')tList.append('哈哈5')for (tList.front(); tList.hasNext(); tList.next()) {console.log(tList.getElement())}結果:
(2)從后到前遍歷列表
var tList = new List()tList.append('哈哈1')tList.append('哈哈2')tList.append('哈哈3')tList.append('哈哈4')tList.append('哈哈5')for (tList.end(); tList.hasPrev(); tList.prev()) {console.log(tList.getElement())}結果:
JavaScript數據結構與算法——列表詳解(上)完結
有錯誤歡迎指出,謝謝。下篇將分析一下列表適合解決的實際問題。
晚安~~
總結
以上是生活随笔為你收集整理的JavaScript数据结构与算法——列表详解(上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 水杯创意宣传文案29句
- 下一篇: Some Essential JavaS