StackList Library For Arduino

Author: Efstathios Chatzikyriakidis
Contact: contact@efxa.org

Library was updated for Arduino 1.0


Navigation


Current Version

1.0 2010-09-23 - Efstathios Chatzikyriakidis - added exit(), blink(): error reporting and handling methods.


History

1.0 2010-09-23 - Efstathios Chatzikyriakidis - added exit(), blink(): error reporting and handling methods.
1.0 2010-09-20 - Alexander Brevig - added setPrinter(): indirectly reference a Serial object.
1.0 2010-09-15 - Efstathios Chatzikyriakidis - initial release of the library.


Description

StackList is a library implementing a generic, dynamic stack (linked list version) for the Arduino.

It is created to help adding LIFO (Last In - First Out) Abstract Data Structure to a program for any use.

StackList library is part of the "Data Structures & Algorithms" libraries.


Installation

Download here: StackList.zip

Put the "StackList" directory in "libraries" directory.

In the Arduino IDE, create a new sketch and select from the menubar "Sketch->Import Library->StackList".

Once the library is imported, an "#include <StackList.h>" line will appear at the top of your Sketch.


Creation

Replace "T" with the data type you want to use.

StackList <T> stack;

Create instance (object) of a StackList class.


Methods

Replace "T" with the data type you want to use.

void push (const T item)

Push an item with data type "T" to the stack.

T pop ()

Pop an item with data type "T" from the stack.

T peek ()

Get an item with data type "T" from the stack.

bool isEmpty ()

Check if the stack is empty.

int count ()

Get the number of items in the stack.

void setPrinter (Print & printer)

Sets the printer of the stack.


Examples

Swapping strings by using a generic, dynamic stack data structure.

/*
 *  Swapping strings by using a generic, dynamic stack data structure.
 *
 *  Copyright (C) 2010  Efstathios Chatzikyriakidis (contact@efxa.org)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


// include stack library header.
#include <StackList.h>

// declare string messages.
String a = "Happy Hacking!";
String b = "Hacking Happy!";

// create a stack of strings messages.
StackList <String> stack;

// startup point entry (runs once).
void
setup () {
  // start serial communication.
  Serial.begin (9600);

  // set the printer of the stack.
  stack.setPrinter (Serial);
}

// loop the main sketch.
void
loop () {
  // print the string messages.
  Serial.print ("a: "); Serial.println (a);
  Serial.print ("b: "); Serial.println (b);

  // push the strings to the stack.
  stack.push (a);
  stack.push (b);

  // pop the strings from the stack.
  a = stack.pop ();
  b = stack.pop ();

  // delay 1 second.
  delay (1000);
}


FAQ

  • How do I use multiple stacks?

StackList is a class. Therefore to use multiple stacks, you must create an instance for each of them.


Information

Last Modified: February 11, 2012, at 10:58 AM

By: efxa

Share