Author's original: http://hawkzz.com/blog/blog/1514966479779
Brief introduction
Recently, in the reading "Data structure and algorithm JavaScript description", this article, just read the notes, is a repetition of the wheel, feel useful to see, feel useless, do not spray;
Defined
A list is an ordered set of data, and the items in each list are called elements, and in JavaScript, the elements in the list can be any data type. A list that does not contain any elements is called an empty list.
Abstract data type definitions for lists
Abstract data Types |
definition |
Listsize (attributes) |
Number of elements in the list |
POS (properties) |
The current position of the list |
Length (property) |
Returns the number of elements in a list |
Clear (method) |
Empty all the elements in the list |
ToString (method) |
Returns the string form of the list |
GetElement (method) |
Returns the element at the current position |
Insert (method) |
Inserting a new element after an existing element |
Append (method) |
Add a new element at the end of the list |
Remove (method) |
Remove an element from the list |
Find (method) |
Find the location of an element from the list |
Front (method) |
Moves the current position of a list to the first element |
End (method) |
Move the current position of the list to the last element |
Prev (method) |
Move the current position forward one |
Next (method) |
Move the current position back one |
Currpos (method) |
Returns the current position of the list |
MoveTo (method) |
Moves the current position to the specified position |
Contains (method) |
Find whether an element exists from the list |
Implementation of the List class
1. Create a list class
Define the list constructor to create a listing class based on the defined lists abstract data type
function List() { this.dataList = []; this.listSize = 0; this.pos = 0; this.len = length; this.clear = clear; this.toString = toString; this.getEle = getEle; this.insert = insert; this.append = append; this.remove = remove; this.find = find; this.front = front; this.end = end; this.prev = prev; this.next = next; this.currPos = currPos; this.moveTo = moveTo; this.contains = contains}
2. Add elements to the list: Append
function append(ele) { //添加元素 this.listStze += 1; this.dataList[this.listSize - 1] = ele;}
This method adds an element to the list, expands it, and then adds the element;
3. The location of the query list element: find
function find(ele){ for(var i = 0; i < this.listSize; i++ ){ if(ele === this.dataList[i]){ return i; } } return -1;}
The method finds the position of the element, returns the position of the element if it is found, or returns-1;
4. Remove the list element: remove
function remove(ele) { //删除元素 var index = this.find(ele); if (index > -1) { this.dataList.splice(index, 1); this.listSize -= 1; return true; } return false;}
The method deletes the list element, first querying for the existence of the element through the find () method, and, if present, obtains its subscript, removes the element by splice (), and the list length is reduced by 1;
5. Whether the query element exists: contains
function contains(ele){ var index = this.find(ele); if (index > -1) { return true; } return false;}
6. Inserting elements: Insert
function insert(ele, after) { //插入元素 var index = this.find(after); if (index > -1) { this.dataList.splice(index + 1, 0, ele); this.listSize += 1; }else{ this.append(ele); }}
The change method implements inserting an element behind an element, first using the Find () method to query for the existence of an after element in the list, and, if present, after the after element, inserting the ele element, if it does not exist, in the last face of the list;
7, query the length of a list: length
function length() { //列表长度 return this.listSize;}
8. Clear the list: clear
function clear() { //清空列表 delete this.dataList; this.dataList = []; this.pos = 0; return true;}
This method empties the list, first deletes the old list, and then creates a new list with the pointer reset to 0;
9, get the current position of the element; Getele;
function getEle() { return this.dataList[this.pos];}
10. Set the current position to start with: Front
function front() { //设置当前位置是0 this.pos = 0;}
11, set the current position is the last element position: End
function end() { this.pos = this.listSize - 1;}
12. Set the pointer to move forward one: prev
function prev() { if (this.pos > 0) { this.pos -= 1; } else { this.front(); }}
13. Set the pointer to move backwards one: Next
function next() { //设置当前位置向后移动 if (this.pos < this.listSize - 1) { this.pos += 1; } else { this.pos = this.end(); }}
14. Get the position of the current pointer: Currpos
function currPos() { //返回当前位置 return this.pos;}
12, move when specified position: MoveTo
function moveTo(position) { //移动当指定位置 if (position >= 0 && position <= this.listSize - 1) { this.pos = position; return true; } return false;}
13. List Loop
First, create a list;
var list = new List();
Then, import the data into the list
var arr = ['a','b','c','d','e'];for(var i = 0;i < arr.length ; i ++){ list.append(arr[i]);}
Next, the Loop list
for(list.front;list.currPos < list.length(); list.next()){ console.log(list.getEle());}
Or
for(list.end();list.currPos >= 0 ; list.prev()){ console.log(list.getEle());}
JavaScript Implementation list Structure