使用js封装单向链表

文章介绍了单向链表的数据结构,强调了其在数据删除和插入上的优势,同时指出查找操作的效率较低。通过`Node`和`LinkList`两个类进行了链表的封装,实现了包括push、removeAt、insert、size、isEmpty等方法,支持链表的基本操作。
摘要由CSDN通过智能技术生成

一、单向链表的优点

进行数据的删除、插入简单;

二、单向链表的缺点

进行数据查找时比较慢;

三、使用类进行封装

class Node {
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}


class LinkList {

    constructor() {
        this.count = 0;
        this.head = null;
    }

    push(data) {

        const node = new Node(data);
        if (this.head === null) {
            this.head = node;
        } else {
            let current = this.head;
            while (current.next !== null) {
                current = current.next;
            }
            current.next = node;
        }
        this.count++;
    }

    removeAt(index) {
        if (index >= 0 && index < this.count) {
            let current = this.head;
            if (index == 0) {
                this.head = this.head.next;
            } else {
                let previous;

                for (let i = 0; i < index; i++) {
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
            }
            this.count--;
            return current.element;
        }
        return false
    }

    removeAt2(index) {
        if (index >= 0 && index < this.count) {
            let current = this.head;
            if (index == 0) {
                this.head = this.head.next;
            } else {
                let previous = this.getNodeAt(index - 1);
                current = previous.next;
                previous.next = current.next;
            }
            this.count--;
            return current.element;
        }
    }

    equal(a, b) {
        // return a=== b;
        return JSON.stringify(a) === JSON.stringify(b);
        // imumutable 判断数据相等的插件库
    }

    indexOf(element) {
        let current = this.head;
        for (let i = 0; i < this.count; i++) {
            if (current.element == element) {
                return i;
            }
            current = current.next;

        }
    }

    remove(element) {
        const index = this.indexOf(element);
        this.removeAt(index);
    }

    getNodeAt(index) {
        if (index >= 0 && index < this.count) {
            let node = this.head;
            for (let i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        }
        return false
    }

    insert(element, index) {
        if (0 <= index || index <= this.count) {
            let node = new Node(element);
            if (index == 0) {
                let current = this.head;
                node.next = current;
                this.head = node;
            } else {
                let previous = this.getNodeAt(index - 1);
                let next = previous.next;
                node.next = next;
                previous.next = node;
            }
            this.count++;
            return true;
        }
        return false;
    }

    size() {
        return this.count;
    }

    isEmpty() {
        return this.size() === 0;
    }

    getHead() {
        return this.head;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中没有类的概念,但可以使用结构体和函数指针来实现链表的封装,下面是一个简单的单向链表封装示例。 ```c #include <stdio.h> #include <stdlib.h> typedef struct _node { int value; struct _node* next; } ListNode; typedef struct _linked_list { ListNode* head; int size; void (*insert)(struct _linked_list*, int); void (*remove)(struct _linked_list*, int); void (*print)(struct _linked_list*); void (*clear)(struct _linked_list*); } LinkedList; void insert(LinkedList* list, int value) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->value = value; node->next = NULL; if (list->head == NULL) { list->head = node; } else { ListNode* cur = list->head; while (cur->next) { cur = cur->next; } cur->next = node; } list->size++; } void remove_node(LinkedList* list, int value) { ListNode* cur = list->head; ListNode* prev = NULL; while (cur) { if (cur->value == value) { if (prev == NULL) { list->head = cur->next; } else { prev->next = cur->next; } free(cur); list->size--; return; } prev = cur; cur = cur->next; } } void print(LinkedList* list) { ListNode* cur = list->head; while (cur) { printf("%d ", cur->value); cur = cur->next; } printf("\n"); } void clear(LinkedList* list) { ListNode* cur = list->head; while (cur) { ListNode* next = cur->next; free(cur); cur = next; } list->head = NULL; list->size = 0; } LinkedList* new_linked_list() { LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList)); list->head = NULL; list->size = 0; list->insert = insert; list->remove = remove_node; list->print = print; list->clear = clear; return list; } int main() { LinkedList* list = new_linked_list(); list->insert(list, 1); list->insert(list, 2); list->insert(list, 3); list->print(list); list->remove(list, 2); list->print(list); list->clear(list); free(list); return 0; } ``` 在这个示例中,我们首先定义了两个结构体:`ListNode`和`LinkedList`。`ListNode`表示链表节点,包含一个整数值和指向下一个节点的指针。`LinkedList`表示链表本身,包含指向头节点的指针和链表中节点的数量,以及链表操作的函数指针。 然后,我们定义了一些函数来操作链表。`insert`函数用于在链表末尾插入一个新节点,新节点的值为传入的参数。`remove_node`函数用于从链表中删除一个节点,值为传入的参数。`print`函数用于打印链表中所有节点的值。`clear`函数用于清空链表中的所有节点,释放内存。 在`new_linked_list`函数中,我们创建了一个新的`LinkedList`对象,并将链表操作的函数指针赋值给相应的成员变量。在`main`函数中,我们使用`new_linked_list`函数创建了一个新的链表对象,然后使用链表操作的函数指针来操作链表。 这个示例只是一个简单的单向链表封装,如果需要更复杂的功能,可以在`LinkedList`结构体中添加更多的成员变量和函数,并在函数中实现更多的链表操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值