/* File: stringitem.C string operations needed for ListNode and List Classes. */ #include #include #include #include "stringitem.h" ListItem::ListItem() { /* default constructor */ item = NULL ; } ListItem::ListItem(data x) { /* alternate constructor */ if (x == NULL) return ; item = strdup(x) ; if (item == NULL) { fprintf(stderr, "could not duplicate string\n") ; exit(1) ; } } ListItem::~ListItem() { /* destructor */ /* Debugging: printf("ListItem Destructor: (%p, %s)\n", item, item) ; */ if (item != NULL) free(item) ; } data ListItem::copy() { /* make a copy */ char *str ; if (item == NULL) return NULL ; str = strdup(item) ; if (str == NULL) { fprintf(stderr, "could not duplicate string\n") ; exit(1) ; } return str ; } /* Compare data in host object with given data. <0 is smaller, =0 is equal, >0 is bigger */ int ListItem::compare(data x) { if (item == NULL && x == NULL) return 0 ; if (item == NULL) return -1 ; if (x == NULL) return 1 ; return strcmp(item, x) ; } void ListItem::print() { /* print to stdout */ if (item != NULL) { /* Debugging: printf("(%p,%s)", item, item) ; */ printf("%s", item) ; } }