Coder Social home page Coder Social logo

simplelist's Introduction

SimpleList

Use std::list whenever possible, or at least LinkedList, as it's more tested and still maintained!!!

Nothing big, just my own implementation of a linked list in c++ for all kind of Arduino projects.
This Arduino Library was inspired by the LinkedList and is mostly compatible too it.
I made it to get a deeper understanding of lists.

Installation

  1. Download the source code from GitHub.
  2. Unzip and rename the Folder name to "SimpleList".
  3. Paste it in your Library folder (Usually located somewhere at documents/Arduino/libraries).
  4. Restart the Arduino IDE.

You can also just download the SimpleList.h file and paste it in your Arduino sketch folder.

Usage

Include the library

#include <SimpleList.h>  

Creating a SimpleList

// A list of integer
SimpleList<int> *myLinkedList = new SimpleList<int>();

// A list of 'MyClass'
SimpleList<MyClass> *mySimpleList = new SimpleList<MyClass>();

Getting the list size

int theSize = myList->size();

Adding compare function

// Add a compare function to sort or search the list
    list->setCompare([](int &a, int &b) -> int {
      if(a < b) return -1;
      if(a == b) return 0;
      if(a > b) return 1;  
    });

Adding elements

// add(obj) will add the object at the end of the list
myList->add(myObject);

// add(index, obj) method will insert the object at the specified index
myList->add(0, myObject); // Add at the beginning
myList->add(3, myObject); // Add at index 3

// insert will try to put the object at the correct spot to keep the list isSorted (compare function is required!)
myList->insert(myObject);

Getting elements

// Get the first element
myObject = myList->get(0);

// Get the third element
myObject = myList->get(2);

// Get the last element
myObject = myList->get(myList->size() - 1);

Sorting the list

// PLEASE NOTE: compare function must be set!

// Sort the list
list->sort();

// Check if list is currently sorted
bool isSorted = list->isSorted();

Replacing elements

// Replace the first element
myList->replace(0, myObject);

// Replace the third element
myList->replace(2, myObject);

// Replace the last element
myList->replace(myList->size() - 1, myObject);

Removing elements

// Remove the first object
myList->remove(0);

// pop() will remove and return the last element
myDeletedObject = myList->pop();

// shift() will remove and return the first element
myDeletedObject = myList->shift();

// clear() will erase the entire list, leaving it with 0 elements
myList->clear();

// Please note that clear() wont free memory from pointers, you have to manually delete/free those!
// Example:
while(list->size() > 0){
	delete myList->get(0).somePointer;
	list->remove(0);
}

Searching for elements

// PLEASE NOTE: compare function must be set!

// seach() returns the index of the element, not the element itself!
int indexOfSeven = list->search(7);

// When the list is sorted, you can also do a more efficient binary search
// here find the element
int indexOfIntOne = list->binSearch(1);

Counting elements

// PLEASE NOTE: compare function must be set!
int numberOfZeros = myList->count(0);

Swapping elements

// swap(index-X, index-Y)
list->swap(0, list->size()-1);

simplelist's People

Contributors

fabianonline avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.