int main(){ /*make list and output file*/ ListHndl TheList = newList(); FILE *out = fopen("out.out", "w"); /*test empty in empty case*/ if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); printf("testing insert one number\n"); insertAtFront(TheList,(unsigned long*)25728); printf("%lu\n",(unsigned long)getFirst(TheList)); printf("%lu\n",(unsigned long)getLast(TheList)); /*should have same value*/ printf("testing list with three numbers\n"); insertAtFront(TheList,(unsigned long*)1589458); insertAtBack(TheList,(unsigned long*)35762111234); printf("%lu\n",(unsigned long)getFirst(TheList)); /*test empty in full case*/ if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); /*test moving the current pointer around*/ moveFirst(TheList); moveNext(TheList); printf("%lu\n",(unsigned long)getCurrent(TheList)); moveLast(TheList); movePrev(TheList); printf("%lu\n",(unsigned long)getCurrent(TheList)); /*test printList*/ printList(out, TheList); /*test makeEmpty*/ makeEmpty(TheList); if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); /*test inserting functions*/ insertAtFront(TheList,(unsigned long*)2); insertAtFront(TheList,(unsigned long*)1); insertAtFront(TheList,(unsigned long*)4); insertAtBack(TheList,(unsigned long*)4); moveLast(TheList); insertBeforeCurrent(TheList,(unsigned long*)3); printList(out,TheList); deleteFirst(TheList); deleteCurrent(TheList); deleteLast(TheList); printList(out,TheList); makeEmpty(TheList); printList(out,TheList); /*free list and close output file*/ freeList(&TheList); fclose(out); return 0; }
// Perform breadth first search on graph with given source. void doBFS(Graph *G, int source) { // Initialize graph first. for (int i = 0; i < G->numVertices; ++i) { G->color[i] = 0; // Set to white. G->distance[i] = 0; G->parent[i] = -1; } G->color[source] = 1; // Set to grey. G->distance[source] = 0; // Create a list Q and add source to it. ListHndl Q = newList(); insertAtBack(Q, source); while (!isEmpty(Q)) { //Dequeue and delete first element. int u = getFirst(Q); deleteFirst(Q); // Move current position to point at first. if (!isEmpty(G->vertices[u])) moveFirst(G->vertices[u]); //Traverse vertices of u. while (!offEnd(G->vertices[u])) { int v = getCurrent(G->vertices[u]); if (G->color[v] == 0) { // Check if V is white. G->color[v] = 1; // Color V grey. G->distance[v] = G->distance[u] + 1; G->parent[v] = u; insertAtBack(Q, v); } moveNext(G->vertices[u]); } G->color[u] = 2; // Color u black. } freeList(&Q); }
void doBFS(GraphHndl g, int source) { ListHndl tempList = NewList(); /*Make the lists white, reset distances and parents*/ for(int i = 0; i < g->vertNums; i++) { g->parentPtr[i] = -1; g->distancePtr[i] = INT_MAX; g->colorPtr[i] = 0; } /*Temporary list holds paths from source*/ insertAtBack(tempList, source); g->colorPtr[source] = 1; g->distancePtr[source] = 0; /*For every node in the source's adjacency list..*/ while(!isEmpty(tempList)) { int currNode = getFirst(tempList); /*Handle case where we're passed a bigger node*/ if(currNode > g->vertNums) { deleteFirst(tempList); break; } /*Second temporary list holds edge path*/ ListHndl tempList2 = g->vertices[currNode]; moveFirst(tempList2); /* For each of the next node's adjacent nodes .. */ while(!offEnd(tempList2)) { int tempEdge = getCurrent(tempList2); /* If the node was white, increase the distance, * color it grey and set it's parent to be currnode * and enqueue this node into the path list */ if(g->colorPtr[tempEdge] == 0) { g->distancePtr[tempEdge] = g->distancePtr[currNode]+1; g->colorPtr[tempEdge] = 1; g->parentPtr[tempEdge] = currNode; insertAtBack(tempList, tempEdge); } moveNext(tempList2); } /*Dequeue the first element and set black to show we're done.*/ deleteFirst(tempList); g->colorPtr[currNode] = 2; } makeEmpty(tempList); //freeList(tempList); }
void doBFS(Graph G, int source) { assert(G->numVerts != 0); ListHndl L = newList(); /*printf("start %d\n", source);*/ insertAtBack(L, source); int i = 0; for(i = 0; i < G->numVerts; i++) { G->color[i]= WHITE; G->parent[i] = NULL; G->distance[i] = INFINITY; } G->color[source] = GREY; G->parent[source] = NULL; G->distance[source] = 0; while(!isEmpty(L)) { int u = getFirst(L); /*printf("u: %d\n", u);*/ if(u > G->numVerts) { deleteFirst(L); break; } ListHndl v = G->verts[u]; int prev; moveFirst(v); while(!offEnd(v)) { int E = getCurrent(v); if(prev == E) { break; } /*printf(" v: %d\n", E);*/ if(G->color[E] == WHITE) { G->color[E] = GREY; G->parent[E] = u; if(G->distance[E] < 0) { G->distance[E] = 0; } G->distance[E] = G->distance[u] + 1; insertAtBack(L, E); } prev = E; moveNext(v); } deleteFirst(L); G->color[u] = BLACK; } }
void insert(HashTable H, char* name, int num){ assert (H != NULL); ListNode temp = malloc(sizeof(ListNodeStruct)); temp->num = num; ListHndl L = lookup(H, name); /* if book does not exist, make one */ if(L == NULL){ ListHndl slot = H->array[hash((unsigned char*)name) % H->tableSize]; HashNode N = newHashNode(name); L = N->L; insertAtBack(slot,N); } insertAtBack(L,temp); }
static aclList parsePermissions(xmlNode *node, aclList policyList) { xmlNode *cur_node = node; policyPtr accessNode = null; if (cur_node && cur_node->type == XML_ELEMENT_NODE && !strcmp(cur_node->name, permissionsTagName)) { for (cur_node = node->children; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { if (!strcmp(cur_node->name, allowTagName)) { accessNode = parsePermission(cur_node, 1); if (accessNode) { insertAtBack(policyList, accessNode); } else { return null; } } else if (!strcmp(cur_node->name, denyTagName)) { accessNode = parsePermission(cur_node, 0); if (accessNode) { insertAtStart(policyList, accessNode); } else { return null; } } else { fprintf(stderr, "\nInvalid Element:%s\n", cur_node->name); } } } } else { fprintf(stderr, "Expected element : %s, as root node but found:%s", permissionsTagName, cur_node->name); } return policyList; }
// Constructor for a new book. // Copies the name using strncpy // Creates a new linked list of type (int). Book *newBook(char* name, int value) { Book *this = malloc (sizeof(Book)); assert(this != NULL); strncpy(this->name, name, 40); this->libID = newList(sizeof(int)); insertAtBack(this->libID, &value); return this; }
void List<NODETYPE>::concatenate(List<NODETYPE> &listSecond) { ListNode<NODETYPE> *currentPtr = listSecond.firstPtr; while (currentPtr != 0) { insertAtBack(currentPtr->getData()); currentPtr = currentPtr->nextPtr; } }
// Return the List handle of paths to a destination. ListHndl getPathTo(Graph *G, int destination) { int distance = getDistance(G, destination); if (distance == -1) return NULL; ListHndl tmp = newList(); // Freed by user. insertAtBack(tmp, destination); for (int i = 0; i < distance; ++i) { insertAtFront(tmp, G->parent[destination]); destination = G->parent[destination]; } return tmp; }
// Add an edge to a graph by appending it to given linked list. void addEdge(Graph *G, int from, int to) { insertAtBack(G->vertices[from], to); G->numEdges++; }
must_inline void insertRectNext(TY* pY,const TRect& rect){ if (pY->nextY==0) insertAtBack(pY,rect); else insertRect(&pY->nextY,rect); }
void addEdge(Graph G, int from, int to) { insertAtBack(G->verts[from], to); G->numEdges = G->numEdges + 1; }
void addEdge(GraphHndl g, int from, int to) { /* Put the newest edge path at the end of the list */ insertAtBack(g->vertices[from], to); g->edgeNums = g->edgeNums + 1; //increment edge numbers after adding one in. }
void insertID(BookListHndl L, int id) { assert (L != NULL); insertAtBack(L->curr->bookIDs, id); }
/* main program*/ int main(){ /* variables */ char inName[128]; char outName[128]; ListHndl array[128]; char tempLine[128]; int customers; int purchases; int tempCust; unsigned long tempBook; FILE *in; FILE *out; /* greeting & prompt for input and output file names */ printf("Welcome! Please enter your input file here: "); scanf("%s",inName); in = fopen(inName, "r"); if(in == NULL){ printf("Error: This input file does not exist.\n"); exit(1); } printf("Please enter your output file here: "); scanf("%s",outName); out = fopen(outName, "w"); /* check numbers of customer and purchased */ do{ if(fgets(tempLine, 128, in) == NULL) invalidInput(); }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &customers) != 1) invalidInput(); do{ if(fgets(tempLine, 128, in) == NULL) invalidInput(); }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &purchases) != 1) invalidInput(); /*makes empty Lists and fills them up*/ for(int i = 0; i<customers; i++) array[i] = newList(); for(int i = 0; i<purchases; i++){ do{ if(fgets(tempLine, 128, in) == NULL){ printf("Error: Number of purchases does not match (too little).\n"); exit(1); } }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &tempCust) != 1) invalidInput(); if(sscanf(tempLine,"%*d%lu", &tempBook) != 1) invalidInput(); if(tempCust > customers){ printf("Error: Invalid customer number.\n"); exit(1); } if(isEmpty(array[tempCust-1])){ insertAtBack(array[tempCust-1],tempBook); continue; } moveFirst(array[tempCust-1]); while(!offEnd(array[tempCust-1])){ if(tempBook < (unsigned)getCurrent(array[tempCust-1])){ insertBeforeCurrent(array[tempCust-1], tempBook); break; } moveNext(array[tempCust-1]); } if(offEnd(array[tempCust-1])) insertAtBack(array[tempCust-1],tempBook); } /* checks for any lingering purchases */ for(;;){ if(fgets(tempLine, 128, in) == NULL) break; if(sscanf(tempLine,"%d", &tempCust) != 0){ printf("Error: Number of purchases does not match (too many).\n"); exit(1); } } /*printing store*/ fprintf(out, "customer# books purchased\n"); for(int i = 0; i<customers; i++){ fprintf(out, "%d ",i+1); moveFirst(array[i]); while(!offEnd(array[i])){ fprintf(out, "%lu ", getCurrent(array[i])); moveNext(array[i]); } fprintf(out, "\n"); } /*free everythingggggggggggg*/ for(int i = 0; i<customers; i++) freeList(&array[i]); fclose(in); fclose(out); /* end program */ printf("Done.\n"); return 0; }
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); }