Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* list testing program
- * Dwight Makaroff:
- * modified Sep 24, 2010
- * modified October 2, 2013
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "list.h"
- int dblcomp(void *a, void *b)
- {
- return ( *(double*) a == *(double *) b);
- }
- void PrintDblList(LIST *L)
- {
- int i = 0;
- /* REAL HACK */
- void *p;
- void *saveItem = ListCurr(L);
- p = ListFirst(L);
- while (p && i< ListCount(L)){
- printf ("List Item is %f\n", * (double *) p);
- p = ListNext(L);
- i++;
- }
- /* If multiple identical items, like integers are in the list,
- one would have to do some sort of counting to know which
- item in the list we were at when PrintIntList was called.
- I'm thinking that you could do a traverse to the end before
- printing and then after printing, move k steps back from the end.
- */
- /* this works for now */
- p = ListFirst(L);
- while (saveItem != ListCurr(L))
- p = ListNext(L);
- return;
- }
- void PrintDblItem(void *i)
- {
- printf("PI: %f\n", * (double*) i);
- }
- void PrintIntItem(void *i)
- {
- printf("PI: %ld\n", (long) i);
- }
- void myItemFree(void *a)
- {
- return;
- }
- void myDblItemFree(void *a)
- {
- free (a);
- return;
- }
- void doubleListTesting(void)
- {
- void *myItem;
- double *b, *c, *a, *z;
- LIST *L1,*L2;
- int rv, counter;
- b = (double *) malloc (sizeof(double));
- *b = 22.2;
- a = (double *) malloc (sizeof(double));
- *a = 1;
- z = (double *) malloc (sizeof(double));
- *z = 99.2;
- c = (double *) malloc (sizeof(double));
- *c = 420;
- L1 = ListCreate();
- counter = ListCount(L1);
- myItem = ListLast(L1);
- myItem = ListFirst(L1);
- if (myItem == NULL)
- {
- printf("item is null on empty list\n");
- }
- myItem = ListNext(L1);
- if (myItem == NULL)
- {
- printf("trying to go to past the end of list \n");
- }
- myItem = ListPrev(L1);
- if (myItem == NULL)
- {
- printf("trying to go to before the beginning of list \n");
- }
- rv = ListAppend(L1, (void *)a);
- if (rv == -1)
- {
- printf("trying to append failed \n");
- }
- else
- {
- printf("appended %f to the list \n", *a);
- }
- myItem = ListPrev(L1);
- if (myItem == NULL)
- {
- printf("trying to go to before the beginning of the end of list \n");
- }
- rv = ListPrepend(L1, (void *) b);
- if (rv != 0)
- {
- printf("trying to prepend failed\n");
- }
- else
- {
- printf("prepended %f to the list \n", *b);
- }
- if (rv == 0) PrintDblList(L1);
- myItem = ListFirst(L1);
- if (myItem)
- printf("at beginning of list\n");
- else
- {
- perror("ListFirst");
- }
- rv = ListAdd(L1, (void *) z);
- if (rv !=0)
- {
- printf("trying to add failed\n");
- }
- else
- {
- printf("added %f to the list \n", *z);
- }
- rv = ListInsert(L1, (void *)c);
- if (rv != 0)
- {
- printf("trying to insert failed \n");
- }
- else
- {
- printf("insert %f to the list \n", *c);
- }
- if (rv == 0)
- {
- PrintDblList(L1);
- printf("list should be: %f %f %f %f\n ", *b, *c, *z, *a);
- }
- myItem = ListFirst(L1);
- L2 = ListCreate();
- ListConcat(L1,L2);
- myItem = ListSearch(L1, dblcomp, (void *)z);
- myItem = ListTrim(L1);
- myItem = ListRemove(L1);
- ListFree(L1,myItemFree);
- printf("DONE WITH ptr to DOUBLE TESTING\n\n");
- }
- int main (void)
- {
- doubleListTesting();
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement