/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ #include #include typedef long int Element; int compare (Element e1, Element e2) { return (e1 >= e2) - (e1 <= e2); } #if LINKEDLIST #define SORTED 1 #define ITERATIVE 1 #include "List.c" /* rename functions */ #define Heap List #define mkEmptyHeap() mkEmptyList() #define mkOneElemHeap(e) cons(e,mkEmptyList()) #define isEmptyHeap(h) isEmptyList(h) #define minElem(h) head(h) #define deleteMinElem(h) removeHead(h) #define insertElem(e,h) addElem(e,h) #else #include "Heap.c" #endif #if TRACE #define trace(s) s #else #define trace(s) #endif static Heap addElems (unsigned n, Heap h); static Heap delElems (unsigned n, Heap h); int main (int argc, char *argv[]) { unsigned int card = 1000; /* max size of heap */ if (argc == 2) sscanf (argv[1], "%u", &card); trace ( fprintf (stderr, "run priority queue test with %u elements\n", card)); { Heap h = mkEmptyHeap (); h = addElems (card, h); h = delElems (card, h); trace ( fprintf (stderr, "test finished %s\n", isEmptyHeap (h) ? "successfully" : "with failure")); return !isEmptyHeap (h); } } Heap addElems (unsigned n, Heap h) { trace ( fprintf (stderr, "insert %u random elements\n", n)); while (n--) { Element e = random (); trace ( fprintf (stderr, "%12ld\n", e)); h = insertElem (e, h); } return h; } Heap delElems (unsigned n, Heap h) { trace ( fprintf (stderr, "delete %u elements\n", n)); while (n--) { trace ( fprintf (stderr, "%12ld\n", minElem (h))); h = deleteMinElem (h); } return h; }