native-linked-list
v0.4.0
Published
A library supports `single linked list` `double linked list` and `lru list`
Downloads
2
Readme
linked list
A library supports single linked list
double linked list
and lru list
.
How to use
#include <iostream>
#include "LinkedList.h"
#include "DoubleLink.h"
#include "LRUList.h"
using namespace std;
int main(int argc, char **argv)
{
LinkedList<int>* list = new LinkedList<int>();
list->insertTail(1);
list->insertTail(2);
list->insertTail(3);
list->remove(2);
list->insertHead(9);
list->traversal();
ListElement<int>* e = list->find(5);
if (e != NULL) {
cout << "check none exist find failed" << endl;
return 1;
}
cout << "check none exist find success" << endl;
e = list->find(3);
if (e == NULL || e->data != 3) {
cout << "check exist find failed" << endl;
return 2;
}
cout << "check exist find success" << endl;
DoubleLink<int>* dlink = new DoubleLink<int>();
dlink->insertFirst(1);
dlink->appendLast(2);
dlink->appendLast(3);
dlink->appendLast(4);
DNode<int>* node = dlink->findNode(3);
if (node == NULL) {
cout << "double link find error" << endl;
return 3;
}
dlink->deleteLast();
dlink->deleteFirst();
dlink->traversal();
LRUList<int>* lru = new LRUList<int>(3);
RemovedTail<int> result;
lru->addNewElement(1,result);
cout << "remove tail " << result.tailRemoved << endl;
lru->addNewElement(2, result);
cout << "remove tail " << result.tailRemoved << endl;
lru->addNewElement(3, result);
cout << "remove tail " << result.tailRemoved << endl;
lru->addNewElement(4, result);
cout << "remove tail " << result.tailRemoved << " " << result.value << endl;
lru->addNewElement(5, result);
cout << "remove tail " << result.tailRemoved << " " << result.value << endl;
lru->traversal();
return 0;
}
API
See the online api document.
Node addon
A node wrapper is supplied, only supports lru list now. See its api.
Install npm package
npm install native-linked-list --save
Demo
const {LRUList} = require('native-linked-list');
const {expect} = require('chai');
const list = new LRUList(3);
list.addOne(1,function(tailRemoved,tail) {
expect(tailRemoved).to.equal(false);
expect(tail).to.equal(null);
expect(list.size()).to.equal(1);
});
list.addOne(2,function(tailRemoved,tail) {
expect(tailRemoved).to.equal(false);
expect(tail).to.equal(null);
expect(list.size()).to.equal(2);
expect(list.get(0)).to.equal('2');
expect(list.get(1)).to.equal('1');
});
list.addOne(3,function(tailRemoved,tail) {
expect(tailRemoved).to.equal(false);
expect(tail).to.equal(null);
expect(list.size()).to.equal(3);
expect(list.get(0)).to.equal('3');
expect(list.get(1)).to.equal('2');
expect(list.get(2)).to.equal('1');
});
list.addOne(4,function(tailRemoved,tail) {
expect(tailRemoved).to.equal(true);
expect(tail).to.equal('1');
expect(list.size()).to.equal(3);
expect(list.get(0)).to.equal('4');
expect(list.get(1)).to.equal('3');
expect(list.get(2)).to.equal('2');
});