Added ability to maintain ordered linked list based on "insertAsc" function. The function takes a comparator function that allows for specific order behavior. If values collide, then FIFO or LIFO order can be maintained based on comparator function implementation.

Dependents:   JobScheduler

Fork of LinkedList by Sam Grove

Files at this revision

API Documentation at this revision

Comitter:
sgnezdov
Date:
Mon Jul 10 16:46:46 2017 +0000
Parent:
10:cb2e50ed6945
Child:
12:ab8a80bcfd64
Commit message:
tested descending order

Changed in this revision

LinkedList.cpp Show annotated file Show diff for this revision Revisions of this file
LinkedList.h Show annotated file Show diff for this revision Revisions of this file
--- a/LinkedList.cpp	Fri Jul 07 23:36:03 2017 +0000
+++ b/LinkedList.cpp	Mon Jul 10 16:46:46 2017 +0000
@@ -59,7 +59,7 @@
 }
 
 template<class retT>
-retT *LinkedList<retT>::insertAsc(void *data, bool (isBigger)(void* data1, void *data2))
+retT *LinkedList<retT>::insertOrdered(void *data, bool (isBigger)(void* data1, void *data2))
 {
     retT *new_node = new retT [1];
     // make sure the new object was allocated
--- a/LinkedList.h	Fri Jul 07 23:36:03 2017 +0000
+++ b/LinkedList.h	Mon Jul 10 16:46:46 2017 +0000
@@ -68,10 +68,10 @@
  * @endcode
  */
  
- /** Example using new insertAsc function:
+ /** Example using new insertOrdered function:
  * @code
  
- #include "mbed.h"
+#include "mbed.h"
 #include "LinkedList.h"
 
 bool ascending(void *n1, void *n2)
@@ -83,6 +83,15 @@
     return rv;
 }
 
+bool descending(void *n1, void *n2)
+{
+    int *d1 = (int*)n1;
+    int *d2 = (int*)n2;
+    bool rv = *d1 <= *d2;
+    printf("(%d %d:%d)", *d1, *d2, rv);
+    return rv;
+}
+
 void printList(LinkedList<node> &list, const char* msg) 
 {
     printf("%s: ", msg);
@@ -94,10 +103,8 @@
     printf("\n");
 }
 
-int main()
+void testAscending()
 {
-    printf("\nJob Scheduler Demo\n");
-    
     node *tmp;
     
     int n0 = 0;
@@ -109,39 +116,39 @@
     
     LinkedList<node>list;
     
-    tmp = list.insertAsc(&n2, ascending);
+    tmp = list.insertOrdered(&n2, ascending);
     if (NULL == tmp) {
-        error("insertAsc did not insert a node");
+        error("insertOrdered did not insert a node");
     }
     printList(list, "exp 2");
     
-    tmp = list.insertAsc(&n1, ascending);
+    tmp = list.insertOrdered(&n1, ascending);
     if (NULL == tmp) {
-        error("insertAsc did not insert a node");
+        error("insertOrdered did not insert a node");
     }
     printList(list, "exp 1,2");
 
-    tmp = list.insertAsc(&n4, ascending);
+    tmp = list.insertOrdered(&n4, ascending);
     if (NULL == tmp) {
-        error("insertAsc did not insert a node");
+        error("insertOrdered did not insert a node");
     }
     printList(list, "exp 1,2,4");
 
-    tmp = list.insertAsc(&n3, ascending);
+    tmp = list.insertOrdered(&n3, ascending);
     if (NULL == tmp) {
-        error("insertAsc did not insert a node");
+        error("insertOrdered did not insert a node");
     }
     printList(list, "exp 1,2,3,4");
 
-    tmp = list.insertAsc(&n0, ascending);
+    tmp = list.insertOrdered(&n0, ascending);
     if (NULL == tmp) {
-        error("insertAsc did not insert a node");
+        error("insertOrdered did not insert a node");
     }
     printList(list, "exp 0,1,2,3,4");
 
-    tmp = list.insertAsc(&n1B, ascending);
+    tmp = list.insertOrdered(&n1B, ascending);
     if (NULL == tmp) {
-        error("insertAsc did not insert a node");
+        error("insertOrdered did not insert a node");
     }
     printList(list, "exp 0,1,1,2,3,4");
     
@@ -156,12 +163,84 @@
         error("pos3 is not n1B");
     }
     printf("n1B is good\n");
+}
 
+void testDescending()
+{
+    node *tmp;
+    
+    int n0 = 0;
+    int n1 = 1;
+    int n1B = 1;
+    int n2 = 2;
+    int n3 = 3;
+    int n4 = 4;
+    
+    LinkedList<node>l;
+    
+    tmp = l.insertOrdered(&n2, descending);
+    if (NULL == tmp) {
+        error("insertOrdered did not insert a node");
+    }
+    printList(l, "exp 2");
+    
+    tmp = l.insertOrdered(&n1, descending);
+    if (NULL == tmp) {
+        error("insertOrdered did not insert a node");
+    }
+    printList(l, "exp 2,1");
+
+    tmp = l.insertOrdered(&n4, descending);
+    if (NULL == tmp) {
+        error("insertOrdered did not insert a node");
+    }
+    printList(l, "exp 4,2,1");
+
+    tmp = l.insertOrdered(&n3, descending);
+    if (NULL == tmp) {
+        error("insertOrdered did not insert a node");
+    }
+    printList(l, "exp 4,3,2,1");
+
+    tmp = l.insertOrdered(&n0, descending);
+    if (NULL == tmp) {
+        error("insertOrdered did not insert a node");
+    }
+    printList(l, "exp 4,3,2,1,0");
+
+    tmp = l.insertOrdered(&n1B, descending);
+    if (NULL == tmp) {
+        error("insertOrdered did not insert a node");
+    }
+    printList(l, "exp 4,3,2,1,1,0");
+    
+    tmp = l.pop(4);
+    if (tmp->data != &n1) {
+        error("pos 2 is not n1\n");
+    }
+    printf("n1 is good\n");
+    
+    tmp = l.pop(5);
+    if (tmp->data != &n1B) {
+        error("pos3 is not n1B");
+    }
+    printf("n1B is good\n");
+}
+
+
+int main()
+{
+    printf("\nJob Scheduler Demo\n");
+
+    testDescending();
+    
     error("done\n");
  
     exit(0);
 }
 
+
+
  * @endcode
  */
 
@@ -198,13 +277,20 @@
     * This will preserve ascending order of data.
     * @param data - some data type that is added to the list
     * @param isBigger - comparator function returns true if data1 is bigger
-    * than data2 and false otherwise.  If data1 equals data2, then ">" comparision
+    * than data2 and false otherwise.  
+    * If data1 equals data2, then ">" comparision
     * will result in LIFO order.  Using ">=" operator in the function
     * results in "FIFO" order and thus it preserves order in which items
     * were added to the list.
+    *
+    * If isBigger function is implemented with data1 < data2, then
+    * result insert will be in descending order.  If data1 equals data2, then
+    * LIFO order is established.
+    * If data1 <= data2, then FIFO order is preserved when data1 equals data2.  
+    *
     * @return The member that was just inserted (NULL if not inserted).
     */
-    retT *insertAsc(void *data, bool (isBigger)(void* data1, void *data2));
+    retT *insertOrdered(void *data, bool (isBigger)(void* data1, void *data2));
     
     /** Add a member to the end of the list
      *  @param data - Some data type that is added to the list