/** * 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. */ /*--------------------*/ /* some macros and static variables for making version and conditional compilation flags visible in assembler and object files ignore in first reading */ /*--------------------*/ #include "List.h" #include #include #include /*--------------------*/ /* simple ops as functions */ List mkEmptyList(void) { return (List) 0; } /*--------------------*/ int isEmptyList(List l) { return l == (List) 0; } /*--------------------*/ Element head(List l) { assert(! isEmptyList(l)); return l->info; } /*--------------------*/ List tail(List l) { assert(! isEmptyList(l)); return l->next; } /*--------------------*/ List removeHead(List l) { assert(! isEmptyList(l)); { List res = l->next; free(l); return res; } } List cons(Element e, List l) { /* the only call of malloc */ List res = malloc(sizeof(*l)); if (! res) { perror("cons: malloc failed"); exit(1); } res->info = e; res->next = l; return res; } /*--------------------*/ List addElem(Element e, List l) { List *pl = &l; while (! isEmptyList(*pl) && ! geElement((*pl)->info, e)) { pl = &((*pl)->next); } *pl = cons(e, *pl); return l; } /*--------------------*/ /*consistency check */ int invList(List l) { if (isEmptyList(l)) return 1; if (isEmptyList(l->next)) return 1; return geElement(l->next->info, l->info) && invList(l->next); } /*--------------------*/