Code Monkey home page Code Monkey logo

ll's Introduction

Base Structs

This library provides two base structs, which handle the linking of nodes :

  1. SingleLink :
  • for singularly linked lists, à la C++ STL's forward_list.
  • size is that of a single pointer.
  1. DoubleLink :
  • for doubly linked lists, à la C++ STL's list.
  • size is of two pointers.
  • inherits from SingleLink, so all SingleLink methods may be used on it.

To compose your linked list, simply inherit by making it the first member of your node struct :

struct node { DoubleLink link; T value; };

What this inheritance means is that a struct node * and a DoubleLink * can be correctly casted back and forth, which is crucial for the next feature provided by the library.

Range-based Loops

Loops of the for_n(), until() and for_all() variety (including _back() versions for iterating in reverse) are provided as macros expanding to for loop statements.

Many examples can be found in ll.c, which uses these to implement methods. One particularly useful demonstration is :

/* Count the number of nodes between begin and end, returning -1 if end not found */
intmax_t DoubleLink_len(const DoubleLink *begin, const DoubleLink *end, bool back)
{
        uintmax_t cnt = 0;
        if (back) {
                if (end)
                        DoubleLink_until_back(begin, end) {
                                if (!begin->prv) return -1; /* We're at the last node */        
                                else cnt++;
                        }
                else /* Count all nodes till list terminates if end is NULL */
                        DoubleLink_for_all_back(begin)
                                cnt++;
        } else
                if (end)
                        DoubleLink_until(begin, end) {
                                if (!begin->nxt) return -1;
                                else cnt++;
                        }
                else
                        DoubleLink_for_all(begin)
                                cnt++;
        return cnt;
}

These loops are not only convenient and error-free, but also very expressive to a future reader. They may be used for any strut node * where the struct node inherits from an appropriate base struct.

Methods

Methods are provided symetrically for both types of links. All methods accept ranges as input, so they can operate on single nodes, whole lists, and anything in between. You must ensure that passed ranges are valid, and do not overlap where that is prohibited.

  • get() : to return the nth node from a given node.
  • len() : to count the number of nodes between two nodes, the inverse of get().
  • insert() : to link a range of nodes after a given node.
  • remove() : to de-link a range of nodes, the inverse of insert().
  • swap() : to exchange the linking of two ranges of nodes.
  • reverse() : to invert the order of linking for a range of nodes.

To see examples, take a look at sample.c.

ll's People

Contributors

a-p-jo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.