Index: kernel/f5gb.cc
===================================================================
--- kernel/f5gb.cc	(revision 5ec4a8bc9c1e8e23c5f5f95235a4e4839659b682)
+++ kernel/f5gb.cc	(revision a0350e9a597a2d719cdc7e27752cc38d0a6147b8)
@@ -2,5 +2,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: f5gb.cc,v 1.15 2008-12-27 13:50:05 ederc Exp $ */
+/* $Id: f5gb.cc,v 1.16 2009-01-15 17:44:23 ederc Exp $ */
 /*
 * ABSTRACT: f5gb interface
@@ -24,17 +24,4 @@
 #include "lpolynomial.h"
 #include "lists.h"
-
-
-/*
-================================================
-computation of ONE polynomial as global variable
-================================================
-*/
-poly one_poly() {
-    poly one = pInit();
-    pSetCoeff(one,nInit(1));
-    return one;
-}
-
 
 
@@ -80,12 +67,7 @@
 ==================================================
 */
-LList* F5inc(long* i, poly* f_i, LList* g_prev) {
-    poly one = pInit();
-    pSetCoeff(one, nInit(1));
-    static poly ONE = one;
-    //poly ONE = pOne();
+LList* F5inc(int* i, poly* f_i, LList* g_prev, poly* ONE) {
     LList* g_curr       =   g_prev;
-    LNode* prev_last    =   g_prev->getLast(); //tags the end of g_prev->only 1 list for g_prev & g_curr
-    g_curr->append(&ONE,i,f_i);
+    g_curr->insert(ONE,i,f_i);
    
     return g_curr;
@@ -98,8 +80,10 @@
 */
 ideal F5main(ideal id, ring r) {
-   
-    static poly ONE = pOne();
-    long i,j;
-
+    int i,j;
+    const int idElems = IDELEMS(id);
+    // 1 polynomial for defining initial labels & further tests
+    static poly ONE = pOne(); 
+    
+    
     // definition of one-polynomial as global constant ONE
     //poly one = pInit();
@@ -122,32 +106,8 @@
     idShow(id);
     i = 1;
-    //lp->setIndex(&i);
-    //lp->setTerm(&ONE);
-    //lp->setPoly(&id->m[0]);
-    LPoly* lp = new LPoly(&ONE,&i,&ONE);    
-    
     // only for debugging
-    long k = 2;
-    LList* g_prev = new LList(&ONE,&k,&id->m[2]);
-    LNode* current;
-    LPoly* lp2  = new LPoly(&ONE,&k,&ONE);    
+    //LNode* current;
     //LList* g_curr = new LList(lp);
-    k = 134;
-    g_prev->append(&ONE,&k,&id->m[3]);
-    g_prev->append(&ONE,&k,&id->m[3]);
-    g_prev->append(lp2);
-    g_prev->append(lp2);
-    g_prev->append(lp);
-    g_prev->append(&ONE,&k,&id->m[1]);
-    g_prev->append(&ONE,&k,&id->m[3]);
-    g_prev->append(lp2);
-    i = g_prev->getLength();
-    Print("%ld\n\n",i); 
-    current = g_prev->getFirst();
-    while(NULL != current) {
-    Print("Index: %ld\n",*(current->getLPoly()->getIndex()));
-    Print("Pointer comparison: %p , %p\n\n",g_prev->getFirst(),current);
-    current = current->getNext();
-    }
+    //}
     //for(i=2; i<IDELEMS(id); i++) {
         //g_curr = F5inc(&i,&id->m[i],g_prev);
Index: kernel/f5gb.h
===================================================================
--- kernel/f5gb.h	(revision 5ec4a8bc9c1e8e23c5f5f95235a4e4839659b682)
+++ kernel/f5gb.h	(revision a0350e9a597a2d719cdc7e27752cc38d0a6147b8)
@@ -2,5 +2,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: f5gb.h,v 1.14 2008-12-27 13:50:05 ederc Exp $ */
+/* $Id: f5gb.h,v 1.15 2009-01-15 17:44:23 ederc Exp $ */
 /*
 * ABSTRACT: f5gb interface
@@ -12,12 +12,4 @@
 #include "lpolynomial.h"
 #include "lists.h"
-
-
-/*
-================================================
-computation of ONE polynomial as global variable
-================================================
-*/
-poly one_poly();
 
 
@@ -45,5 +37,5 @@
 ==================================================
 */
-LList* F5inc(const long i, LList* g_prev);
+LList* F5inc(const int i, LList* g_prev);
 
 
Index: kernel/lists.cc
===================================================================
--- kernel/lists.cc	(revision 5ec4a8bc9c1e8e23c5f5f95235a4e4839659b682)
+++ kernel/lists.cc	(revision a0350e9a597a2d719cdc7e27752cc38d0a6147b8)
@@ -25,5 +25,5 @@
 */
 
-// generating new list elements in the labeled / classical polynomial / LNode view
+// generating new list elements (labeled / classical polynomial / LNode view)
 LNode::LNode(LPoly* lp) {
     data = lp;
@@ -31,8 +31,8 @@
 }
        
-LNode::LNode(poly* t, long* i, poly* p) {
+LNode::LNode(poly* t, int* i, poly* p) {
     LPoly* lp = new LPoly(t,i,p);
     data = lp;
-    Print("Index angelegt?  %ld\n",*(data->getIndex()));
+    Print("Index angelegt?  %d\n",*(data->getIndex()));
     next = NULL;
 }
@@ -48,15 +48,15 @@
 }
        
-// append new elements to the list from the labeled / classical polynomial view
-LNode* LNode::append(LPoly* lp) {
-    LNode* new_element = new LNode(lp);
-    next = new_element;
-    return new_element;
+// insert new elements to the list always in front (labeled / classical polynomial view)
+LNode* LNode::insert(LPoly* lp) {
+    LNode* newElement = new LNode(lp);
+    newElement->next = this;
+    return newElement;
 }
         
-LNode* LNode::append(poly* t, long* i, poly* p) {
-    LNode* new_element = new LNode(t,i,p);
-    next = new_element;
-    return new_element;
+LNode* LNode::insert(poly* t, int* i, poly* p) {
+    LNode* newElement = new LNode(t,i,p);
+    newElement->next = this;
+    return newElement;
 }
         
@@ -77,5 +77,5 @@
 
 // test if for any list element the polynomial part of the data is equal to *p
-bool LNode:polyTest(poly* p) {
+bool LNode::polyTest(poly* p) {
     LNode* temp = new LNode(this);
     while(NULL != temp) {
@@ -88,10 +88,4 @@
 }
 
-LNode* LNode::operator++() {
-    LNode* temp= new LNode(this);
-    return temp->getNext();
-}
-
-
 
 /*
@@ -103,11 +97,9 @@
 LList::LList(LPoly* lp) {
     first   = new LNode(lp);
-    last    = first;
     length  = 1;
 }
 
-LList::LList(poly* t,long* i,poly* p) {
+LList::LList(poly* t,int* i,poly* p) {
     first   = new LNode(t,i,p);
-    last    = first;
     length  = 1;
 } 
@@ -117,11 +109,12 @@
 }
 
-void LList::append(LPoly* lp) {
-    last = last->append(lp);
+// insertion in front of the list
+void LList::insert(LPoly* lp) {
+    first = first->insert(lp);
     length++;
 }
 
-void LList::append(poly* t,long* i, poly* p) {
-    last = last->append(t,i,p);
+void LList::insert(poly* t,int* i, poly* p) {
+    first = first->insert(t,i,p);
     length++;
 }
@@ -131,5 +124,5 @@
 }
 
-long LList::getLength() const {
+int LList::getLength() const {
     return length;
 }
@@ -139,6 +132,194 @@
 }
 
-LNode* LList::getLast() {
-    return last;
+
+/*
+=======================================
+functions working on the class PrevNode
+=======================================
+*/
+
+PrevNode::PrevNode(LNode* l) {
+    data = l;
+    next = NULL;
+}
+       
+PrevNode::~PrevNode() {
+    delete next;
+    delete data;   
+}
+       
+PrevNode* PrevNode::append(LNode* l) {
+    PrevNode* new_element = new PrevNode(l);
+    next = new_element;
+    return new_element;
+}
+
+LNode* PrevNode::getLNode() {
+    return this->data;
+}
+
+LNode* PrevNode::getPrevLast(int i) {
+    int j;
+    PrevNode* temp = this;
+    for(j=1;j<=i-1;j++) {
+        temp = temp->next;
+    }
+    return temp->data;
+}
+
+/*
+=======================================
+functions working on the class PrevList
+=======================================
+*/
+
+PrevList::PrevList(LNode* l) {
+    PrevNode* first =   new PrevNode(l);
+    last            =   first;    
+}
+
+void PrevList::append(LNode* l) {
+    last = last->append(l);
+}
+
+LNode* PrevList::getPrevLast(int i) {
+    switch(i) {
+        case 0:     return NULL;
+        case 1:     return first->getLNode();
+        default:    first->getPrevLast(i);
+    }
+}
+
+/*
+====================================
+functions working on the class CNode
+====================================
+*/
+
+CNode::CNode(CPair* c) {
+    data    =   c;   
+    next    =   NULL;    
+}
+
+CNode::~CNode() {
+    delete next;
+    delete data;
+}
+
+// insert sorts the critical pairs firstly by increasing total degree, secondly by increasing label
+// note: as all critical pairs have the same index here, the second sort is done on the terms of the labels
+// working only with linked, but not doubly linked lists due to memory usage we have to check the 
+// insertion around the first element separately from the insertion around all other elements in the list
+CNode* CNode::insert(CPair* c) {
+    if( c->getDeg() < this->data->getDeg() ) { // lower degree than the first list element
+        CNode* newElement = new CNode(c);
+        newElement->next = this;
+        return newElement;
+    }
+    if( c->getDeg() == this->data->getDeg() ) { // same degree than the first list element
+        if( c->getT1() <= this->data->getT1() ) {
+            CNode* newElement   =   new CNode(c);
+            newElement->next    =   this;
+            return newElement;
+        }
+        else {
+            CNode* temp = this;
+            while(  temp->next != NULL ) {
+                if( temp->next->data->getDeg() == c->getDeg() ) { 
+                    if( c->getT1() <= temp->next->data->getT1() ) {
+                        temp = temp->next;
+                    }
+                    else {
+                        CNode* newElement   =   new CNode(c);
+                        newElement->next    =   temp->next;
+                        temp->next          =   newElement;
+                        return this;
+                    } 
+                }
+                else {
+                    CNode* newElement   =   new CNode(c);
+                    newElement->next    =   temp->next;
+                    temp->next          =   newElement;
+                    return this;
+                }
+            }
+            CNode* newElement   =   new CNode(c);
+            newElement->next    =   NULL;
+            temp->next          =   newElement;
+            return this;
+        }
+    } // outer if-clause
+    if( c->getDeg() > this->data->getDeg() ) { // greater degree than the first list element
+        CNode* temp =   this;
+        while( temp->next != NULL) {   
+            if( c->getDeg() < temp->next->data->getDeg() ) {
+                CNode* newElement   =   new CNode(c);
+                newElement->next    =   temp->next;
+                temp->next          =   newElement;
+                return this;
+            }
+            if( c->getDeg() == temp->next->data->getDeg() ) {
+                if( c->getT1() <= temp->next->data->getT1() ) {
+                    CNode* newElement   =   new CNode(c);
+                    newElement->next    =   temp->next;
+                    temp->next          =   newElement;
+                    return this;
+                }
+                else {
+                    temp = temp->next;
+                    while(  temp->next != NULL ) {
+                        if( temp->next->data->getDeg() == c->getDeg() ) { 
+                            if( c->getT1() <= temp->next->data->getT1() ) {
+                                temp = temp->next;
+                            }
+                            else {
+                                CNode* newElement   =   new CNode(c);
+                                newElement->next    =   temp->next;
+                                temp->next          =   newElement;
+                                return this;
+                            } 
+                        }
+                        else {
+                            CNode* newElement   =   new CNode(c);
+                            newElement->next    =   temp->next;
+                            temp->next          =   newElement;
+                            return this;
+                        }
+                    }
+                    CNode* newElement   =   new CNode(c);
+                    newElement->next    =   NULL;
+                    temp->next          =   newElement;
+                    return this;
+                }
+            }
+            if( c->getDeg() > temp->next->data->getDeg() ) {
+                temp    =   temp->next;
+            }
+        }
+        CNode* newElement   =   new CNode(c);
+        newElement->next    =   NULL;
+        temp->next          =   newElement;
+        return this;
+    }
+}
+
+/*
+====================================
+functions working on the class CList
+====================================
+*/
+
+CList::CList(CPair* c) {
+    first   = new CNode(c);
+}
+
+CList::~CList() {
+    delete first;
+}
+
+// insert sorts the critical pairs firstly by increasing total degree, secondly by increasing label
+// note: as all critical pairs have the same index here, the second sort is done on the terms of the labels
+void CList::insert(CPair* c) {
+    first = first->insert(c);
 }
 #endif
Index: kernel/lists.h
===================================================================
--- kernel/lists.h	(revision 5ec4a8bc9c1e8e23c5f5f95235a4e4839659b682)
+++ kernel/lists.h	(revision a0350e9a597a2d719cdc7e27752cc38d0a6147b8)
@@ -2,5 +2,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: lists.h,v 1.2 2008-12-27 13:50:05 ederc Exp $ */
+/* $Id: lists.h,v 1.3 2009-01-15 17:44:24 ederc Exp $ */
 /*
 * ABSTRACT: list interface
@@ -11,22 +11,17 @@
 
 #ifdef HAVE_F5
-
-
 /*
-=======================
-=======================
-linked lists for LPolys
-=======================
-=======================
-*/
-
-
-/*
-===========================
-classes for lists of LPolys 
-===========================
+============================
+============================
+classes for lists used in F5 
+============================
+============================
 */
 class LNode;
 class LList;
+class PrevNode;
+class PrevList;
+class CNode;
+class CList;
 
 
@@ -43,10 +38,10 @@
         // generating new list elements from the labeled / classical polynomial view
                 LNode(LPoly* lp);
-                LNode(poly* t, long* i, poly* p);
+                LNode(poly* t, int* i, poly* p);
                 LNode(LNode* ln);
                 ~LNode();
         // append new elements to the list from the labeled / classical polynomial view
-        LNode*  append(LPoly* lp);
-        LNode*  append(poly* t, long* i, poly* p);
+        LNode*  insert(LPoly* lp);
+        LNode*  insert(poly* t, int* i, poly* p);
         // get next from current LNode
         LNode*  getNext();
@@ -58,5 +53,4 @@
         // test if for any list element the polynomial part of the data is equal to *p
         bool    polyTest(poly* p);
-        LNode*  operator++();
 };
 
@@ -70,35 +64,51 @@
     private:
         LNode*  first;
-        LNode*  last;
-        long    length;
+        int     length;
     public:
                 LList(LPoly* lp);
-                LList(poly* t,long* i,poly* p);
+                LList(poly* t,int* i,poly* p);
                 ~LList();
-        void    append(LPoly* lp);
-        void    append(poly* t,long* i, poly* p);
+        void    insert(LPoly* lp);
+        // insertion in front of the list
+        void    insert(poly* t,int* i, poly* p);
         bool    polyTest(poly* p);
-        long    getLength() const;
+        int     getLength() const;
         LNode*  getFirst();
-        LNode*  getLast();
 };
 
 
 /*
-=======================
-=======================
-linked lists for CPairs 
-=======================
-=======================
+=============================================
+PrevNode class (nodes for lists of gPrevLast)
+=============================================
 */
+class PrevNode {
+    private:
+        LNode*      data;
+        PrevNode*   next;
+    public:
+        PrevNode(LNode* l);
+        ~PrevNode();
+        PrevNode*   append(LNode* l);
+        LNode*      getLNode();
+        LNode*      getPrevLast(int i);
+};
 
 
 /*
-===========================
-classes for lists of CPairs 
-===========================
+====================================================
+class PrevList(lists of last node elements in gPrev)
+====================================================
 */
-class CNode;
-class CList;
+class PrevList {
+    private:
+        PrevNode*   first;
+        PrevNode*   last;
+    public:
+                PrevList(LNode* l);
+                ~PrevList();
+        void    append(LNode* l);
+        LNode*  getPrevLast(int i);
+};
 
 
@@ -110,23 +120,11 @@
 class CNode {
     private:
-        LPoly* data;
+        CPair* data;
         CNode* next;
     public:
-        CNode(LPoly* lp, CNode* n) {
-            data = lp;
-            next = n;
-        }
-        ~CNode() {
-            delete next;
-            delete data;   
-        }
-        CNode* append(LPoly* lp) {
-            CNode* new_element = new CNode(lp,NULL);
-            next = new_element;
-            return new_element;
-        }
-        LPoly* getLPoly() const {
-            return data;
-        }
+                CNode(CPair* c);
+                ~CNode(); 
+        CNode*  insert(CPair* c); 
+        CNode*  getLPoly() const; 
 };
 
@@ -140,28 +138,8 @@
     private:
         CNode*  first;
-        CNode*  last;
-        long    length;
     public:
-        CList(LPoly* lp) {
-            first = new CNode(lp,NULL);
-            last = first;
-            length = 1;
-        }
-        ~CList() {
-            delete first;
-        }
-        void append(LPoly* lp) {
-            last = last->append(lp);
-            length++;
-        }
-        long getLength() const {
-            return length;
-        }
-        CNode* getFirst() const {
-            return first;
-        }
-        CNode* getLast() const {
-            return last;
-        }
+                CList(CPair* c); 
+                ~CList(); 
+        void    insert(CPair* c); 
 };
 #endif
Index: kernel/lpolynomial.cc
===================================================================
--- kernel/lpolynomial.cc	(revision 5ec4a8bc9c1e8e23c5f5f95235a4e4839659b682)
+++ kernel/lpolynomial.cc	(revision a0350e9a597a2d719cdc7e27752cc38d0a6147b8)
@@ -2,5 +2,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: lpolynomial.cc,v 1.4 2008-12-27 13:50:06 ederc Exp $ */
+/* $Id: lpolynomial.cc,v 1.5 2009-01-15 17:44:24 ederc Exp $ */
 /*
 * ABSTRACT: lpolynomial definition 
@@ -30,7 +30,8 @@
 ================================================================
 */
-LPoly::LPoly(poly* t,long* i,poly* p) {
+LPoly::LPoly(poly* t,int* i,poly* p) {
     set(t,i,p);
 }
+
 void LPoly::setPoly(poly* p)  {
     polynomial = *p;
@@ -41,8 +42,7 @@
 }
 
-void LPoly::setIndex(long* i) {
+void LPoly::setIndex(int* i) {
     index = *i;
 }
-
 
 void LPoly::setDel(bool b) {
@@ -58,5 +58,5 @@
 }
 
-long* LPoly::getIndex() {
+int* LPoly::getIndex() {
     return &index;
 }
@@ -66,5 +66,5 @@
 }
 
-void LPoly::set(poly* t, long* i, poly* p) {
+void LPoly::set(poly* t, int* i, poly* p) {
     this->setTerm(t);
     this->setIndex(i);
@@ -75,3 +75,55 @@
     return this;
 }
+
+/*
+====================================
+functions working on the class CPair
+====================================
+*/
+CPair::CPair(int degree, poly term1, LPoly* lpoly1, poly term2, LPoly* lpoly2) {
+   deg  =   degree;
+   t1   =   term1;
+   lp1  =   lpoly1;
+   t2   =   term2;
+   lp2  =   lpoly2;
+}
+
+int CPair::getDeg() {
+    return deg;
+}
+
+poly CPair::getT1() {
+    return t1;
+}
+
+poly CPair::getT2() {
+    return t2;
+}
+
+poly CPair::getLp1Poly() {
+    return *(lp1->getPoly());
+}
+
+poly CPair::getLp2Poly() {
+    return *(lp2->getPoly());
+}
+
+poly CPair::getLp1Term() {
+    return *(lp1->getTerm());
+}
+
+poly CPair::getLp2Term() {
+    return *(lp2->getTerm());
+}
+
+int CPair::getLp1Index() {
+    return *(lp1->getIndex());
+}
+
+
+int CPair::getLp2Index() {
+    return *(lp2->getIndex());
+}
+
+
 #endif
Index: kernel/lpolynomial.h
===================================================================
--- kernel/lpolynomial.h	(revision 5ec4a8bc9c1e8e23c5f5f95235a4e4839659b682)
+++ kernel/lpolynomial.h	(revision a0350e9a597a2d719cdc7e27752cc38d0a6147b8)
@@ -2,5 +2,5 @@
 *  Computer Algebra System SINGULAR     *
 ****************************************/
-/* $Id: lpolynomial.h,v 1.4 2008-12-27 13:50:06 ederc Exp $ */
+/* $Id: lpolynomial.h,v 1.5 2009-01-15 17:44:24 ederc Exp $ */
 /*
 * ABSTRACT: labeled polynomial interface
@@ -10,4 +10,13 @@
 
 #ifdef HAVE_F5
+/*
+=========================================================
+=========================================================
+classes for labeled polynomials/pairs/S-polynomials in F5
+=========================================================
+=========================================================
+*/
+class LPoly;
+class CPair;
 
 
@@ -20,18 +29,18 @@
     private:
         poly    term; //term of signature
-        long    index; //index of signature
+        int     index; //index of signature
         poly    polynomial; //standard polynomial data
         bool    del; //for deletion in TopReduction Subalgorithm
     public:
-                LPoly(poly*t,long* i,poly* p); 
+                LPoly(poly*t,int* i,poly* p); 
         void    setPoly(poly* p);
         poly*   getPoly();
         void    setTerm(poly* t);
         poly*   getTerm();
-        void    setIndex(long* i);
-        long*   getIndex();
+        void    setIndex(int* i);
+        int*    getIndex();
         void    setDel(bool b);
         bool    getDel() const;
-        void    set(poly* t, long* i, poly* p);
+        void    set(poly* t, int* i, poly* p);
         LPoly*  get();
 };
@@ -43,7 +52,22 @@
 ===============================
 */
-struct CPair {
-    LPoly*  cp1;   // first  component
-    LPoly*  cp2;   // second component
+class CPair {
+    private:
+        int     deg;    // total degree of the critical pair
+        poly    t1;     // first term for label
+        LPoly*  lp1;     // first labeled poly
+        poly    t2;     // second term for label
+        LPoly*  lp2;     // second labeled poly
+    public:
+                CPair(int degree, poly term1, LPoly* lpoly1, poly term2, LPoly* lpoly2);
+        int     getDeg();
+        poly    getT1();
+        poly    getLp1Poly();
+        poly    getLp1Term();
+        int     getLp1Index();
+        poly    getT2();
+        poly    getLp2Poly();
+        poly    getLp2Term();
+        int     getLp2Index();
 };
 
