int main() { int count = 0; int total = 0; int largest = INT_MIN; Element i; Vector vector = createVector(); V_Iterator it = getEnd(&vector); while( (scanf("%d",&i) == 1 ) ) { if(vector.size == 0 ) prepend(&vector,i); else { insertAfter(&it,i); moveNext(&it); } if( i > largest) largest = i; } printf( "Valor máximo: %d\n", largest); it = getBegin(&vector); while( it.index < vector.size ) { i = getElement(&it); // if( largest % i == 0 ) { total += i; count++; // } moveNext(&it); } if(count != 0) printf( "Média: %d\n", (total/count)); }
// BFS Algorithm void BFS(Graph G, int s) { if(G == NULL) { printf("Graph Error: calling BFS() on NULL Graph reference"); exit(1); } if(s<1 || s>getOrder(G)) { printf("Graph Error: calling BFS() with out of bounds 's' value"); exit(1); } for(int i=1; i<=getOrder(G); i++) { G->color[i] = 0; // initialize 0=white G->distance[i] = INF; // initialize INF distance G->parent[i] = NIL; // initialize NIL parent } G->source = s; // set source to s G->color[s] = 1; // set color to gray (found) G->distance[s] = 0; // no distance G->parent[s] = NIL; // NIL parent List Q = newList(); // Use list as queue append(Q, s); for(moveTo(Q, 0); getIndex(Q)>=0; moveNext(Q)) { int x = getElement(Q); for(moveTo(G->adj[x], 0); getIndex(G->adj[x])>=0; moveNext(G->adj[x])) { int y = getElement(G->adj[x]); if(G->color[y] == 0) { // if the color is white G->color[y] = 1; // set color to gray G->distance[y] = G->distance[x]+1; G->parent[y] = x; append(Q, y); } } G->color[x] = 2; // set color to black } freeList(&Q); // free the list }
TreeIterator2D::TreeIterator2D(TreeLoader2D *trees) { TreeIterator2D::trees = trees; //Test if the GridList has anything in it if (trees->pageGridList.empty()) { // If not, set hasMore to false and return. hasMore = false; return; } //Setup iterators currentGrid = trees->pageGridList.begin(); currentX = 0; currentZ = 0; currentTreeList = &trees->_getGridPage(currentGrid->second, currentX, currentZ); currentTree = currentTreeList->begin(); hasMore = true; //If there's not tree in the first page, keep looking if (currentTree == currentTreeList->end()) moveNext(); //Read the first tree's data _readTree(); //Read one more tree, so peekNext() will properly return the first item, while the system //actually is looking ahead one item (this way it can detect the end of the list before //it's too late) if (hasMore) moveNext(); }
int main() { ListHndl intList; intList = newList(); int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; /* * Integer list testing * */ printHeading(" INTEGER LIST ", '#', 80); printList(stdout, intList); accessorTest("isEmpty", 1, isEmpty(intList)); for(int i = 0; i < 10; i++) { insertAtFront(intList, data[i]); mutatorTest("%s : data = %d", "insertAtFront", data[i]); } moveFirst(intList); for(int i = 0; i < 5; i++) { printList(stdout, intList); insertBeforeCurrent(intList, data[i]); mutatorTest("insertBeforeCurrent : data = %d", data[i]); moveNext(intList); } accessorTest("isEmpty", 0, isEmpty(intList)); printList(stdout, intList); moveFirst(intList); while( !offEnd(intList) ) { printList(stdout, intList); moveNext(intList); } moveLast(intList); while( !offEnd(intList) ) { printList(stdout, intList); movePrev(intList); } makeEmpty(intList); mutatorTest("makeEmpty( intList)"); printList(stdout, intList); accessorTest("isEmpty", 1, isEmpty(intList)); freeList(intList); return 0; }
FPTRLIST_INLINE bool FPtrList::FPtrListPrivate::moveTo(int nIndex) { if (nIndex < 0 || nIndex > m_nSize) { return false; } if (nIndex == 0) { return moveToHeader(); } else if (nIndex == m_cursorPos - 1) { return movePrevious(); } else if (nIndex == m_cursorPos + 1) { return moveNext(); } else { /** 计算最佳搜寻方式,减少遍历次数 */ int nHalfSize = m_nSize / 2; int nStep = (int)abs(m_cursorPos - nIndex); if (nStep < nHalfSize) { if (nIndex < m_cursorPos) return movePrevious(nStep); else return moveNext(nStep); } else { if (nIndex < nHalfSize) { /** 从前半部分顺序往后查找 */ moveToHeader(); return moveNext(nIndex); } else { moveToLast(); return movePrevious(m_nSize - nIndex - 1); } } } /** 失败了,游标重置到开始 */ moveToHeader(); return false; }
int main(int argc, char* argv[]){ List A = newList(); List B = newList(); List C = NULL; int i; for(i=1; i<=20; i++){ append(A,i); prepend(B,i); } printList(stdout,A); printf("\n"); printList(stdout,B); printf("\n"); for(moveFront(A); index(A)>=0; moveNext(A)){ printf("%d ", get(A)); } printf("\n"); for(moveBack(B); index(B)>=0; movePrev(B)){ printf("%d ", get(B)); } printf("\n"); C = copyList(A); printf("%s\n", equals(A,B)?"true":"false"); printf("%s\n", equals(B,C)?"true":"false"); printf("%s\n", equals(C,A)?"true":"false"); moveFront(A); for(i=0; i<5; i++) moveNext(A); // at index 5 insertBefore(A, -1); // at index 6 for(i=0; i<9; i++) moveNext(A); // at index 15 insertAfter(A, -2); for(i=0; i<5; i++) movePrev(A); // at index 10 delete(A); printList(stdout,A); printf("\n"); printf("%d\n", length(A)); clear(A); printf("%d\n", length(A)); freeList(&A); freeList(&B); freeList(&C); return(0); }
void BFS(Graph G, int s){ if( G==NULL ){ printf("Graph Error: cannot run BFS() on NULL Graph pointer"); exit(1); } if( 1>s || s>getOrder(G) ){ printf("Graph Error: vertex s is not in |V(G)|"); exit(1); } G->source = s; int i, x, y; for( i=1; i<=getOrder(G); i++){ G->color[i] = WHITE; G->dist[i] = INF; G->pred[i] = NIL; } G->color[s] = GRAY; G->dist[s] = 0; G->pred[s] = NIL; List Q = newList(); append(Q, s); moveFront(Q); while( getindex(Q)!=-1 ){ x = get(Q); moveFront(G->adj[x]); while( getindex(G->adj[x])!=-1 ){ y = get(G->adj[x]); if( G->color[y]==WHITE ){ G->color[y] = GRAY; G->dist[y] = G->dist[x] + 1; G->pred[y] = x; append(Q, y); } moveNext(G->adj[x]); } G->color[x] = BLACK; moveNext(Q); } freeList(&Q); }
void printComponents(FILE *out,Graph G, List L){ if(G == NULL){ printf("FindComponents.c ERROR: In printComponents()"); printf(" calling G on a NULL Graph reffrence\n"); exit(1); } if(L == NULL){ printf("FindComponents.c ERROR: In printComponents()"); printf("calling List on a NULL list refrence\n"); exit(1); } if(length(L) == 0){ return; } moveTo(L,0); int count = 0; while(getIndex(L) >= 0){ if(getParent(G,getElement(L)) == -1){ count++; } moveNext(L); } fprintf(out,"G contains %d strongly connected components:\n",count); int comp = 1; moveTo(L,0); while(comp <= count){ fprintf(out,"Component %d: ",comp); while(getIndex(L) >= 0 && getParent(G,getElement(L)) != -1){ if(getParent(G,getElement(L)) != -1 && getIndex(L) >= 0){ fprintf(out,"%d ",getElement(L)); moveNext(L); }else{ fprintf(out,"%d\n",getElement(L)); moveNext(L); comp++; break; } } if(getParent(G,getElement(L)) == -1 && getIndex(L) >= 0){ fprintf(out,"%d\n",getElement(L)); moveNext(L); comp++; continue; } fprintf(out,"\n"); } }
/* 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); }
TutorialDialog::TutorialDialog(QWidget *parent) : QDialog(parent), ui(new Ui::TutorialDialog){ ui->setupUi(this); lblText = findChild<QLabel*>("lblText"); tutorials = new QString[5]; tutorials[0] = tr("Hello! Is it your first time here? \nWelcome to Series! Track your favourite TV series, you'll never forget which\nwas the last episode you watched!\n" \ "Thank you for downloading the app, I hope you like it!"); tutorials[1] = tr("In order to add a new series, just click Add new TV series button, it allows\nyou to choose name, espisode and season for a new series!\nA dialog will be showed to do this operation, " \ "just insert name, season and\nepisode (optional) and press Ok. The new series will be displayed immediately."); tutorials[2] = tr("You can update season and episode of every series: just wrtite it into the\ncorrect field and press the Save button."); tutorials[3] = tr("In order to make some changes, press the Modify button.\nYou will be able to change name and positions (the display order)\nof every series. You can also delete them."); tutorials[4] = tr("I hope you like this app! Thank you for downloading it."); index = 0; lblText->setText(tutorials[0]); btnNext = findChild<QPushButton*>("btnNext"); btnBack = findChild<QPushButton*>("btnBack"); btnClose = findChild<QPushButton*>("btnClose"); connect(btnNext, SIGNAL(released()), this, SLOT(moveNext())); connect(btnBack, SIGNAL(released()), this, SLOT(moveBack())); connect(btnClose, SIGNAL(released()), this, SLOT(closeDialog())); btnBack->setEnabled(false); }
void doBFS(GraphRef g, int source){ ListRef list = newList(); initGraph(g); insertAfterLast(list, source); g->color[source]= 1; g->distance[source]=0; while(!isEmpty(list)){ int current = getFirst(list); if(current > g->numVertices){ deleteFirst(list); break; } ListRef edgeList = g->vertices[current]; moveFirst(edgeList); while(!offEnd(edgeList)){ int edge = getCurrent(edgeList); if(g->color[edge]==0){ g->distance[edge] = g->distance[current]+1; g->color[edge] = 1; g->parent[edge] = current; insertAfterLast(list, edge); } moveNext(edgeList); } deleteFirst(list); g->color[current] = 2; } makeEmpty(list); freeList(list); }
//helper recursive function for DFS int visit(Graph G, List S, int u, int* time, int k){ *time = *time + 1; G->vdiscover[u] = *time; G->vcolor[u]= 'g'; G->vcc[u]=k; moveTo(G->vneighbor[u],0); while (getIndex(G->vneighbor[u]) > -1) { int v; v = getElement(G->vneighbor[u]); if (G->vcolor[v] == 'w') { G->vparent[v] = u; visit(G, S, v, time, k); } moveNext(G->vneighbor[u]); } G->vcolor[u]='b'; *time = *time + 1; G->vfinish[u] = *time; if (getIndex(S) > -1) { insertAfter(S, u); } else { prepend(S, u); } return *time; }
//inserts a new directed edge from u to v, i.e. v is added to the adjacency List of //u (but not u to the adjacency List of v). //Pre: u and v between 1 and getOrder(G) void addArc(Graph G, int u, int v) { if( G == NULL ) { printf("Graph Error: calling addArc() on NULL Graph reference\n"); exit(1); } if((1>u) || (u > getOrder(G)) || (1>v) || (v > getOrder(G))) { printf("Graph Error: calling addArc() on invalid vertex value\n"); exit(1); } //insert v into u: //goes to beginning of u's neighbor list moveTo(G->vneighbor[u],0); //moves to correct stop in the list while((getIndex(G->vneighbor[u]) > -1) && (getElement(G->vneighbor[u]) < v)) { moveNext(G->vneighbor[u]); } //checks if we are off the end of the list if (getIndex(G->vneighbor[u]) == -1) { append(G->vneighbor[u], v); G->size++; } //if not off end of list, insert before current position else { insertBefore(G->vneighbor[u], v); 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; } }
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; }
virtual size_t count() { size_t result = 0; while (moveNext()) ++result; reset(); return result; }
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); }
/* * moveTo * If 0 <= i <= getLength()-1, moves current element marker to * position i in this List. Otherwise current is undefined. */ void moveTo(ListRef L, int i){ int j; L->current = L->front; for (j = 0; j < i; j++){ moveNext(L); } }
// Performs Breadth-first search on the Graph G with the // given source vertex s. void BFS(Graph G, int s) { for(int i = 0; i < (G->order + 1); ++i) { G->parent[i] = NIL; G->distance[i] = INF; G->color[i] = WHITE; } G->source = s; G->distance[s] = 0; G->color[s] = GRAY; List Q = newList(); prepend(Q, s); while(length(Q) > 0) { int ele = back(Q); deleteBack(Q); List adj = G->adj[ele]; moveFront(adj); while(index(adj) > -1) { int v = get(adj); if(G->color[v] == WHITE) { G->color[v] = GRAY; G->parent[v] = ele; G->distance[v] = G->distance[ele] + 1; prepend(Q, v); } moveNext(adj); } } freeList(&Q); }
int main() { int count = 0; int total = 0; int largest = INT_MIN; Element i; LinkedList list = NULL; LL_Iterator it; printf("Digite números para adicionar à lista (CTRL+D para sair): "); while( (scanf("%d",&i) == 1 ) ) { list = prepend(list, i); if( i > largest) largest = i; } printf( "Valor máximo: %d\n", largest); it = getBegin(list); while( it != NULL ) { i = getElement(it); //if( largest % i == 0 ) { total += i; count++; //} it = moveNext(it); } if(count != 0) printf( "Média: %d\n", (total/count)); }
// 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 GoofyLeapImageGallery::detectMovement() { if(leap.getSimpleHands()[0].fingers.size() >= 4) { if(getSingleHandDetected()) { ofPoint handPos = leap.getSimpleHands()[0].fingers[INDEX].tip; float diff = handPos.x - prevHandPos.x; prevHandPos = handPos; if(swipeFree) { if(diff > -swipeRange.y && diff < -swipeRange.x) { if(actualImageCount < urlImages.size() - 1) movePrev(); } if(diff > swipeRange.x && diff < swipeRange.y) { if(actualImageCount > 0) moveNext(); } } } } }
// prints the list in it's entirety void printList(ListRef L) { L->current = L->first; while(L->current != NULL) { printf("%7d: %ld\n", getLength(L), getCurrent(L)); moveNext(L); } }
Array<T> toArray() { Array<T> result; while (moveNext()) result.push_back(current()); reset(); return result; }
/** * Altera o valor do e-nésimo elemento da lista, contando a partir * do 0. Tem complexidade O(n). */ void setNth(List list, int n, ItemType newItem) { Iterator it = getBegin(list); int i; for( it = 0; i < n; i++) { moveNext(it); } setValue(it,newItem); }
/** * Retorna o e-nésimo elemento da lista, contando a partir do 0. * Tem complexidade O(n). */ ItemType getNth(List list, int n) { Iterator it = getBegin(list); int i; for( i = 0; i < n; i++) { moveNext(it); } return getValue(it); }
/** * Altera o valor do e-nésimo elemento da lista, contando a partir * do 0. Tem complexidade O(n). */ void setNth(LinkedList list, int n, Element newElement) { LL_Iterator it = getBegin(list); int i; for( it = 0; i < n; i++) { moveNext(it); } setValue(it,newElement); }
/** * Retorna o e-nésimo elemento da lista, contando a partir do 0. * Tem complexidade O(n). */ Element getNth(LinkedList list, int n) { LL_Iterator it = getBegin(list); int i; for( it = 0; i < n; i++) { moveNext(it); } return getValue(it); }
void terrama2::core::DataStoragerTiff::store(DataSetSeries series, DataSetPtr outputDataSet) const { if(!outputDataSet.get() || !series.syncDataSet.get()) { QString errMsg = QObject::tr("Mandatory parameters not provided."); TERRAMA2_LOG_ERROR() << errMsg; throw DataStoragerException() << ErrorDescription(errMsg); } QUrl uri(QString::fromStdString(dataProvider_->uri)); auto path = uri.path().toStdString(); try { std::string mask = getMask(outputDataSet); auto dataset = series.syncDataSet->dataset(); size_t rasterColumn = te::da::GetFirstPropertyPos(dataset.get(), te::dt::RASTER_TYPE); if(!isValidColumn(rasterColumn)) { QString errMsg = QObject::tr("No raster attribute."); TERRAMA2_LOG_ERROR() << errMsg; throw DataStoragerException() << ErrorDescription(errMsg); } size_t timestampColumn = te::da::GetFirstPropertyPos(dataset.get(), te::dt::DATETIME_TYPE); dataset->moveBeforeFirst(); while(dataset->moveNext()) { std::shared_ptr<te::rst::Raster> raster(dataset->isNull(rasterColumn) ? nullptr : dataset->getRaster(rasterColumn).release()); std::shared_ptr<te::dt::DateTime> timestamp; if(!isValidColumn(timestampColumn) || dataset->isNull(timestampColumn)) timestamp = nullptr; else timestamp.reset(dataset->getDateTime(timestampColumn).release()); if(!raster.get()) { QString errMsg = QObject::tr("Null raster found."); TERRAMA2_LOG_ERROR() << errMsg; continue; } std::string filename = replaceMask(mask, timestamp, outputDataSet); std::string output = path + "/" + filename; te::rp::Copy2DiskRaster(*raster, output); } } catch(const DataStoragerException&) { throw; } catch(...) { //TODO: fix DataStoragerTiff catch } }
/* 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); } }