链表::循环双链表
2008-02-21 18:57
267 查看
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<title>Untitled Document</title>
</head>
<body>
<p>
循环双链表结构
</p>
<script type="text/javascript">
function cdnode(_code, _name){
this.code = _code;
this.name = _name;
this.front = this;
this.back = this;
}
function create_dclink(arr){
if (!arr || !arr.length) {
return null;
}
var head = null, _back = null;
for (var i = 0, cnt = arr.length; i < cnt; i++) {
var tmpNode = new cdnode(arr[i][0], arr[i][1]);
if (i == 0) {
head = tmpNode;
_back = tmpNode;
}
tmpNode.front = head;
head.back=tmpNode;
tmpNode.back = _back;
_back.front=tmpNode;
_back = tmpNode;
}
return head;
}
function insert_node(head, ptr, newNode){
if (head == null) {
if (newNode != null) {
newNode.back = newNode;
newNode.front = newNode;
}
return newNode;
}
if (ptr == null) {
var backNode = head.back;
backNode.front = newNode;
newNode.back = backNode;
newNode.front = head;
head.back = newNode;
return newNode;
}
var current = head;
while (current.code != ptr.code && current.front != head) {
current = current.front;
}
if (current.code == ptr.code) {
var _front = current.front;
_front.back = newNode;
newNode.back = current;
newNode.front = _front;
current.front = newNode;
}
return head;
}
function delete_node(dclink, nodeId){
if (dclink == null) {
return;
}
var ptr = dclink;
while (ptr.code != nodeId && ptr.front != dclink) {
ptr = ptr.front;
}
if (ptr.code == nodeId) {
if (ptr == dclink) {
dclink = dclink.front;
var backNode = ptr.back;
backNode.front = dclink;
dclink.back = backNode;
}
else {
var frontNode = ptr.front;
var backNode = ptr.back;
frontNode.back = backNode;
backNode.front = frontNode;
}
ptr = null;
}
return dclink;
}
function show_dclink(head){
var current = head
while (current.front!=head) {
document.writeln(current.name + "<br />");
current = current.front;
}
document.writeln(current.name + "<br />");
current = current.front;
}
var data = [[1, "beimu"], [2, "aihui"], [3, "bob"]];
var head = create_dclink(data);
head = delete_node(head, 3);
show_dclink(head);
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<title>Untitled Document</title>
</head>
<body>
<p>
循环双链表结构
</p>
<script type="text/javascript">
function cdnode(_code, _name){
this.code = _code;
this.name = _name;
this.front = this;
this.back = this;
}
function create_dclink(arr){
if (!arr || !arr.length) {
return null;
}
var head = null, _back = null;
for (var i = 0, cnt = arr.length; i < cnt; i++) {
var tmpNode = new cdnode(arr[i][0], arr[i][1]);
if (i == 0) {
head = tmpNode;
_back = tmpNode;
}
tmpNode.front = head;
head.back=tmpNode;
tmpNode.back = _back;
_back.front=tmpNode;
_back = tmpNode;
}
return head;
}
function insert_node(head, ptr, newNode){
if (head == null) {
if (newNode != null) {
newNode.back = newNode;
newNode.front = newNode;
}
return newNode;
}
if (ptr == null) {
var backNode = head.back;
backNode.front = newNode;
newNode.back = backNode;
newNode.front = head;
head.back = newNode;
return newNode;
}
var current = head;
while (current.code != ptr.code && current.front != head) {
current = current.front;
}
if (current.code == ptr.code) {
var _front = current.front;
_front.back = newNode;
newNode.back = current;
newNode.front = _front;
current.front = newNode;
}
return head;
}
function delete_node(dclink, nodeId){
if (dclink == null) {
return;
}
var ptr = dclink;
while (ptr.code != nodeId && ptr.front != dclink) {
ptr = ptr.front;
}
if (ptr.code == nodeId) {
if (ptr == dclink) {
dclink = dclink.front;
var backNode = ptr.back;
backNode.front = dclink;
dclink.back = backNode;
}
else {
var frontNode = ptr.front;
var backNode = ptr.back;
frontNode.back = backNode;
backNode.front = frontNode;
}
ptr = null;
}
return dclink;
}
function show_dclink(head){
var current = head
while (current.front!=head) {
document.writeln(current.name + "<br />");
current = current.front;
}
document.writeln(current.name + "<br />");
current = current.front;
}
var data = [[1, "beimu"], [2, "aihui"], [3, "bob"]];
var head = create_dclink(data);
head = delete_node(head, 3);
show_dclink(head);
</script>
</body>
</html>
相关文章推荐
- C语言单链表,双向链表,循环单链表,循环双链表的基本操作
- 数据结构-02-链表数据结构之双链表和循环链表
- 数据结构学习心得——双链表和循环链表
- 数据结构链表之循环双链表
- 单链表,双链表和循环链表之间的区别详解
- 双链表 、循环链表
- 数据结构【线性表(二)链表】项目之循环双链表应用
- 2015年大二上-数据结构-链表(6)-循环双链表应用
- 双向链表实现队列与循环链表
- 双循环链表遍历
- Java数据结构学习之 有序非循环双向链表的实现
- 单循环链表C++实现
- 链表初解(二)——双链表的创建、删除、插入
- 单循环链表(C语言实现)
- 【剑指offer】递归循环两种方式反转链表
- 苦逼的双向循环链表
- C语言循环链表
- C语言实现双向循环链表
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- 汇编语言循环遍历链表代码分析(16)