#include "llist_C.h" #include #include void destroy(ListPtr this) { NodePtr p = this->head, t; while (p != NULL) { t = p; p = getNext(p); cleanLNode(&t); } this->head = NULL; return; } void copyLList_byPtr(ListPtr to, ListPtr from) { NodePtr t = from->head; if (to->head != NULL) { destroy(to); } while (t != NULL) { insertLList_tail(to, getData(t)); t = getNext(t); } return; } void copyLList_bySct(ListPtr to, LList from) { copyLList_byPtr(to, &from); return; } void insertLList_tail(ListPtr to, char d) { NodePtr p = to->head; while (p != NULL && getNext(p) != NULL) { p = getNext(p); } if (p != NULL) { setNext(p, makeLNode(d, NULL)); } else { to->head = makeLNode(d,NULL); } return; } /* look for data in list */ NodePtr findLList(ListPtr in, char d) { NodePtr p = in->head; while (p != NULL && getData(p) != d) { p = getNext(p); } return p; } NodePtr bef_findLList(ListPtr in, char d) { NodePtr p = in->head; if (p == NULL || getData(p) >= d) { p = NULL; } else { while (getNext(p) != NULL && getData(getNext(p)) < d) { p = getNext(p); } } return p; } void printLList(ListPtr this, const char *pre, const char *in, const char *post) { NodePtr p = this->head; if (p == NULL) { printf("%s%s\n", pre, post); } else { printf("%s%c", pre, getData(p)); while (getNext(p) != NULL) { p = getNext(p); printf("%s%c", in, getData(p)); } printf("%s\n", post); } return; } NodePtr makeLNode(char d, NodePtr n) { NodePtr p = (NodePtr)malloc(sizeof(LNode)); p->data = d; p->next = n; return p; } NodePtr makeLNode_empty(void) { NodePtr p = (NodePtr)malloc(sizeof(LNode)); p->data = '\0'; p->next = NULL; return p; } NodePtr getNext(NodePtr of) { return of->next; } void setNext(NodePtr of, NodePtr to) { of->next = to; return; } char getData(NodePtr of) { return of->data; } void setData(NodePtr of, char to) { of->data = to; return; } void copyLNode_byPtr(NodePtr to, NodePtr from) { to->data = from->data; to->next = from->next; return; } void copyLNode_bySct(NodePtr to, LNode from) { to->data = from.data; to->next = from.next; return; } ListPtr makeLList_empty(void) { ListPtr p = (ListPtr)malloc(sizeof(LList)); p->head = NULL; return p; } ListPtr makeLList(char d) { ListPtr p = (ListPtr)malloc(sizeof(LList)); p->head = makeLNode(d, NULL); return p; } /* place new data in list */ void insertLList(ListPtr into, char d, NodePtr after) { if (after == NULL || into->head == NULL) { insertLList_head(into, d); } else { insertLList_mid(into, d, after); } return; } void insertLList_mid(ListPtr into, char d, NodePtr after) { NodePtr p; if (into != NULL && after != NULL) { p = makeLNode(d, getNext(after)); if (p != NULL) { setNext(after, p); } } return; } void insertLList_head(ListPtr into, char d) { NodePtr p = makeLNode(d, into->head); if (p != NULL) { into->head = p; } return; } void cleanLNode(NodePtr * this) { free(*this); *this = NULL; return; } void cleanLList_byPtr(ListPtr * this) { destroy(*this); free(*this); *this = NULL; return; } void cleanLList_bySct(LList this) { destroy(&this); return; }