JS实现单链表

函数功能:实现单链表的初始化,插入节点、删除节点、查找节点和删除节点功能;

/**
 * 单链表构造函数
 */
function LinkedList () {
    this.ptr = null;
    this.head = null;

    this.length = 0;
    this.insert = insertNode;
    this.delete = deleteNode;
    this.search = searchNode;
    this.traverse = traverseNode;
}

/**
 * 定义链表节点
 */ 
function ListNode (data) {
    this.data = data;
    this.next = null;
}

/**
 * 插入节点
 */
function insertNode (data) {
    var node = new ListNode(data);
    this.ptr = this.head;

    if (!this.ptr) {
        this.ptr = node;
        this.head = node;
    } else {
        while (this.ptr) {
            if (this.ptr.next) {
                this.ptr = this.ptr.next;
            } else {
                break;
            }
        }
        this.ptr.next = node;
    }
    this.length++;
}

/**
 * 搜索节点
 */
function searchNode (data) {
    this.ptr = this.head;
    if (!this.ptr) {
        return false;
    }
    while (this.ptr) {
        if (this.ptr.data === data) {
            return true;
        } else if (this.ptr.next) {
            this.ptr = this.ptr.next;
        } else {
            return false;
        }
    }
    this.ptr = this.head;
}

function deleteNode (data) {
    this.ptr = this.head;
    if (!this.ptr) {
        return false;
    } else if (this.search(data)) {
        this.ptr = this.head;
        if (this.ptr.data === data) {
            this.ptr = null;
            this.head = null;
        } else {
            while (this.ptr.next) {
                if (this.ptr.next.data === data) {
                    if (this.ptr.next.next) {
                        this.ptr.next = this.ptr.next.next;
                    } else {
                        this.ptr.next = null;
                    }
                    return true;
                } else {
                    this.ptr = this.ptr.next;
                }
            }
        }
        this.length--;
    } else {
        return false;
    }
}

/**
 * 遍历输出
 */
function traverseNode () {
    var p = this.head;
    if (!this.head) {
        return false;
    }
    while (this.head) {
        console.log(this.head.data);
        if (this.head.next) {
            this.head = this.head.next;
        } else {
            break;
        }
    }

    this.head = p;
}

/**
 * 序列测试
 */
var list = new LinkedList();
list.insert('a');
console.log(list.length);
list.insert('b');
console.log(list.length);
list.insert('c');
console.log(list.length);
list.traverse();
console.log(list.search('b'));
list.delete('b');
list.insert('d');
list.traverse();
list.delete('a');
list.delete('c');
list.delete('d');
list.traverse();
list.insert('e');
list.traverse();

结果截图:

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Neil-

你们的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值