js封装单向链表

      // 封装链表类
      class LinkedList {
        constructor() {
          // 内部的类:节点类
          function Node(data) {
            this.data = data;
            this.next = null;
          }
          // 属性
          this.head = null;
          this.length = 0;
          // 方法
          let current, previous, index;
          // 1.append方法
          LinkedList.prototype.append = data => {
            const newNode = new Node(data);
            if (this.length === 0) {
              this.head = newNode;
            } else {
              current = this.head;
              while (current.next) {
                current = current.next;
              }
              current.next = newNode;
            }
            this.length += 1;
          };
          // 2.toString方法
          LinkedList.prototype.toString = () => {
            current = this.head;
            let listString = '';
            while (current) {
              listString += current.data + ' ';
              current = current.next;
            }
            return listString;
          };
          // 3.insert方法
          LinkedList.prototype.insert = (position, data) => {
            if (position < 0 || position > this.length) return false;
            const newNode = new Node(data);
            index = 0;
            current = this.head;
            previous = null;
            if (position === 0) {
              newNode.next = this.head;
              this.head = newNode;
            } else {
              while (index++ < position) {
                previous = current;
                current = current.next;
              }
              newNode.next = current;
              previous.next = newNode;
            }
            this.length += 1;
            return true;
          };
          // 4.get方法
          LinkedList.prototype.get = position => {
            if (position < 0 || position >= this.length) return null;
            current = this.head;
            index = 0;
            while (index++ < position) {
              current = current.next;
            }
            return current.data;
          };
          // 5.indexOf方法
          LinkedList.prototype.indexOf = data => {
            current = this.head;
            index = 0;
            while (current) {
              if (current.data === data) {
                return index;
              } else {
                current = current.next;
                index += 1;
              }
            }
            return -1;
          };
          // 6.update方法
          LinkedList.prototype.update = (position, newData) => {
            if (position < 0 || position >= this.length) return false;
            index = 0;
            current = this.head;
            while (index++ < position) {
              current = current.next;
            }
            current.data = newData;
            return true;
          };
          // 7.removeAt方法
          LinkedList.prototype.removeAt = position => {
            if (position < 0 || position > this.length) return null;
            current = this.head;
            index = 0;
            previous = null;
            if (position === 0) {
              this.head = this.head.next;
            } else {
              while (index++ < position) {
                previous = current;
                current = current.next;
              }
              previous.next = current.next;
            }
            this.length -= 1;
            return current.data;
          };
          // 8.remove方法
          LinkedList.prototype.remove = data => {
            const position = this.indexOf(data);
            return this.removeAt(position);
          };
          // 9.isEmpty方法
          LinkedList.prototype.isEmpty = () => this.length === 0;
          // 10.size方法
          LinkedList.prototype.size = () => this.length;
        }
      }

  • 1
    点赞
  • 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、付费专栏及课程。

余额充值