1、线性表
1.1 线性表顺序存储结构--顺序表
在内存中开辟一段连续的存储空间,用一组连续的存储单元依次存储数据元素。
1.2 线性表的链式存储结构--链表
单链表、循环链表、双向链表。
js实现链表:(js数组可以不指定大小,用function构造类来实现链表 )
<html>
<head>
<script type="text/javascript">
function myclass(value)
{
this.data=value;
this.next=null;
}
var first=null;
var last=null;
function run()
{
a=new myclass(document.getElementById("txt").value);
if (first==null)
{
first=a;
last=a;
}
else
{
last.next=a;
last=a;
}
document.getElementById("txt").value="";
alert(a.data);
}
function show()
{
for(temp=first;temp!=null;temp=temp.next)
{
alert(temp.data);
}
}
</script>
</head>
<body>
<input type=text width=10 id="txt">
<input type=button value="add" οnclick="run()">
<input type=button value="show" οnclick="show()">
</body>
</html>
该代码实现点击“add”将text 中的内容加入到链表中。点击“show”将整个链表的内容输出。你可以参考一下,树结构也可以由相似方法实现。
js实现双向链表:
linkedlistnode.js 节点类
/*
* 链表节点
*/
Dare.LinkedListNode = function () {
this.data = null;//数据域
this.prev = null;//前驱
this.next = null;//后驱
};
Dare.extend(Dare.LinkedListNode, Dare);
Dare.LinkedListNode.prototype.getValue = function () {
return this.data;
};
Dare.LinkedListNode.prototype.setValue = function (obj) {
this.data = obj;
};
Dare.LinkedListNode.prototype.getPrev = function () {
return this.prev;
};
Dare.LinkedListNode.prototype.setPrev = function (node) {
this.prev = node;
};
Dare.LinkedListNode.prototype.getNext = function () {
return this.prev;
};
Dare.LinkedListNode.prototype.setNext = function (node) {
this.prev = node;
};
linkedlist.js 链表类
/*
* 双向链表
*/
Dare.LinkedList = function () {
this.head = null;
this.current = null;
this.tail = null;
this.length = 0;
};
Dare.extend(Dare.LinkedList, Dare);
/*
* 尾插法添加节点
*/
Dare.LinkedList.prototype.appendNode = function (node) {
if (this == null) return;
if (node == null) return;
var tail = this.tail;
if (tail == null) {
this.tail = this.head = node;
}
else {
tail.next = node;
node.prev = tail;
this.tail = node;
}
this.length++;
};
/*
* 删除节点
*/
Dare.LinkedList.prototype.moveNode = function (node) {
if (this == null) return;
if (node == null) return;
//中间节点
var prev = node.prev;
if (prev != null) {
prev.next = node.next;
}
if (node.next != null) {
node.next.prev = prev;
}
//头节点
if (node == this.head) {
this.head = node.next;
}
//尾节点
if (node == this.tail) {
if (prev) {
this.tail = prev;
}
else {
this.head = this.tail;
}
}
node.prev = null;
node.next = null;
this.length--;
};
/*
* 构造节点
*/
Dare.LinkedList.prototype.constructNode = function (node, obj) {
if (node == null || obj == null) return;
node.data = obj;
return node;
};
/*
* 获取节点数据
*/
Dare.LinkedList.prototype.getNodeData = function (node) {
if (node == null) return;
return node.data;
};
/*
* 从头开始
*/
Dare.LinkedList.prototype.start = function () {
if (this == null) return;
return this.current = this.head;
};
/*
* 从尾开始
*/
Dare.LinkedList.prototype.end = function () {
if (this == null) return;
return this.current = this.tail;
};
/*
* 下个节点
*/
Dare.LinkedList.prototype.nextNode = function () {
if (this == null) return;
if (this.current == null) return
var node = this.current;
this.current = this.current.next;
return node;
};
/*
* 上个节点
*/
Dare.LinkedList.prototype.prevNode = function () {
if (this == null) return;
if (this.current == null) return
var node = this.current;
this.current = this.current.prev;
return node;
};
/*
* 链表是否空
*/
Dare.LinkedList.prototype.isempty = function () {
if (this == null) return true;
if (this.head == null) {
return true;
}
else {
return false;
}
};
/*
* 链表长度
*/
Dare.LinkedList.prototype.getLength = function () {
if (this == null) return;
return this.length;
};
/*
* 清空链表
*/
Dare.LinkedList.prototype.clearList = function () {
this.head.next = null;
this.head = null;
};
/*
* 是否存在节点
*/
Dare.LinkedList.prototype.containsNode = function (node) {
if (this == null) return false;
if (this.isempty()) return false;
var current = null;
var next = null;
this.start();
current = this.nextNode();
while (current != null) {
next = this.nextNode();
if (current == node) {
return true;
}
current = next;
}
};
实战调用用例代码陆续更新:
<script type="text/javascript">
var linkedList = new Dare.LinkedList();//实例化链表对象
function createList() {
for (var i = 0; i < 7; i++) {
var movie= {};//json对象作为数据域
var linkedListNode = new Dare.LinkedListNode();//实例化节点对象
movie.id = i;
movie.name = 'movie_' + i;
linkedListNode.data = movie;
linkedList.appendNode (linkedListNode);//尾插法创建链表
}
printList(linkedList);//打印输出链表
}
function printList(list) {
var node=list.head;
if (node == null) return;
var html = '';
while (node!= null) {
var movie= node.data;
html += movie.id + "|" + movie.name + "<br>";
node = node.next;
}
document.write(html);
}
</script>
内置数据类型和标准程序库:
内置类型指的是C++的标准数据类型,比如int、double...还有由这些类型衍生出的数组、指针等等。
vector是c++标准程序库STL中的成员,一般称为容器(container),除了vector外,STL还有deque、map、list等等。
内置数据类型是最基本的数据类型,标准库类是一个一个的C++对象。