Skip to content

Arduino Library implementing a generic, dynamic stack (array version).

License

Notifications You must be signed in to change notification settings

oogre/StackArray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StackArray Library For Arduino

Author: Efstathios Chatzikyriakidis Contact: [email protected] Library was updated for Arduino 1.0

Current Version

1.0 2014-09-22 - Vincent Evrard - added unshift(): add item to the array. (javascript like)

History

1.0 2010-09-25 - Efstathios Chatzikyriakidis - added resize(): for growing, shrinking the array size. 1.0 2010-09-25 - Efstathios Chatzikyriakidis - added resize(): for growing, shrinking the array size. 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

StackArray is a library implementing a generic, dynamic stack (array version) for the Arduino.

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

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

Installation

Download here: StackArray.zip

Put the "StackArray" directory in "libraries" directory.

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

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

Creation

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

StackArray stack;

Create instance (object) of a StackArray class.

Methods

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

void unshift (const T item)

Add an item with data type "T" to the top of stack.

void push (const T item)

Add an item with data type "T" to the bottom of stack.

T pop ()

Remove and return an item with data type "T" from the bottom stack.

T peek ()

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

bool isEmpty ()

Check if the stack is empty.

bool isFull ()

Check if the stack is full.

int count ()

Get the number of items in the stack.

void setPrinter (Print & printer)

Sets the printer of the stack.

Examples

Reverse a string by using a generic, dynamic stack data structure.

/*
 *  Reverse a string by using a generic, dynamic stack data structure.
 *
 *  Copyright (C) 2010  Efstathios Chatzikyriakidis ([email protected])
 *
 *  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 <StackArray.h>

// declare a string message.
const String msg = "Happy Hacking!";

// create a stack of characters.
StackArray <char> stack;

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

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

  // print the message before reversing.
  Serial.println ("Normal String: " + msg);

  // push all the message's characters to the stack.
  for (int i = 0; i < msg.length (); i++)
    stack.push (msg.charAt (i));

  // print the message after reversing.
  Serial.print ("Reversed String: ");

  // pop all the message's characters from the stack.
  while (!stack.isEmpty ())
    Serial.print (stack.pop ());

  // print end of line character.
  Serial.println ();
}

// loop the main sketch.
void loop () {
  // nothing here.
}

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

/*
 *  Swapping numbers by using a generic, dynamic stack data structure.
 *
 *  Copyright (C) 2010  Efstathios Chatzikyriakidis ([email protected])
 *
 *  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 <StackArray.h>

// declare two numbers.
double a = 1.1;
double b = 2.2;

// create a stack of numbers.
StackArray <double> 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 values of the numbers.
  Serial.print ("a: "); Serial.println (a);
  Serial.print ("b: "); Serial.println (b);

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

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

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

Store a string by using a generic, dynamic stack data structure.

/*
 *  Store a string by using a generic, dynamic stack data structure.
 *
 *  Copyright (C) 2014  Vincent Evrard ([email protected])
 *
 *  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 <StackArray.h>

// declare a string message.
const String msg = "Happy Hacking!";

// create a stack of characters.
StackArray <char> stack;

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

  delay(2000);

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

  // print the message before reversing.
  Serial.println ("Normal String: " + msg);

  // push all the message's characters to the stack.
  for (int i = 0; i < msg.length (); i++)
    stack.unshift(msg.charAt (i));

  // print the message after reversing.
  Serial.print ("Reversed String: ");

  // pop all the message's characters from the stack.
  while (!stack.isEmpty ())
    Serial.print( stack.pop() );

  // print end of line character.
  Serial.println ();
}

void loop(){
  // nothing here.
}

FAQ

How do I use multiple stacks? StackArray is a class. Therefore to use multiple stacks, you must create an instance for each of them.

About

Arduino Library implementing a generic, dynamic stack (array version).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages