Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision db066d756f5b01d67a9bcb1ea28c9571452a3762)
+++ src/Makefile.am	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
@@ -1,4 +1,4 @@
 SOURCE = atom.cpp bond.cpp builder.cpp config.cpp element.cpp helpers.cpp molecules.cpp moleculelist.cpp periodentafel.cpp vector.cpp verbose.cpp
-HEADER = defs.hpp helpers.hpp molecules.hpp
+HEADER = defs.hpp helpers.hpp molecules.hpp stackclass.hpp
 
 bin_PROGRAMS = molecuilder joiner analyzer
Index: src/atom.cpp
===================================================================
--- src/atom.cpp	(revision db066d756f5b01d67a9bcb1ea28c9571452a3762)
+++ src/atom.cpp	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
@@ -106,229 +106,2 @@
 };
 
-/******************************** Functions for class AtomStackClass ********************************/
-
-/** Constructor of class AtomStackClass.
- */
-AtomStackClass::AtomStackClass(int dimension)
-{
-  CurrentLastEntry = 0;
-  CurrentFirstEntry = 0;
-  NextFreeField = 0;
-  EntryCount = dimension;
-  StackList = (atom **) Malloc(sizeof(atom *)*EntryCount, "AtomStackClass::AtomStackClass: **StackList");
-};
-
-/** Destructor of class AtomStackClass.
- */
-AtomStackClass::~AtomStackClass()
-{
-  Free((void **)&StackList, "AtomStackClass::AtomStackClass: **StackList");
-};
-
-/** Pushes an object onto the stack.
- * \param *object atom to be pushed on stack
- * \return true - sucess, false - failure, meaning stack field was occupied
- */
-bool AtomStackClass::Push(atom *object)
-{
-  if (!IsFull()) {    // check whether free field is really not occupied
-    StackList[NextFreeField] = object;
-    CurrentLastEntry = NextFreeField;
-    NextFreeField = (NextFreeField + 1) % EntryCount; // step on to next free field
-    return true;
-  } else {
-    cerr << "ERROR: Stack is full, " << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tNextFreeField " << NextFreeField << "\tEntryCount " << EntryCount << "!" << endl;
-    return false;
-  }
-};
-
-/** Pops first/oldest atom from stack.
- * First in, last out.
- * \return atom pointer from stack, NULL - if failure (no atom pointer in field)
- */
-atom *AtomStackClass::PopFirst()
-{
-  atom *Walker = NULL;
-  if (!IsEmpty()) {
-    Walker = StackList[CurrentFirstEntry];
-    if (Walker == NULL)
-      cerr << "ERROR: Stack's field is empty!" << endl;
-    StackList[CurrentFirstEntry] = NULL;
-    if (CurrentFirstEntry != CurrentLastEntry) { // hasn't last item been popped as well?
-      CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)
-    } else {
-      CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)
-      CurrentLastEntry = CurrentFirstEntry;
-    }
-  } else
-    cerr << "ERROR: Stack is empty!" << endl; 
-  return Walker;
-};
-
-/** Pops last atom from stack.
- * First in, first out.
- * \return atom pointer from stack, NULL - if failure (no atom pointer in field)
- */
-atom *AtomStackClass::PopLast()
-{
-  atom *Walker = NULL;
-  if (!IsEmpty()) {
-    Walker = StackList[CurrentLastEntry];
-    StackList[CurrentLastEntry] = NULL;
-    if (Walker == NULL)
-      cerr << "ERROR: Stack's field is empty!" << endl;
-    NextFreeField = CurrentLastEntry;
-    if (CurrentLastEntry != CurrentFirstEntry)  // has there been more than one item on stack
-      CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; // step back from current free field to last (modulo does not work in -1, thus go EntryCount-1 instead)
-  } else {
-    cerr << "ERROR: Stack is empty!" << endl;
-  } 
-  return Walker;
-};
-
-/** Removes a certain item from the stack.
- * Item is seeked between \a CurrentFirstEntry and \a CurrentLastEntry, if found, place in stack is NULL'd and
- * all subsequent items shifted by one position downward (with wrapping taken into account).
- * \param *ptr adress of item
- * \return true - item was removed, false - item was not found
- */
-bool AtomStackClass::RemoveItem(atom *ptr)
-{
-  bool found = false;
-  cout << Verbose(5) << "First " << CurrentFirstEntry<< "\tLast " << CurrentLastEntry<< "\tNext " << NextFreeField<< "\tCount " << EntryCount<< "." << endl;
-  int i=CurrentFirstEntry;
-  if (!IsEmpty())
-    do {
-      if (StackList[i] == ptr) {  // if item found, remove
-        cout << Verbose(5) << "Item " << *ptr << " was number " << i << " on stack, removing it." << endl;
-        found = true;
-        StackList[i] = NULL;
-      }
-      if ((found) && (StackList[i] != NULL)) {  // means we have to shift (and not the removed item)
-        if (i == 0) { // we are down to first item in stack, have to put onto last item
-          cout << Verbose(5) << "Shifting item 0 to place " << EntryCount-1 << "." << endl;
-          StackList[EntryCount-1] = StackList[0];
-        } else {
-          cout << Verbose(5) << "Shifting item " << i << " to place " << i-1 << "." << endl;
-          StackList[i-1] = StackList[i];
-        }
-      }
-      i=((i + 1) % EntryCount); // step on
-    } while (i!=NextFreeField);
-  else
-    cerr << "ERROR: Stack is already empty!" << endl;
-  if (found) {
-    NextFreeField = CurrentLastEntry;
-    if (CurrentLastEntry != CurrentFirstEntry)  // has there been more than one item on stack
-      CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount;
-  }
-  return found;
-};
-
-/** Test the functionality of the stack.
- * \param *out ofstream for debugging
- * \param *test one item to put on stack  
- * \return true - all tests worked correctly
- */
-void AtomStackClass::TestImplementation(ofstream *out, atom *test)
-{
-  atom *Walker = test;
-  *out << Verbose(1) << "Testing the snake stack..." << endl;
-  for (int i=0;i<EntryCount;i++) {
-    *out << Verbose(2) << "Filling " << i << "th element of stack." << endl; 
-    Push(Walker);
-    Walker=Walker->next;
-  }
-  *out << endl;
-  Output(out);
-  if (IsFull())
-    *out << "Stack is full as supposed to be!" << endl;
-  else
-    *out << "ERROR: Stack is not as full as supposed to be!" << endl;
-  //if (StackList[(EntryCount+1)/2] != NULL) {
-    *out << "Removing element in the middle ..." << endl;
-    RemoveItem(StackList[(EntryCount+1)/2]);
-    Output(out);
-  //}
-  //if (StackList[CurrentFirstEntry] != NULL) {
-    *out << "Removing first element  ..." << endl;
-    RemoveItem(StackList[CurrentFirstEntry]);
-    Output(out);
-  //}
-  //if (StackList[CurrentLastEntry] != NULL) {
-    *out << "Removing last element ..." << endl;
-    RemoveItem(StackList[CurrentLastEntry]);
-    Output(out);
-  //}
-  *out << "Clearing stack ... " << endl;  
-  ClearStack();
-  Output(out);
-  if (IsEmpty())
-    *out << "Stack is empty as supposed to be!" << endl;
-  else
-    *out << "ERROR: Stack is not as empty as supposed to be!" << endl;
-  *out << "done." << endl;
-};
-
-/** Puts contents of stack into ofstream \a *out.
- * \param *out ofstream for output
- */
-void AtomStackClass::Output(ofstream *out) const
-{
-  *out << "Contents of Stack: ";
-  for(int i=0;i<EntryCount;i++) {
-    *out << "\t";
-    if (i == CurrentFirstEntry)
-      *out << " 1";
-    if  (i == CurrentLastEntry)
-      *out << " "<< EntryCount;
-    if (i ==  NextFreeField)
-      *out << " F";
-    *out << ": " << StackList[i];
-  }
-  *out << endl;
-};
-
-/** Checks whether stack is empty.
- * Simply checks whether AtomStackClass::NextFreeField is equal to AtomStackClass::CurrentFirstEntry and
- * AtomStackClass::CurrentFirstEntry is equal to AtomStackClass::CurrentLastEntry.
- * \return true - empty, false - not
- */
-bool AtomStackClass::IsEmpty()
-{
-  return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry == CurrentFirstEntry));
-};
-
-/** Checks whether stack is full.
- * Simply checks whether AtomStackClass::NextFreeField is equal to AtomStackClass::CurrentFirstEntry and
- * AtomStackClass::CurrentFirstEntry is _not_ equal to AtomStackClass::CurrentLastEntry.
- * \return true - full, false - not
- */
-bool AtomStackClass::IsFull()
-{
-  return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry != CurrentFirstEntry));
-};
-
-/** Returns number of items on stack.
- * Simply returns difference between AtomStackClass::Stacklist entry AtomStackClass::CurrentEntry-1.
- * \return number of items on stack
- * \warning will never return correct item count if stack is full, i.e. count would be AtomStackClass::EntryCount.
- */
-int AtomStackClass::ItemCount()
-{
-  //cout << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tEntryCount " << EntryCount << "." << endl;
-  return((NextFreeField + (EntryCount - CurrentFirstEntry)) % EntryCount);
-};
-
-/** Clears the stack from all atoms.
- * \return true - sucess, false - failure
- */
-void AtomStackClass::ClearStack()
-{
-  for(int i=0;i<EntryCount; i++)
-    StackList[i] = NULL;
-  CurrentFirstEntry = 0;
-  CurrentLastEntry = 0;
-  NextFreeField = 0;
-};
-
Index: src/helpers.hpp
===================================================================
--- src/helpers.hpp	(revision db066d756f5b01d67a9bcb1ea28c9571452a3762)
+++ src/helpers.hpp	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
@@ -59,5 +59,159 @@
 char *FixedDigitNumber(const int FragmentNumber, const int digits);
 
-/********************************************** helpful structures *********************************/
+/********************************************** helpful template functions *********************************/
+
+/******************************** Some templates for list management ***********************************/
+
+/** Adds linking of an item to a list.
+ * \param *walker
+ * \return true - adding succeeded, false - error in list
+ */
+template <typename X> void link(X *walker, X *end)
+{
+  X *vorher = end->previous;
+  if (vorher != NULL)
+    vorher->next = walker;
+  end->previous = walker;
+  walker->previous = vorher;
+  walker->next = end;
+};
+
+/** Removes linking of an item in a list.
+ * \param *walker
+ * \return true - removing succeeded, false - given item not found in list
+ */
+template <typename X> void unlink(X *walker)
+{
+  if (walker->next != NULL) 
+    walker->next->previous = walker->previous;
+  if (walker->previous != NULL)
+    walker->previous->next = walker->next;
+};
+
+/** Adds new item before an item \a *end in a list.
+ * \param *pointer   item to be added
+ * \param *end  end of list
+ * \return true - addition succeeded, false - unable to add item to list
+ */
+template <typename X>  bool add(X *pointer, X *end)
+{
+  if (end != NULL) {
+    link(pointer, end);
+  } else {
+    pointer->previous = NULL;
+    pointer->next = NULL; 
+  }
+  return true;
+};
+
+/** Finds item in list
+ * \param *suche  search criteria
+ * \param *start  begin of list
+ * \param *end  end of list
+ * \return X - if found, NULL - if not found
+ */
+template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
+{
+  X *walker = start;
+  while (walker->next != end) { // go through list
+    walker = walker->next; // step onward beforehand
+    if (*walker->sort == *suche) return (walker);
+  }
+  return NULL;
+};
+
+/** Removes an item from the list without check.
+ * \param *walker item to be removed
+ * \return true - removing succeeded, false - given item not found in list
+ */
+template <typename X> void removewithoutcheck(X *walker)
+{
+  if (walker != NULL) {
+    unlink(walker);
+    delete(walker);
+    walker = NULL;
+  }
+};
+
+/** Removes an item from the list, checks if exists.
+ * Checks beforehand if atom is really within molecule list.
+ * \param *pointer   item to be removed
+ * \param *start  begin of list
+ * \param *end  end of list
+ * \return true - removing succeeded, false - given item not found in list
+ */
+template <typename X> bool remove(X *pointer, X *start, X *end)
+{
+  X *walker = find (pointer->sort, start, end);
+/*  while (walker->next != pointer) { // search through list
+    walker = walker->next;
+    if (walker == end) return false;  // item not found in list
+  }*/
+  // atom found, now unlink
+  if (walker != NULL)
+    removewithoutcheck(walker);
+  else
+    return false;
+  return true;
+};
+
+/** Cleans the whole list.
+ * \param *start begin of list
+ * \param *end end of list
+ * \return true - list was cleaned successfully, false - error in list structure
+ */
+template <typename X> bool cleanup(X *start, X *end)
+{
+  X *pointer = start->next;
+  X *walker;
+  while (pointer != end) { // go through list
+    walker = pointer; // mark current
+    pointer = pointer->next; // step onward beforehand
+    // remove walker
+    unlink(walker);
+    delete(walker);
+    walker = NULL;
+  }
+  return true;
+};
+
+/** Returns the first marker in a chain list.
+ * \param *me one arbitrary item in chain list
+ * \return poiner to first marker
+ */
+template <typename X> X *GetFirst(X *me)
+{
+  X *Binder = me;
+  while(Binder->previous != NULL)
+    Binder = Binder->previous;
+  return Binder;
+}; 
+
+/** Returns the last marker in a chain list.
+ * \param *me one arbitrary item in chain list
+ * \return poiner to last marker
+ */
+template <typename X> X *GetLast(X *me)
+{
+  X *Binder = me;
+  while(Binder->next != NULL)
+    Binder = Binder->next;
+  return Binder;
+}; 
+
+/** Frees a two-dimensional array.
+ * \param *ptr pointer to array
+ * \param dim first dim of array
+ */
+template <typename X> void Free2DArray(X **ptr, int dim)
+{
+  int i;
+  if (ptr != NULL) {
+    for(i=0;i<dim;i++)
+      if (ptr[i] != NULL)
+        free(ptr[i]);
+    free(ptr);
+  }
+};
 
 /************************************* Class Verbose & Binary *******************************/
Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision db066d756f5b01d67a9bcb1ea28c9571452a3762)
+++ src/molecules.cpp	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
@@ -1419,9 +1419,10 @@
 MoleculeLeafClass * molecule::DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int &MinimumRingSize)
 {
-  AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
+  class StackClass<atom *> *AtomStack;
+  AtomStack = new StackClass<atom *>(AtomCount);
+  class StackClass<bond *> *BackEdgeStack = new StackClass<bond *> (BondCount);
   MoleculeLeafClass *SubGraphs = new MoleculeLeafClass(NULL);
   MoleculeLeafClass *LeafWalker = SubGraphs;
   int CurrentGraphNr = 0, OldGraphNr;
-  int CyclicBonds;
   int ComponentNumber = 0;
   atom *Walker = NULL, *OtherAtom = NULL, *Root = start->next;
@@ -1434,4 +1435,5 @@
   ResetAllAtomNumbers();
   InitComponentNumbers();
+  BackEdgeStack->ClearStack();
   while (Root != end) { // if there any atoms at all
     // (1) mark all edges unused, empty stack, set atom->GraphNr = 0 for all
@@ -1468,4 +1470,5 @@
             // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3)
             Binder->Type = BackEdge;
+            BackEdgeStack->Push(Binder);
             Walker->LowpointNr = ( Walker->LowpointNr < OtherAtom->GraphNr ) ? Walker->LowpointNr : OtherAtom->GraphNr;
             *out << Verbose(3) << "(4a) Visited: Setting Lowpoint of Walker[" << Walker->Name << "] to " << Walker->LowpointNr << "." << endl;
@@ -1568,23 +1571,7 @@
   }
 
-  // correct cyclic bonds that are not included in "same LP" argument
-  Binder = first;
-  while (Binder->next != last) {
-    Binder = Binder->next;
-    Walker = Binder->leftatom;
-    OtherAtom = Binder->rightatom;
-    // now check whether both have a cyclic bond in their list
-    CyclicBonds = 0;   // counts cyclic bonds;
-    for(int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++)
-      if ((CyclicBonds == 0) && (ListOfBondsPerAtom[Walker->nr][i]->Cyclic))
-        CyclicBonds = 1;  
-    for(int i=0;i<NumberOfBondsPerAtom[OtherAtom->nr];i++)
-      if ((CyclicBonds == 1) && (ListOfBondsPerAtom[OtherAtom->nr][i]->Cyclic))
-        CyclicBonds = 2;  
-    Binder->Cyclic = (Binder->Cyclic) || (CyclicBonds == 2); // set the Cyclic criterium either or ... 
-  }
-  
-  // further analysis of the found cycles (print rings, get minimum cycle length)
+  // analysis of the cycles (print rings, get minimum cycle length)
   CyclicStructureAnalysis(out, MinimumRingSize);
+
   *out << Verbose(1) << "Final graph info for each atom is:" << endl;
   Walker = start;
@@ -1624,5 +1611,5 @@
 void molecule::CyclicStructureAnalysis(ofstream *out, int &MinimumRingSize)
 {
-  AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
+  class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
   int LP;
   atom *Walker = NULL, *OtherAtom = NULL, *Root = NULL, *Runner = NULL;
@@ -1630,5 +1617,5 @@
   int RingSize, NumCycles;
 
-  // go through every atom
+  // count number of cyclic bonds per atom and push all with 3 cyclic bonds onto stack
   AtomStack->ClearStack();
   int *NoCyclicBondsPerAtom = (int *) Malloc(sizeof(int)*AtomCount, "molecule::CyclicStructureAnalysis: *NoCyclicBondsPerAtom");
@@ -2065,5 +2052,5 @@
   line << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE;
   File.open(line.str().c_str(), ios::out);
-  *out << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... " << endl;
+  *out << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... ";
   if (File != NULL) {
     // allocate storage structure
@@ -2119,4 +2106,5 @@
     status = false;
   }
+  *out << endl;
   Free((void **)&buffer, "molecule::CheckAdjacencyFileAgainstMolecule: *buffer");
   
@@ -2620,5 +2608,5 @@
 
 /** Adds atoms up to \a BondCount distance from \a *Root and notes them down in \a **AddedAtomList.
- * Gray vertices are always enqueued in an AtomStackClass FIFO queue, the rest is usual BFS with adding vertices found was
+ * Gray vertices are always enqueued in an StackClass<atom *> FIFO queue, the rest is usual BFS with adding vertices found was
  * white and putting into queue. 
  * \param *out output stream for debugging
@@ -2636,5 +2624,5 @@
   int *ShortestPathList = (int *) Malloc(sizeof(int)*AtomCount, "molecule::BreadthFirstSearchAdd: *ShortestPathList");
   enum Shading *ColorList = (enum Shading *) Malloc(sizeof(enum Shading)*AtomCount, "molecule::BreadthFirstSearchAdd: *ColorList");
-  AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
+  class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
   atom *Walker = NULL, *OtherAtom = NULL;
   bond *Binder = NULL;
@@ -2802,5 +2790,5 @@
 
 
-/** Looks through a AtomStackClass and returns the likeliest removal candiate.
+/** Looks through a StackClass<atom *> and returns the likeliest removal candiate.
  * \param *out output stream for debugging messages
  * \param *&Leaf KeySet to look through
@@ -2922,7 +2910,7 @@
   enum Shading *ColorVertexList = (enum Shading *) Malloc(sizeof(enum Shading)*AtomCount, "molecule::CreateListOfUniqueFragmentsOfOrder: *ColorList");
   enum Shading *ColorEdgeList = (enum Shading *) Malloc(sizeof(enum Shading)*BondCount, "molecule::CreateListOfUniqueFragmentsOfOrder: *ColorBondList");
-  AtomStackClass *RootStack = new AtomStackClass(AtomCount);
-  AtomStackClass *TouchedStack = new AtomStackClass((int)pow(4,Order)+2); // number of atoms reached from one with maximal 4 bonds plus Root itself
-  AtomStackClass *SnakeStack = new AtomStackClass(Order+1); // equal to Order is not possible, as then the AtomStackClass cannot discern between full and empty stack!
+  StackClass<atom *> *RootStack = new StackClass<atom *>(AtomCount);
+  StackClass<atom *> *TouchedStack = new StackClass<atom *>((int)pow(4,Order)+2); // number of atoms reached from one with maximal 4 bonds plus Root itself
+  StackClass<atom *> *SnakeStack = new StackClass<atom *>(Order+1); // equal to Order is not possible, as then the StackClass<atom *> cannot discern between full and empty stack!
   MoleculeLeafClass *Leaflet = NULL, *TempLeaf = NULL; 
   MoleculeListClass *FragmentList = NULL;
@@ -3489,6 +3477,6 @@
   double tmp;
   vector TranslationVector;
-  //AtomStackClass *CompStack = NULL;
-  AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
+  //class StackClass<atom *> *CompStack = NULL;
+  class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
   bool flag = true;
 
Index: src/molecules.hpp
===================================================================
--- src/molecules.hpp	(revision db066d756f5b01d67a9bcb1ea28c9571452a3762)
+++ src/molecules.hpp	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
@@ -21,7 +21,7 @@
 
 #include "helpers.hpp"
+#include "stackclass.hpp"
 
 class atom;
-class AtomStackClass;
 class bond;
 class config;
@@ -46,163 +46,10 @@
   bool operator() (const KeySet SubgraphA, const KeySet SubgraphB) const;
 };
+
 //bool operator < (KeySet SubgraphA, KeySet SubgraphB);   //note: this declaration is important, otherwise normal < is used (producing wrong order)
 inline void InsertFragmentIntoGraph(ofstream *out, struct UniqueFragments *Fragment); // Insert a KeySet into a Graph
 inline void InsertGraphIntoGraph(ofstream *out, Graph &graph1, Graph &graph2, int *counter);  // Insert all KeySet's in a Graph into another Graph 
-
-/******************************** Some templates for list management ***********************************/
-
-/** Adds linking of an item to a list.
- * \param *walker
- * \return true - adding succeeded, false - error in list
- */
-template <typename X> void link(X *walker, X *end)
-{
-  X *vorher = end->previous;
-  if (vorher != NULL)
-    vorher->next = walker;
-  end->previous = walker;
-  walker->previous = vorher;
-  walker->next = end;
-};
-
-/** Removes linking of an item in a list.
- * \param *walker
- * \return true - removing succeeded, false - given item not found in list
- */
-template <typename X> void unlink(X *walker)
-{
-  if (walker->next != NULL) 
-    walker->next->previous = walker->previous;
-  if (walker->previous != NULL)
-    walker->previous->next = walker->next;
-};
-
-/** Adds new item before an item \a *end in a list.
- * \param *pointer   item to be added
- * \param *end  end of list
- * \return true - addition succeeded, false - unable to add item to list
- */
-template <typename X>  bool add(X *pointer, X *end)
-{
-  if (end != NULL) {
-    link(pointer, end);
-  } else {
-    pointer->previous = NULL;
-    pointer->next = NULL; 
-  }
-  return true;
-};
-
-/** Finds item in list
- * \param *suche  search criteria
- * \param *start  begin of list
- * \param *end  end of list
- * \return X - if found, NULL - if not found
- */
-template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
-{
-  X *walker = start;
-  while (walker->next != end) { // go through list
-    walker = walker->next; // step onward beforehand
-    if (*walker->sort == *suche) return (walker);
-  }
-  return NULL;
-};
-
-/** Removes an item from the list without check.
- * \param *walker item to be removed
- * \return true - removing succeeded, false - given item not found in list
- */
-template <typename X> void removewithoutcheck(X *walker)
-{
-  if (walker != NULL) {
-    unlink(walker);
-    delete(walker);
-    walker = NULL;
-  }
-};
-
-/** Removes an item from the list, checks if exists.
- * Checks beforehand if atom is really within molecule list.
- * \param *pointer   item to be removed
- * \param *start  begin of list
- * \param *end  end of list
- * \return true - removing succeeded, false - given item not found in list
- */
-template <typename X> bool remove(X *pointer, X *start, X *end)
-{
-  X *walker = find (pointer->sort, start, end);
-/*  while (walker->next != pointer) { // search through list
-    walker = walker->next;
-    if (walker == end) return false;  // item not found in list
-  }*/
-  // atom found, now unlink
-  if (walker != NULL)
-    removewithoutcheck(walker);
-  else
-    return false;
-  return true;
-};
-
-/** Cleans the whole list.
- * \param *start begin of list
- * \param *end end of list
- * \return true - list was cleaned successfully, false - error in list structure
- */
-template <typename X> bool cleanup(X *start, X *end)
-{
-  X *pointer = start->next;
-  X *walker;
-  while (pointer != end) { // go through list
-    walker = pointer; // mark current
-    pointer = pointer->next; // step onward beforehand
-    // remove walker
-    unlink(walker);
-    delete(walker);
-    walker = NULL;
-  }
-  return true;
-};
-
-/** Returns the first marker in a chain list.
- * \param *me one arbitrary item in chain list
- * \return poiner to first marker
- */
-template <typename X> X *GetFirst(X *me)
-{
-  X *Binder = me;
-  while(Binder->previous != NULL)
-    Binder = Binder->previous;
-  return Binder;
-}; 
-
-/** Returns the last marker in a chain list.
- * \param *me one arbitrary item in chain list
- * \return poiner to last marker
- */
-template <typename X> X *GetLast(X *me)
-{
-  X *Binder = me;
-  while(Binder->next != NULL)
-    Binder = Binder->next;
-  return Binder;
-}; 
-
-/** Frees a two-dimensional array.
- * \param *ptr pointer to array
- * \param dim first dim of array
- */
-template <typename X> void Free2DArray(X **ptr, int dim)
-{
-	int i;
- 	if (ptr != NULL) {
- 		for(i=0;i<dim;i++)
- 			if (ptr[i] != NULL)
- 				free(ptr[i]);
- 		free(ptr);
- 	}
-};
-
 int CompareDoubles (const void * a, const void * b);
+
 
 /************************************* Class definitions ****************************************/
@@ -370,32 +217,4 @@
 
 ostream & operator << (ostream &ost, atom &a);
-
-/* Stack of Atoms.
- * Is used during DepthFirstSearchAnalysis() to detect nonseparable components.
- */
-class AtomStackClass {
-  public:
-  AtomStackClass(int dimension);
-  ~AtomStackClass();
-  
-  bool Push(atom *object);
-  atom *PopFirst();
-  atom *PopLast();
-  bool AtomStackClass::RemoveItem(atom *ptr);
-  void ClearStack();
-  bool IsEmpty();
-  bool IsFull();
-	int ItemCount();
-  void Output(ofstream *out) const;
-  void TestImplementation(ofstream *out, atom *test);
-  
-  private:
-    atom **StackList;   //!< the list containing the atom pointers
-    int EntryCount;     //!< number of entries in the stack
-    int CurrentLastEntry;   //!< Current last entry (newest item on stack)
-    int CurrentFirstEntry;   //!< Current first entry (oldest item on stack)
-    int NextFreeField;       //!< Current index of next free field
-};
-
 
 /** Bonds between atoms.
Index: src/stackclass.hpp
===================================================================
--- src/stackclass.hpp	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
+++ src/stackclass.hpp	(revision 6d35e462d5c89a48ff8212fab4811f87fdab291e)
@@ -0,0 +1,262 @@
+#ifndef STACKCLASS_HPP_
+#define STACKCLASS_HPP_
+
+template <typename T> class StackClass;
+
+/******************************** Functions for class StackClass ********************************/
+
+/* Stack of Stuff.
+ * Is used during DepthFirstSearchAnalysis() to detect nonseparable components.
+ */
+template <typename T> class StackClass {
+  public:
+  StackClass<T>(int dimension);
+  ~StackClass<T>();
+  
+  bool Push(T object);
+  T PopFirst();
+  T PopLast();
+  bool RemoveItem(T ptr);
+  void ClearStack();
+  bool IsEmpty();
+  bool IsFull();
+  int ItemCount();
+  void Output(ofstream *out) const;
+  void TestImplementation(ofstream *out, T test);
+  
+  private:
+    T *StackList;   //!< the list containing the atom pointers
+    int EntryCount;     //!< number of entries in the stack
+    int CurrentLastEntry;   //!< Current last entry (newest item on stack)
+    int CurrentFirstEntry;   //!< Current first entry (oldest item on stack)
+    int NextFreeField;       //!< Current index of next free field
+};
+
+/** Constructor of class StackClass.
+ */
+template <typename T> StackClass<T>::StackClass(int dimension)
+{
+  CurrentLastEntry = 0;
+  CurrentFirstEntry = 0;
+  NextFreeField = 0;
+  EntryCount = dimension;
+  StackList = (T *) Malloc(sizeof(T)*EntryCount, "StackClass::StackClass: **StackList");
+};
+
+/** Destructor of class StackClass.
+ */
+template <typename T> StackClass<T>::~StackClass()
+{
+  Free((void **)&StackList, "StackClass::StackClass: **StackList");
+};
+
+/** Pushes an object onto the stack.
+ * \param *object atom to be pushed on stack
+ * \return true - sucess, false - failure, meaning stack field was occupied
+ */
+template <typename T> bool StackClass<T>::Push(T object)
+{
+  if (!IsFull()) {    // check whether free field is really not occupied
+    StackList[NextFreeField] = object;
+    CurrentLastEntry = NextFreeField;
+    NextFreeField = (NextFreeField + 1) % EntryCount; // step on to next free field
+    return true;
+  } else {
+    cerr << "ERROR: Stack is full, " << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tNextFreeField " << NextFreeField << "\tEntryCount " << EntryCount << "!" << endl;
+    return false;
+  }
+};
+
+/** Pops first/oldest atom from stack.
+ * First in, last out.
+ * \return atom pointer from stack, NULL - if failure (no atom pointer in field)
+ */
+template <typename T> T StackClass<T>::PopFirst()
+{
+  T Walker = NULL;
+  if (!IsEmpty()) {
+    Walker = StackList[CurrentFirstEntry];
+    if (Walker == NULL)
+      cerr << "ERROR: Stack's field is empty!" << endl;
+    StackList[CurrentFirstEntry] = NULL;
+    if (CurrentFirstEntry != CurrentLastEntry) { // hasn't last item been popped as well?
+      CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)
+    } else {
+      CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)
+      CurrentLastEntry = CurrentFirstEntry;
+    }
+  } else
+    cerr << "ERROR: Stack is empty!" << endl; 
+  return Walker;
+};
+
+/** Pops last element from stack.
+ * First in, first out.
+ * \return element pointer from stack, NULL - if failure (no atom pointer in field)
+ */
+template <typename T> T StackClass<T>::PopLast()
+{
+  T Walker = NULL;
+  if (!IsEmpty()) {
+    Walker = StackList[CurrentLastEntry];
+    StackList[CurrentLastEntry] = NULL;
+    if (Walker == NULL)
+      cerr << "ERROR: Stack's field is empty!" << endl;
+    NextFreeField = CurrentLastEntry;
+    if (CurrentLastEntry != CurrentFirstEntry)  // has there been more than one item on stack
+      CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; // step back from current free field to last (modulo does not work in -1, thus go EntryCount-1 instead)
+  } else {
+    cerr << "ERROR: Stack is empty!" << endl;
+  } 
+  return Walker;
+};
+
+/** Removes a certain item from the stack.
+ * Item is seeked between \a CurrentFirstEntry and \a CurrentLastEntry, if found, place in stack is NULL'd and
+ * all subsequent items shifted by one position downward (with wrapping taken into account).
+ * \param *ptr adress of item
+ * \return true - item was removed, false - item was not found
+ */
+template <typename T> bool StackClass<T>::RemoveItem(T ptr)
+{
+  bool found = false;
+  cout << Verbose(5) << "First " << CurrentFirstEntry<< "\tLast " << CurrentLastEntry<< "\tNext " << NextFreeField<< "\tCount " << EntryCount<< "." << endl;
+  int i=CurrentFirstEntry;
+  if (!IsEmpty())
+    do {
+      if (StackList[i] == ptr) {  // if item found, remove
+        cout << Verbose(5) << "Item " << *ptr << " was number " << i << " on stack, removing it." << endl;
+        found = true;
+        StackList[i] = NULL;
+      }
+      if ((found) && (StackList[i] != NULL)) {  // means we have to shift (and not the removed item)
+        if (i == 0) { // we are down to first item in stack, have to put onto last item
+          cout << Verbose(5) << "Shifting item 0 to place " << EntryCount-1 << "." << endl;
+          StackList[EntryCount-1] = StackList[0];
+        } else {
+          cout << Verbose(5) << "Shifting item " << i << " to place " << i-1 << "." << endl;
+          StackList[i-1] = StackList[i];
+        }
+      }
+      i=((i + 1) % EntryCount); // step on
+    } while (i!=NextFreeField);
+  else
+    cerr << "ERROR: Stack is already empty!" << endl;
+  if (found) {
+    NextFreeField = CurrentLastEntry;
+    if (CurrentLastEntry != CurrentFirstEntry)  // has there been more than one item on stack
+      CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount;
+  }
+  return found;
+};
+
+/** Test the functionality of the stack.
+ * \param *out ofstream for debugging
+ * \param *test one item to put on stack  
+ * \return true - all tests worked correctly
+ */
+template <typename T> void StackClass<T>::TestImplementation(ofstream *out, T test)
+{
+  T Walker = test;
+  *out << Verbose(1) << "Testing the snake stack..." << endl;
+  for (int i=0;i<EntryCount;i++) {
+    *out << Verbose(2) << "Filling " << i << "th element of stack." << endl; 
+    Push(Walker);
+    Walker=Walker->next;
+  }
+  *out << endl;
+  Output(out);
+  if (IsFull())
+    *out << "Stack is full as supposed to be!" << endl;
+  else
+    *out << "ERROR: Stack is not as full as supposed to be!" << endl;
+  //if (StackList[(EntryCount+1)/2] != NULL) {
+    *out << "Removing element in the middle ..." << endl;
+    RemoveItem(StackList[(EntryCount+1)/2]);
+    Output(out);
+  //}
+  //if (StackList[CurrentFirstEntry] != NULL) {
+    *out << "Removing first element  ..." << endl;
+    RemoveItem(StackList[CurrentFirstEntry]);
+    Output(out);
+  //}
+  //if (StackList[CurrentLastEntry] != NULL) {
+    *out << "Removing last element ..." << endl;
+    RemoveItem(StackList[CurrentLastEntry]);
+    Output(out);
+  //}
+  *out << "Clearing stack ... " << endl;  
+  ClearStack();
+  Output(out);
+  if (IsEmpty())
+    *out << "Stack is empty as supposed to be!" << endl;
+  else
+    *out << "ERROR: Stack is not as empty as supposed to be!" << endl;
+  *out << "done." << endl;
+};
+
+/** Puts contents of stack into ofstream \a *out.
+ * \param *out ofstream for output
+ */
+template <typename T> void StackClass<T>::Output(ofstream *out) const
+{
+  *out << "Contents of Stack: ";
+  for(int i=0;i<EntryCount;i++) {
+    *out << "\t";
+    if (i == CurrentFirstEntry)
+      *out << " 1";
+    if  (i == CurrentLastEntry)
+      *out << " "<< EntryCount;
+    if (i ==  NextFreeField)
+      *out << " F";
+    *out << ": " << StackList[i];
+  }
+  *out << endl;
+};
+
+/** Checks whether stack is empty.
+ * Simply checks whether StackClass::NextFreeField is equal to StackClass::CurrentFirstEntry and
+ * StackClass::CurrentFirstEntry is equal to StackClass::CurrentLastEntry.
+ * \return true - empty, false - not
+ */
+template <typename T> bool StackClass<T>::IsEmpty()
+{
+  return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry == CurrentFirstEntry));
+};
+
+/** Checks whether stack is full.
+ * Simply checks whether StackClass::NextFreeField is equal to StackClass::CurrentFirstEntry and
+ * StackClass::CurrentFirstEntry is _not_ equal to StackClass::CurrentLastEntry.
+ * \return true - full, false - not
+ */
+template <typename T> bool StackClass<T>::IsFull()
+{
+  return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry != CurrentFirstEntry));
+};
+
+/** Returns number of items on stack.
+ * Simply returns difference between StackClass::Stacklist entry StackClass::CurrentEntry-1.
+ * \return number of items on stack
+ * \warning will never return correct item count if stack is full, i.e. count would be StackClass::EntryCount.
+ */
+template <typename T> int StackClass<T>::ItemCount()
+{
+  //cout << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tEntryCount " << EntryCount << "." << endl;
+  return((NextFreeField + (EntryCount - CurrentFirstEntry)) % EntryCount);
+};
+
+/** Clears the stack from all atoms.
+ * \return true - sucess, false - failure
+ */
+template <typename T> void StackClass<T>::ClearStack()
+{
+  for(int i=0;i<EntryCount; i++)
+    StackList[i] = NULL;
+  CurrentFirstEntry = 0;
+  CurrentLastEntry = 0;
+  NextFreeField = 0;
+};
+
+
+
+#endif /*STACKCLASS_HPP_*/
