int main(int argc, const char * argv[]) { FILE *file = fopen(argv[1], "r"); char line[1024]; while(fgets(line, 1024, file)) { //printf("%s", line); } struct node *aList; initializeLinkedList(&aList); insertBack(9, &aList); insertBack(61, &aList); insertFront(0, &aList); deleteFront(&aList); insertFront(109, &aList); insertFront(81, &aList); insertBack(1890, &aList); printf("%s\n", "printing list:"); printLinkedList(&aList); destroyLinkedList(&aList); printf("got %d\n", ant); fclose(file); #ifndef _MY_MYTEST_H_ printf("defining"); #endif return 0; }
int main(){ struct linked * head = NULL ; int i = 90; head = insertFront(head,i*3); insertBack(head,90); insertBack(head,91); insertBack(head,92); insertBack(head,93); head = addSorted(head,23); printAll(head); printIter(head); return 0 ; }
/* addEdge */ void addEdge(GraphRef G, int u, int v){ if(isEmpty(G->adj[u])){ insertBack(G->adj[u], v); } else if(!isEmpty(G->adj[u])){ putEdgeArc(G, u, v); } if(isEmpty(G->adj[v])){ insertBack(G->adj[v], u); } else if(!isEmpty(G->adj[v])){ putEdgeArc(G, v, u); } G->size++; }
/* DFS */ void DFS(GraphRef G, ListRef S){ int i, s, top, low; moveTo(S, 0); s = getCurrent(S); G->parent[s] = 0; for(i=0;i<=getLength(S);i++){ if(G->color[s] == 1){ G->parent[s] = 0; visit(G, s); } if(i<getLength(S)-1){ moveNext(S); s = getCurrent(S); } } makeEmpty(S); top = getTime(G); while(getLength(S) < getOrder(G)){ low = 0; for(i=1;i<=getOrder(G);i++){ if(top > G->finish[i] && low < G->finish[i]){ low = G->finish[i]; s = i; } } insertBack(S, s); top = low; } }
void BFS(GraphRef G, int s){ int i, u, tmp; G->source = s; for ( i = 1;i <= getOrder(G); i++){ G->color[i] = 'w'; G->discover[i] = INF; G->parent[i] = NIL; } G->color[s] = 'g'; G->discover[s] = 0; G->parent[s] = NIL; ListRef Q = newList(); insertFront( Q, s ); while( !isEmpty(Q) ){ u = getFront(Q); deleteFront(Q); moveTo(G->adjacency[u], 0); while ( !offEnd(G->adjacency[u]) ){ tmp = getCurrent(G->adjacency[u]); if ( G->color[tmp] == 'w'){ G->color[tmp] = 'g'; G->discover[tmp] = G->discover[u] + 1; G->parent[tmp] = u; insertBack( Q, tmp ); } moveNext(G->adjacency[u]); } G->color[u] = 'b'; } freeList(&Q); }
/* BFS */ void BFS(GraphRef G, int s){ ListRef Q = newList(); int v, w, i; G->source = s; insertFront(Q, s); G->color[s] = 2; G->distance[s] = 0; while(getLength(Q) > 0){ moveTo(Q, 0); v = getCurrent(Q); G->color[v] = 3; deleteCurrent(Q); if(!isEmpty(G->adj[v])){ moveTo(G->adj[v], 0); for(i=0;i<getLength(G->adj[v]);i++){ w = getCurrent(G->adj[v]); if(G->color[w] < 2){ G->color[w] = 2; G->parent[w] = v; G->distance[w] = G->distance[v]+1; insertBack(Q, w); } if(i<getLength(G->adj[v])-1){ moveNext(G->adj[v]); } } } } freeList(&Q); }
ListRef copyList(ListRef L){ NodeRef N = NULL; ListRef M = newList(); for(N = L->front; N != NULL; N=N->next){ insertBack(M, N->data); } return M; }
/* * getPathc * Appends to the List L the vertices of the shortest path in G from source * to u, or appends L the value NIL if no such path exists. */ void getPath(ListRef L, GraphRef G, int u) { if (getSource(G) == NIL) { printf("Graph Error: calling getPath() before BFS is called"); exit(1); } else { if (G->source == u) { insertBack (L, u); } else if (G->parent[u] == NIL) { } else { getPath(L, G, G->parent[u]); insertBack(L, u); } } }
void insertBack(struct linked * head,int val ){ if ( head->next == NULL ){ head->next = createNode(val); } else{ insertBack(head->next,val); } }
/* above */ void transposeHelp(GraphRef T, ListRef L, int i){ int v; moveTo(L,0); while(!offEnd(L)){ v = getCurrent(L); insertBack(T->adj[v],i); moveNext(L); } }
/* * copyList * Returns a new List Identical to the one passed in. */ ListRef copyList(ListRef L){ ListRef copy = newList(); NodeRef temp = L->front; while( temp!=NULL){ insertBack(copy , temp->data); temp = temp->next; } return copy; }
// Definition of copy constructor DLL_ChildClass::DLL_ChildClass(const DLL_ChildClass& param) { //initialization first = NULL; last = NULL; count = 0; //copy param to this Node *p1 = param.first; for (int i = 0; i < param.count; i++) { insertBack(p1->getData()); p1 = p1->getNextLink(); } }
/* addArc() * Pre: (u&v)>0 && (u&v)<=getOrder() */ void addArc(GraphRef G, int u, int v){ if(G == NULL){ fprintf(stderr,"Graph Error: calling addArc on NULL Graph"); exit(1); } if(u<1 || u>getOrder(G)){ fprintf(stderr,"addArc Error: addArc(P1,P2,P3) P2 index out of range"); exit(1); } if(v<1 || v>getOrder(G)){ fprintf(stderr,"addArc Error: addArc(P1,P2,P3) P3 index out of range"); exit(1); } insertBack(G->adj[u],v); G->size++; }
void insertAfterCurrent(ListRef L, int data){ if ( !isEmpty(L) && !offEnd(L) ){ if (getIndex(L) == getLength(L)){ insertBack(L, data); return; } else{ NodeRef N = newNode(data); L->current->next->prev = N; N->next = L->current->next; L->current->next = N; N->prev = L->current; } L->length++; } }
/* putEdgeArc */ void putEdgeArc(GraphRef G, int u, int v){ moveTo(G->adj[u], 0); while(1){ if(getCurrent(G->adj[u]) < v && getIndex(G->adj[u]) < getLength(G->adj[u])-1){ moveNext(G->adj[u]); } else if(getCurrent(G->adj[u]) > v ){ insertBeforeCurrent(G->adj[u], v); break; } else{ insertBack(G->adj[u], v); break; } } }
void insertNode(int pos, int val) { int i; if (i <= 0) { insertFront(val); } else if (i >= m_size) { insertBack(val); } else { ListNode *ptr1, *ptr2; ptr1 = m_head; for (i = 0; i < pos - 1; ++i) { ptr1 = ptr1->next; } ptr2 = new ListNode(val); ptr2->next = ptr1->next; ptr1->next = ptr2; ++m_size; } }
void insertAfterCurrent(ListRef L, long data){ if(L==NULL){ printf("List error: calling insertAfterCurrent on NULL ListRef\n"); exit(1); } if (offEnd(L)){ printf("List error: calling insertAfterCurrent on NULL current\n"); exit(1); } if(L->back == L->current){ insertBack(L, data); } else { NodeRef N = newNode(data); N->next = (L->current)->next; N->prev = L->current; (N->next)->prev = N; (L->current)->next = N; } L->length++; }
void addArc(GraphRef G, int u, int v){ ListRef row = G->adjacency[u]; /* Inserts vetex v into row u in the adjacency array */ if (isEmpty(row)){ insertFront(row, v); return; } else{ moveTo(row, 0); while (!offEnd(row)){ if (v == getCurrent(row)) return; if (v > getCurrent(row)){ moveNext(row); }else{ insertBeforeCurrent(row, v); return; } } insertBack(row, v); } }
List<T>::List(const Iter & begin_iterator, const Iter & end_iterator) : head(NULL), tail(NULL), length(0) { for (Iter i = begin_iterator; i != end_iterator; ++i) insertBack(*i); }
void TreeItem::insertAt(int index, TreeItem *item){ if(index < 0 || index >= m_childItems.size()) insertBack(item); else m_childItems.insert(index, item); }
// Definition of overloaded assignment operator DLL_ChildClass* DLL_ChildClass::operator=(const DLL_ChildClass& param) { if (&(*first) == &(*param.first)) { cerr << "Attempted assignment to itself."; } else { if (count == 0) { //inserting new nodes in list1 with the data copied from list2 Node *p1 = param.first; for (int i = 0; i < param.count; i++) { insertBack(p1->getData()); p1 = p1->getNextLink(); } } else if (param.count == 0) { //empty list1 to make it as list2, by destroying all the nodes destroyList(); } else if (count < param.count) { //overwrite the data in all the nodes of list1 Node *temp1 = first; Node *temp2 = param.first; for (int i = 0; i < count; i++) { temp1->setData(temp2->getData()); temp1 = temp1->getNextLink(); temp2 = temp2->getNextLink(); } //insert additional nodes to copy the rest of the data from list2. for (int i = 0; i < param.count - count; i++) { insertBack(temp2->getData()); temp2 = temp2->getNextLink(); } } else if (count > param.count) { //overwrite the data in the nodes of list1 Node *temp1 = first; Node *temp2 = param.first; for (int i = 0; i < param.count; i++) { temp1->setData(temp2->getData()); temp1 = temp1->getNextLink(); temp2 = temp2->getNextLink(); } last = temp1->getPreviousLink(); last->setNextLink(NULL); //delete the extra nodes. while (temp1 != NULL) { Node *p1 = temp1; temp1 = temp1->getNextLink(); delete p1; } } else if (count == param.count) { //copy the data from one list to the other Node *temp1 = first; Node *temp2 = param.first; for (int i = 0; i < count; i++) { temp1->setData(temp2->getData()); temp1 = temp1->getNextLink(); temp2 = temp2->getNextLink(); } } } return this; }
void *cpu(void *arg){ sharedRes *sharedResource = (sharedRes *) arg; process *currentProcess = NULL; int oldTime = sharedResource->time; while (EXECUTION_CONDITION) { // If the CPU is free... if (currentProcess == NULL) { // If we finished our last process, and there are still processes on the ready queue, schedule another one. if (sharedResource->scheduler->readyQueueSize > 0) { currentProcess = synchronizedNextProcess(sharedResource->scheduler); if (currentProcess != NULL) { currentProcess->timeEnteredCPU = sharedResource->time; pthread_mutex_lock(&lock); pthread_mutex_unlock(&lock); printf("Running pID: %d With Run Time Remaining: %d Time: %d Priority: %d \n", currentProcess->pID, currentProcess->runTimeRemaining, sharedResource->time, currentProcess->priority); } } } // Otherwise, if a process is running... else { // If the process's run time is up, remove it from the CPU. if (currentProcess->runTimeRemaining <= (sharedResource->time - currentProcess->timeEnteredCPU)) { currentProcess->timeDone = sharedResource->time; currentProcess->runTimeRemaining = 0; deallocate(sharedResource, currentProcess); pthread_mutex_lock(&lock); insertBack(sharedResource->doneQ, currentProcess); sharedResource->doneQSize++; pthread_mutex_unlock(&lock); printf("Added pID: %d to the doneQ \n", currentProcess->pID); currentProcess = NULL; } // Otherwise, we'll do some other things... else { // Randomly decide whether or not to generate a system call based on the process's probability of making a system call. if (oldTime != sharedResource->time) { double randomValue = (double)rand() / RAND_MAX; if (randomValue < currentProcess->probSystemCall) { // Determine whether or not the process // should be classified as interactive. currentProcess->timeSystemCall++; // Don't bother calculating anything unless // it has already run a few times, because // otherwise it doesn't mean much. if (currentProcess->timeAllotted > 5) { double systemCallRatio = (double)currentProcess->timeSystemCall / currentProcess->timeAllotted; if (!currentProcess->isInteractive && systemCallRatio > sharedResource->properties->interactiveThreshold) { currentProcess->isInteractive = true; printf("pId %d flagged as interactive\n", currentProcess->pID); } else if (currentProcess->isInteractive && systemCallRatio <= sharedResource->properties->interactiveThreshold) { currentProcess->isInteractive = false; printf("pId %d interactive flag removed\n", currentProcess->pID); } } int updatedRunTime = (currentProcess->runTimeRemaining - (sharedResource->time - currentProcess->timeEnteredCPU)); currentProcess->runTimeRemaining = updatedRunTime < 0 ? 0 : updatedRunTime; //making sure the runTime cannot be negative currentProcess->timeInterrupt = ((int)rand() % 6) + 3;//setting the time it takes for the interrupt to complete currentProcess->timeEnteredWaitQ = sharedResource->time; cll_enqueue(sharedResource->waitQ, currentProcess); printf("System call made by pID %d, added to the waitQ \n", currentProcess->pID); currentProcess = NULL; } } // Round robin / Time slice // Time slice of -1 means no limit. if (currentProcess != NULL && currentProcess->timeSlice != -1 && currentProcess->timeSlice <= (sharedResource->time - currentProcess->timeEnteredCPU)){ currentProcess->runTimeRemaining -= currentProcess->timeSlice; if (currentProcess->runTimeRemaining <= 0){ currentProcess->runTimeRemaining = 0; currentProcess->timeDone = sharedResource->time; deallocate(sharedResource, currentProcess); insertBack(sharedResource->doneQ, currentProcess); sharedResource->doneQSize++; printf("cpuRR Added pID: %d to the doneQ \n", currentProcess->pID); } else { synchronizedSchedule(sharedResource, currentProcess); printf("cpuRR Added pID: %d back to the ReadyQ \n", currentProcess->pID); } currentProcess = NULL; } } } // Update everything in the wait queue. circularlistnode *pointer = sharedResource->waitQ->next; while(pointer != sharedResource->waitQ) { if ((pointer->current->timeInterrupt <= (sharedResource->time - pointer->current->timeEnteredWaitQ) && pointer->current->hasBeenAllocatedMemory) || (!pointer->current->hasBeenAllocatedMemory && allocate(sharedResource, pointer->current))) { removeNode(pointer); pointer->current->timeInterrupt = 0; synchronizedSchedule(sharedResource, pointer->current); printf("Added pID: %d back to the readyQ \n", pointer->current->pID); } pointer = pointer->next; } oldTime = sharedResource->time; } pthread_exit(NULL); }
int main(int argc, char* argv[]) { int i; ListRef A = newList(); ListRef B = newList(); ListRef ACopy = NULL; ListRef AB_Cat = NULL; insertBack(A, 10); insertBack(A, 20); insertBack(A, 30); insertBack(A, 40); insertBack(A, 50); insertBack(A, 60); printf("equals(A,B) : %d\n", equals(A, B)); insertBack(B, 10); insertBack(B, 20); insertBack(B, 30); insertBack(B, 40); insertBack(B, 50); insertBack(B, 60); AB_Cat = catList(A, B); printf("printLIST(AB_Cat) : "); printLIST(AB_Cat); ACopy = copyList(A); printf("printLIST(A) : "); printLIST(A); printf("printLIST(ACopy) : "); printLIST(ACopy); printf("equals(A,ACopy) : %d\n", equals(A, ACopy)); printf("equals(A,B) : %d\n", equals(A, B)); printf("printLIST(A) : "); printLIST(A); moveTo(A, getLength(A)); printf("offEnd(A) : %d\n", offEnd(A)); moveTo(A, 3); insertBeforeCurrent(A, 35); insertAfterCurrent(A, 45); printf("printLIST(A) : "); printLIST(A); printf("getCurrent(A) : %d\n", getCurrent(A)); movePrev(A); printf("getCurrent(A) : %d\n", getCurrent(A)); deleteCurrent(A); printf("printLIST(A) : "); printLIST(A); makeEmpty(B); deleteFront(A); printf("printLIST(A) : "); printLIST(A); printf("getLength(A) : %d\n", getLength(A)); printf("isEmpty(A) : %d\n", isEmpty(A)); makeEmpty(A); printf("isEmpty(A) : %d\n", isEmpty(A)); printf("getLength(A) : %d\n", getLength(A)); /* printf("printLIST(A) : "); printLIST(A); */ insertFront(B, 50); insertBack(B, 60); insertFront(B, 40); insertBack(B, 70); insertFront(B, 30); insertBack(B, 80); insertFront(B, 20); insertBack(B, 90); insertFront(B, 10); printf("printLIST(B) : "); printLIST(B); printf("offEnd(B) : %d\n", offEnd(B)); moveTo(B, 5); printf("offEnd(B) : %d\n", offEnd(B)); printf("getCurrent(B) : %d\n", getCurrent(B)); deleteCurrent(B); printf("printLIST(B) : "); printLIST(B); /* printf("getCurrent(B) : %d\n", getCurrent(B));*/ moveTo(B, 0); printf("getFront(B) : %d\n", getFront(B)); printf("getCurrent(B) : %d\n", getCurrent(B)); deleteFront(B); printf("printLIST(B) : "); printLIST(B); printf("getFront(B) : %d\n", getFront(B)); /* printf("getCurrent(B) : %d\n", getCurrent(B)); */ moveTo(B, (getLength(B)-1)); printf("getCurrent(B) : %d\n", getCurrent(B)); printf("getBack(B) : %d\n", getBack(B)); deleteBack(B); printf("getBack(B) : %d\n", getBack(B)); /* printf("getCurrent(B) : %d\n", getCurrent(B)); */ moveTo(B, (getLength(B)-1)); printf("getCurrent(B) : %d\n", getCurrent(B)); printf("getBack(B) : %d\n", getBack(B)); deleteBack(B); printf("getBack(B) : %d\n", getBack(B)); printf("getCurrent(B) : %d\n", getCurrent(B)); return(0); }