//function to add a word to list void listAdd(char string[], list *theList) { node *search = listSearch(string, theList); //check if word appeared before if(search!=NULL) { //if word was there increast count search->count = search->count+1; return; } else if(search==NULL) { //if word not there node *p = (node*)malloc(sizeof(node)); //create new node for word if(p==NULL) { printf("\nMemory error! Abort\n"); exit(-1); } p->words = (char*)malloc(strlen(string)+1); //allocate space for string if(p->words==NULL) { printf("\nMemory error! Abort\n"); exit(-1); } strcpy(p->words, string); //store words to list p->next = NULL; p->count = 1; //increase word count to 1 theList->size++; //record new size of list if(theList->end==NULL) //check if word is 1st element theList->start = theList->end = p; else { theList->end->next = p; theList->end = p; } } }
pList listSearch(pList list, int uid) { if(list->level==0) return 0; else { pList result = 0; if(uid < list->uid) result = listSearch(list->left, uid); else if(uid > list->uid) result = listSearch(list->right, uid); else if(uid == list->uid) result = list; return result; } }
char * intProccess(char * s) {/*{{{*/ StrInputStream ssin; // StrOutputStream ssout; LinkedList * l = createLinkedList(sizeof(int), intGreater); int x; initStrInputStream(&ssin, s); initStrOutputStream(&ssout, 250); // printf("%d\n", (ssout.length)); while (!sisEof(&ssin)) { readInt(&ssin, &x); if (listSearch(l, &x) == l->nil) { ListNode * a = l->nil->next; while (a != l->nil && *((int *)(a->key)) < x) { a = a->next; } listInsert(l, a, &x); } } listTravers(l, putInt); clrListedList(l, NULL); writeString(&ssout, "\n"); return ssout.begin; }/*}}}*/
//Sells an Item, returns true if successful bool itemSell(Player *p, Item *i){ if(listSearch(&p->inventory, i)){ listRemoveByValue(&p->inventory, i); p->money += i->prize*2/3; updateItems(p); return true; } return false; }
int main() { int i; LINKED_LIST myListHead=getNode(); myListHead->next=NULL; for(i=0;i<10;i++) { LINKED_LIST tempNode=getNode(); tempNode->key=i; insertAtHead(myListHead,tempNode); } printLinkedList(myListHead); LINKED_LIST searchElement; searchElement=getNode(); searchElement=listSearch(myListHead,4); printf("\n\nSearchedElement:%d\n",searchElement->key); LINKED_LIST lastNode; searchElement=getNode(); searchElement=findLastNode(myListHead); printf("\n\nLastNode:%d\n",searchElement->key); LINKED_LIST toDeleteElement; toDeleteElement=getNode(); toDeleteElement=listSearch(myListHead,6); int deletedElement=deleteNode(myListHead,toDeleteElement); printf("\nElementDeleted=%d\n",deletedElement); printLinkedList(myListHead); return 0; }
GEdge *graphGetLink(GVertex *source, GVertex *destination) { NodeList *p; if (!source->LAdjacents || listIsEmpty(source->LAdjacents)) return(NULL); p = listSearch(source->LAdjacents,destination,gEdgeCmpxDestination); if (!p) return(NULL); return((GEdge*)nodeListGetCont(p)); }
void graphUnlinkVertices(Graph *G, GVertex *source, GVertex *destination) { NodeList *pnodo_arco; if(!source->LAdjacents) return; pnodo_arco = listSearch(source->LAdjacents, destination,gEdgeCmpxDestination); if(pnodo_arco){ listRemoveNode(source->LAdjacents, pnodo_arco); // free(pnodo_arco->cont); //nodeListDelete(&pnodo_arco); } }
void performSearchTest(void) { unsigned elem2Search; struct CElem *p; for (elem2Search = 0; elem2Search < 2*MAX; elem2Search++) { printf("Търсим елемент с ключ %u.\n", elem2Search); if (NULL == (p = listSearch(elem2Search))) printf("%s","Елемент с такъв ключ не съществува!\n"); else printf("%Елементът е намерен! Стойност на инф. част: %d\n", p->data); } }
NodeList *graphRemoveVertex(Graph *G, GVertex *V, cmpfn cmp) { NodeList *pnodovertice; // listDelete(&V->LAdjacents); for(pnodovertice = listGetHeader(G); pnodovertice!=NULL; pnodovertice = nodeListGetNext(pnodovertice)){ graphUnlinkVertices(G, (GVertex*)nodeListGetCont(pnodovertice), V); } pnodovertice = listSearch(G, V, cmp); listRemoveNode(G, pnodovertice); return pnodovertice; }
int main() { Node head = nodeCreate(1); printf("1. head is %d\n\n\n\n", head->num); printf("2. The list and the linked list is now:\n"); int array[] = {10, 3, 10, 7, 2, 11, 5, 9, 11, 6, 0, 6, 11, 6}; for(int i = 0; i < 9; i++) { head = listAppend(head, array[i]); } Node item = head; while(item->next != NULL) { printf("%d ", item->num); item = item->next; } printf("%d\n\n\n\n", item->num); printf("3. Now we are testing the nodDel() function.\n"); Node target = listSearch(head, 1); head = nodeDel(head, target); printf("4.\n"); // printf("3. Now we are testing the delDups() function.\n"); // head = delDups(head); item = head; if(head == NULL) { printf("The list is empty"); } else { while(item->next != NULL) { printf("%d ", item->num); item = item->next; } printf("%d\n\n\n\n", item->num); } return 0; }
int main(int argc, char* argv[]) { head_ptr ls_head = (head_ptr)malloc(sizeof(head)); ls_head->head = NULL; list ele_x1; ele_x1.key = 1; list ele_x2; ele_x2.key = 4; list ele_x3; ele_x3.key = 16; list ele_x4; ele_x4.key = 9; listInsert(ls_head, &ele_x1); listInsert(ls_head, &ele_x2); listInsert(ls_head, &ele_x3); listInsert(ls_head, &ele_x4); printList(ls_head); list_ptr ele_find = listSearch(ls_head, 4); if (ele_find != NULL) printf("find element of key in 4\n"); else printf("find not element of key in 4\n"); list ele_x5; ele_x5.key = 25; listInsert(ls_head, &ele_x5); printList(ls_head); if (ele_find != NULL) { listDelete(ls_head, ele_find); printList(ls_head); } free(ls_head); ls_head = NULL; return 0; }
int main() { Node head = nodeCreate(1); printf("1. head is %d\n\n\n\n", head->num); printf("2. The list and the linked list is now:\n"); int array[] = {10, 3, 10, 7, 2, 9, 5, 9, 11, 6, 0, 6, 11, 6}; for(int i = 0; i < 9; i++) { head = listAppend(head, array[i]); } Node item = head; while(item->next != NULL) { printf("%d ", item->num); item = item->next; } printf("%d\n\n\n\n", item->num); Node target = listSearch(head, 11); _Bool isDel = NodeDelWithNoReturnValue(target); if(isDel == true) { item = head; while(item->next != NULL) { printf("%d ", item->num); item = item->next; } printf("%d\n\n\n\n", item->num); } else { printf("can not delete\n"); } return 0; }
// . find the indirect matches in the list which match a sub path // of the url long Catdb::getIndirectMatches ( RdbList *list , Url *url , char **matchRecs , long *matchRecSizes , long maxMatches , char *coll, long collLen) { char path[MAX_URL_LEN+1]; long pathLen; Url partialUrl; key_t partialUrlKey; // start with the whole url...include real catid in indirect memcpy(path, url->getUrl(), url->getUrlLen()); pathLen = url->getUrlLen(); // loop looking for partial matches char *data = NULL; long dataSize = 0; long numMatches = 0; while ( numMatches < maxMatches ) { // make the partial url partialUrl.set(path, pathLen, true); normalizeUrl(&partialUrl, &partialUrl); // make the next key partialUrlKey = makeKey ( &partialUrl, false ); // search for it listSearch ( list, partialUrlKey, &data, &dataSize ); // store a hit if ( data && dataSize > 0 ) { // get the url char *x; long xlen; // hit, check the url // for catdb, skip over the catids /* unsigned char numCatids = *data; // . point to stored url/site // . skip dataSize/fileNum long skip = 1 + (4 * numCatids) + 4; x = data + skip; xlen = dataSize - skip; */ CatRec sr; sr.set ( url, data, dataSize, false); x = sr.m_url; xlen = sr.m_urlLen; // ensure it's a sub-path if ( xlen <= url->getUrlLen() && strncasecmp(x, url->getUrl(), xlen) == 0 ) { //char msg[4096]; //char *mp = msg; //mp += sprintf(mp, "For "); //memcpy(mp, url->getUrl(), url->getUrlLen()); //mp += url->getUrlLen(); //mp += sprintf(mp, " , got Indirect: "); //memcpy(mp, x, xlen); //mp += xlen; //*mp = '\0'; //log ( LOG_INFO, "tagdb: %s", msg ); matchRecs [numMatches] = data; matchRecSizes[numMatches] = dataSize; numMatches++; } } // make the next partial url pathLen--; while ( pathLen > 3 && path[pathLen-1] != '/' ) pathLen--; // check for end if ( pathLen <= 3 || strncmp(&path[pathLen-3], "://", 3) == 0 ) break; // chop off the trailing / pathLen--; } return numMatches; }
// now given an RdbList of SiteRecs can we find the best matching rec // for our site? char *Catdb::getRec ( RdbList *list , Url *url , long *recSize, char* coll, long collLen ) { key_t exactKey; long long startTime = gettimeofdayInMilliseconds(); long long took; char *data; long dataSize; // for now, only get exact hits for catdb // check for an exact key/url match exactKey = makeKey(url, false); // go throught the list looking for the exact key //list->resetListPtr(); data = NULL; dataSize = 0; // call the search listSearch ( list, exactKey, &data, &dataSize ); // make sure the url matches if (data && dataSize > 0) { // get the url /* char *x; long xlen; // hit, check the url // for catdb, skip over the catids if (m_rdbid == RDB_CATDB) { unsigned char numCatids = *data; // . point to stored url/site // . skip dataSize/fileNum long skip = 1 + (4 * numCatids) + 4; x = data + skip; xlen = dataSize - skip; } else { // . point to stored url/site // . skip dataSize/fileNum x = data + 4; xlen = dataSize - 4; } // set the site Url site; site.set ( x , xlen , false ); */ CatRec site; site.set (url, data, dataSize, false); // check for an exact match against the full url long uflen = url->getUrlLen(); char *ufull = url->getUrl(); //long sflen = site.getUrlLen(); //char *sfull = site.getUrl(); long sflen = site.m_urlLen; char *sfull = site.m_url; // if we match, return this rec if ( sflen == uflen && strncmp ( sfull, ufull, sflen ) == 0 ) { *recSize = dataSize; } else { *recSize = 0; data = NULL; } } else { *recSize = 0; data = NULL; } took = gettimeofdayInMilliseconds() - startTime; if ( took > 10 ) log(LOG_INFO, "catdb: catdb lookup took %lli ms, " "listSize=%li", took, list->getListSize() ); return data; }
void deleteKey(int key) { deleteNode(listSearch(key)); }
int main(int argc,char **argv) { list_t list; int index; char *arg; int elements; /* Initialize our list. */ listInitialize(&list); /* Fill our list with all command line arguments given, but skip the * first argument since that's our needle (see below -> listSearch()). */ for(index=2;index<argc;index++) { /* Append the argument to our list. */ if(listAppend(&list,(void *)argv[index])!=listRtrnOk) { fprintf(stderr,"Can't create item!\n"); break; } /* Advance to this next position. By default listAppend() does not * advance our current pointer to the appended element. */ listNext(&list); } /* Print all our arguments from the first to the last. */ printf("First to last:\n"); /* Does any argument exist? */ if((arg=(char *)listFirst(&list))!=NULL) { /* Yes, we print it and... */ do { printf("[%s]\n",arg); /* ...advance to the next argument, untill none left. */ } while((arg=(char *)listNext(&list))!=NULL); } else { printf("empty list\n"); } /* Print all our arguments from the last to the first. */ printf("Last to first:\n"); /* Does any argument exist? */ if((arg=(char *)listLast(&list))!=NULL) { /* Yes, we print it and... */ do { printf("[%s]\n",arg); /* ...advance to the next argument, untill none left. */ } while((arg=(char *)listPrevious(&list))!=NULL); } else { printf("empty list\n"); } /* If there do exist enough arguments... */ if(argc>=2) { /* ...search for the first one given... */ arg=listSearch(&list,cmpr,(void *)argv[1]); /* ...and tell if it's found. */ printf("searching for [%s]... %sfound\n",argv[1],arg!=NULL?"":"not "); } /* Randomly remove half of the arguments. */ srandom(getpid()); for(elements=listElements(&list)/2;elements>0;elements--) { int element=(random()%(argc-2))+2; arg=listSearch(&list,cmpr,(void *)argv[element]); printf("Removing [%s]... ",argv[element]); if(arg!=NULL) { listRemove(&list,NULL); printf("ok\n"); } else { printf("not in the list!\n"); } } /* Print all our arguments from the first to the last. */ printf("First to last:\n"); /* Does any argument exist? */ if((arg=(char *)listFirst(&list))!=NULL) { /* Yes, we print it and... */ do { printf("[%s]\n",arg); /* ...advance to the next argument, untill none left. */ } while((arg=(char *)listNext(&list))!=NULL); } else { printf("empty list\n"); } /* Print all our arguments from the last to the first. */ printf("Last to first:\n"); /* Does any argument exist? */ if((arg=(char *)listLast(&list))!=NULL) { /* Yes, we print it and... */ do { printf("[%s]\n",arg); /* ...advance to the next argument, untill none left. */ } while((arg=(char *)listPrevious(&list))!=NULL); } else { printf("empty list\n"); } /* Destroy the list and all its elements. */ listDestroy(&list,garbageCollect); return(0); }