npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gxc-js-data-structure

v1.0.15

Published

Use JavaScript to implement some data structures and algorithms

Downloads

73

Readme

一个 JS 实现的部分数据结构的包

Stack(栈)

栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶。咖啡厅内的一摞盘子是现实世界中常见的栈的例子。只能从最上面取盘子,盘子洗净后,也只能摞在这一摞盘子的最上面。栈被称为一种后入先出(LIFO,last-in-first-out)的数据结构。

创建栈

import { Stack } from "gxc-js-data-structure";

// 不传参数为空栈
const stack1 = new Stack();
// 可传入一个数组转换为栈
const stack2 = new Stack([]);

添加一个新元素到栈顶

const stack1 = new Stack();
stack1.push(5);

移除栈顶元素

const stack1 = new Stack();
stack1.pop();

返回栈顶元素

const stack1 = new Stack();
stack1.peek();

判断是否空栈

const stack1 = new Stack();
stack1.isEmpty(); // true or false

返回栈里元素个数,类似数组的 length

const stack1 = new Stack();
stack1.size;

将栈结构的内容以字符串的形式返回

const stack1 = new Stack();
stack1.toString();

Queue(队列)

创建队列

import { Queue } from "gxc-js-data-structure";
const queue1 = new Queue();
// 或者传入初始数组
const queue2 = new Queue([]);

向队列尾部添加一个或多个新的项

import { Queue } from "gxc-js-data-structure";
const queue = new Queue();
queue.enqueue(item);

移除队列的第一个项(最前面),并返回被移除的元素

import { Queue } from "gxc-js-data-structure";
const queue = new Queue();
queue.dequeue();

返回队列中第一个元素——最先被添加的项,不改变队列,类似栈的 peek

import { Queue } from "gxc-js-data-structure";
const queue = new Queue();
queue.front();

判断是否空队列

const queue = new Queue();
queue.isEmpty(); // true or false

返回队列元素个数

const queue = new Queue();
queue.size; // true or false

将队列中的内容转成字符串形式

const queue = new Queue();
queue.toString(); // true or false

LinkedList(链表)

创建链表

import { LinkedList } from "gxc-js-data-structure";
const lList = new LinkedList();

链表节点个数

lList.size

判断是否为空链表

lList.isEmpty

指定位置插入节点

lList.insert(item, index) // index为插入位置,默认为链表尾部

指定位置节点

lList.update(item, index) // index为指定节点位置

获取指定位置的节点

lList.find(index) // index为需要获取节点的位置