线性表定义
线性表是是n个数据元素的有限序列。
线性表的顺序表示和实现
用一组地址连续的存储单元依次存储线性表的数据元素。 查找的时间复杂度为O(1),但是插入和删除元素时,需要将插入或者删除以后的元素往前或往后移一个单位长度。时间复杂度为O(n)。
线性表的链式表示和实现
用一组任意的存储单元存储线性表的数据元素。
function logLinear(){//构造链表并输出
// 构造链表 并输出链表的数据
function linearNode(value,next){ //构造函数 构造线性表的节点结构
this.value = value;
this.next = next; //指向下一个节点的指针
}
var linearHeadNode = new linearNode(null), //标识链表
node = linearHeadNode; //node 用来作为存储临时结点
for (var i = 0; i < 20; i++) {
node.next = new linearNode(i);
node = node.next;
}
node = linearHeadNode;
while (node.next) {
console.log(node.next);
node = node.next;
}
}
function mathlinear(){ //进行一元多项式的计算
// 构造链表 并输出链表的数据
function linearNode(qa,qb,next){ //构造函数 构造线性表的节点结构
this.qa = qa;
this.qb = qb;
this.next = next; //指向下一个节点的指针
}
var linearListA = new linearNode(), //标识链表
linearListB = new linearNode(),
linearListC = new linearNode(),
nodeC = linearListC,
nodeB = linearListB,
nodeA = linearListA; //node 用来作为存储临时结点
for (var i = 0; i < 4; i++) {
nodeA.next = new linearNode(i, i+3);
nodeB.next = new linearNode(i+2, i+2);
nodeA = nodeA.next;
nodeB = nodeB.next;
}
nodeA = linearListA;
nodeB = linearListB;
while (nodeA.next&&nodeB.next) {
if (nodeA.next.qb<nodeB.next.qb) {
nodeC.next = nodeA.next;
nodeC = nodeC.next;
nodeA = nodeA.next;
}else if (nodeA.next.qb===nodeB.next.qb) {
nodeC.next = nodeA.next;
nodeC.next.qa = nodeA.next.qa+nodeB.next.qa;
nodeC = nodeC.next;
nodeB = nodeB.next;
nodeA = nodeA.next;
}else {
nodeC.next = nodeB.next;
nodeC = nodeC.next;
nodeB = nodeB.next;
}
}
nodeC = linearListC;
while (nodeC.next) {
console.log("系数:"+nodeC.next.qa +"指数:"+nodeC.next.qb);
nodeC = nodeC.next;
}
}
循环链表
表中最后一个结点的指针域指向头结点。由此从表中任意位置出发都可以找到表中其他结点。
双向链表
双向链表的结点中有两个指针域,其一指向直接后继,另一指向直接前继。
由于链表在空间的合理利用上和插入,删除时不需要移动等优点,因此在许多场合,她是线性表的首选存储结构