/************************************** * stringlist.c * author: Erin Nichols * date: 02.22.00 * version: 0.1 * utilities for a doubly linked list *************************************/ #include struct node { /* our behind the scenes node struct */ char *contents; /* contents of the node */ struct node *next; /* pointer to the next node */ struct node *prev; /* pointer to the previous node */ }; /* struct node */ struct list { /* wrapper struct (for extra fun) */ struct node *current; /* pointer to our current node */ struct node *front; /* pointer to the front of the list */ struct node *back; /* pointer to the back of the list */ int len; /* the length of our list */ }; /* struct list */ /* get, set, and basic functions */ void setContents(struct node c, char *s) { c.contents = s; /* set contents of node c to s */ } /* void setContents(struct node *, char *) */ void setNext(struct node *c, struct node *n) { (*c).next = n; } /* void setNext(struct node *, struct node *) */ void setPrev(struct node *c, struct node *p) { (*c).prev = p; } /* void setPrev(struct node *, struct node *) */ char *getContents(struct node c) { return c.contents; } /* char *getContents(struct node *) */ struct node *getNext(struct node c) { return c.next; } /* struct node *getNext(struct node *) */ struct node *getPrev(struct node c) { printf("getPrev starting...\n"); return c.prev; } /* struct node *getPrev(struct node *) */ struct node *makeNode(char *c, struct node *p, struct node *n) { /* create a temporary node in which to store parameters */ struct node *tmp; (*tmp).contents = c; (*tmp).prev = p; (*tmp).next = n; return tmp; } /* struct node makeNode(char *, struct node *, struct node *) */ void advance(struct list ls) { /* point current to the next element */ ls.current = getNext(*ls.current); } /* void advance(struct list) */ /* now to put the fun back in FUNction! */ void addToEnd(struct list ls, char *toAdd) { /* if the list is empty */ if ((getPrev(*ls.back) == NULL) && (getNext(*ls.front) == NULL)) { /* create a node and make it the front, current, and back */ ls.front = makeNode(toAdd, NULL, NULL); ls.current = ls.front; ls.back = ls.front; /* set len to 1 */ ls.len = 1; } /* if */ else { /* the list is nonempty */ /* point current to the back */ ls.current = ls.back; /* point next to a new node */ setNext(ls.current, makeNode(toAdd, ls.back, NULL)); /* advance to the new node */ advance(ls); /* increment len */ ls.len++; } /* else */ } /* void addToEnd(struct list, char *) */ void addToFront(struct list ls, char *toAdd) { /* check whether list is empty */ if ((getPrev(*ls.back) == NULL) && (getNext(*ls.front) == NULL)) { /* set front, current, and back to new node */ ls.front = makeNode(toAdd, NULL, NULL); ls.current = ls.front; ls.back = ls.front; ls.len = 1; } /* if */ else { /* the list is nonempty */ /* make the front element's previous element be a new node */ setPrev(ls.front, makeNode(toAdd, NULL, ls.front)); /* set front to the new node */ ls.front = (struct node *)getPrev(*ls.front); /* set current to the front */ ls.current = ls.front; /* increment len */ ls.len++; } /* else */ } /* void addToFront(struct list, char *) */ int atEnd(struct list ls) { /* if the current element is the back of the list */ if (getNext(*ls.current) == NULL) return 0; else return 1; } /* int atEnd(struct list) */ void printList(struct list ls) { /* go to the front */ ls.current = ls.front; printf("( "); /* while we're not at the end */ while (!atEnd(ls)) { /* print the current element */ printf("%s,", getContents(*ls.current)); /* advance */ advance(ls); } /* while */ printf(")\n"); } /* void printList(struct list) */ /* quicksort the list. to be implemented. */ struct list sortList(struct list ls) { return ls; } /* struct list sortList(struct list) */ /* delete the current element. awaiting testing. */ void del(struct node *c) { setNext(getPrev(*c), getNext(*c)); setPrev(getNext(*c), getPrev(*c)); free(getNext(*c)); free(getNext(*c)); } /* del(struct node *) */