void insertBeforeCurrent(ListRef L, long data){ //inserts new node before current if(!offEnd(L)){ //pre: !offEnd NodeRef newnode = malloc(sizeof(NodeType)); newnode->data = data; newnode->count = 1; newnode->next = NULL; newnode->prev = NULL; if(isEmpty(L)){ //checks if list is empty L->first = newnode; L->last = newnode; L->current = newnode; }else if (atFirst(L)){ newnode->next = L->first; L->first->prev = newnode; L->first = newnode; L->current = newnode; }else{ //otherwise connects node to list newnode->next = L->current; newnode->prev = L->current->prev; L->current->prev->next = newnode; L->current->prev = newnode; } L->length++; assert(!offEnd(L) && !isEmpty(L)); //post: !offEnd() and !isEmpty() } }
void deleteCurrent(ListRef L){ //deletes current node and frees it if(!isEmpty(L) && !offEnd(L)){ //pre: !isEmpty() and !offEnd() if(atLast(L)) deleteLast(L); else if(atFirst(L)) deleteFirst(L); else{ NodeRef oldnode = L->current; L->current->prev->next = L->current->next; L->current->next->prev = L->current->prev; L->current = L->current->next; free(oldnode); oldnode = NULL; L->length--; } } }
void deleteFirst(ListRef L){ //deletes first node and frees it if(!isEmpty(L)){ //pre: !isEmpty() NodeRef oldnode = L->first; if(L->first == L->last){ //checks if list is only a single node L->first = NULL; L->last = NULL; L->current = NULL; }else{ if(atFirst(L)) L->current = L->current->next; L->first = L->first->next; L->first->prev = NULL; } free(oldnode); oldnode = NULL; L->length--; } }
/* * insert before the current node, if defined */ void insertBeforeCurrent(ListRef L, long data) { NodeRef n; if (L == NULL) { printf( "Error: Called insertBeforeCurrent() on NULL ListRef\n"); //exit(EXIT_FAILURE); } else if (isEmpty(L)) { printf( "Error: Called insertBeforeCurrent() on empty ListRef\n"); //exit(EXIT_FAILURE); } else if (offEnd(L)) { printf("Error: Called insertBeforeCurrent() with offEnd() == true\n"); //exit(EXIT_FAILURE); } if (atFirst(L)) { insertBeforeFirst(L, data); } else { n = newNode(data); n->next = L->current; n->prev = L->current->prev; n->prev->next = n; n->next->prev = n; L->length++; } }
int main (){ /***Exercise List constructor***/ ListHndl List; List = NULL; List = newList (); if(List){ printf("List Created\n"); }else{ printf("List Not Created\n"); } printf("isEmpty %d\n",isEmpty(List)); /*should print 1*/ /***Populate with test data***/ int i; for(i=0; i<=4; i++){ insertAtFront(List,i); } printList(stdout, List); printf("isEmpty %d\n",isEmpty(List)); /*should print 0*/ int j; for(j=5; j<=9; j++){ insertAtBack(List,j); } printList(stdout, List); /***Exercise all access functions***/ printf("offEnd %d\n",offEnd(List));/*should print 0*/ printf("atFirst %d\n",atFirst(List));/*should print 0*/ printf("atLast %d\n",atLast(List));/*should print 0*/ printf("getFirst %d\n", getFirst(List));/*should print 4*/ printf("getLast %d\n", getLast(List));/*should print 9*/ printf("getCurrent %d\n", getCurrent(List));/*should print 0*/ /***Exercise all removal manipulation functions***/ deleteLast(List); printList(stdout, List); printf("getLast %d\n", getLast(List));/*should print 8*/ deleteFirst(List); printList(stdout, List); printf("getFirst \n", getFirst(List));/*should print 3*/ deleteCurrent(List); printList(stdout, List); moveLast(List); printList(stdout, List); movePrev(List); printList(stdout, List); moveNext(List); printList(stdout, List); /***Exercise various edge cases***/ makeEmpty(List); insertAtFront(List, 40); moveFirst(List); deleteCurrent(List); insertAtFront(List, 41); insertAtBack(List, 42); moveFirst(List); insertBeforeCurrent(List, 43); printList(stdout, List); /***Exercise List destructors***/ deleteCurrent(List); printList(stdout, List); makeEmpty(List); printf("offEnd %d\n",offEnd(List));/*should print 1*/ freeList(&List); return(0); }