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); }
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 deleteX(int x) { node* tempNode = ALLOC_NODE(); if(ourList->head->data == x) deleteFirst(); if(ourList->tail->data == x) deleteLast(); tempNode = ourList->head; int i; for(i=0; i < ourList->length; i++) { tempNode = tempNode->next; if(tempNode->data == x) { tempNode->prev->next = tempNode->next; tempNode->next->prev = tempNode->prev; node* auxNode = ALLOC_NODE(); auxNode = tempNode; tempNode = tempNode->next; free(auxNode); tempNode = tempNode->prev; } } }
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; }
int main() { Stack stk; stk.top = 0; int n = 0; scanf("%d", &n); int i = 0; int temp; while (i < n) { scanf("%d", &temp); push(&stk, temp); i++; } int x = 0; scanf("%d", &x); if (contains(&stk, x) == 1) { printf("YES\n"); } else { printf("NO\n"); } int position = deleteFirst(&stk, x); while (stk.top != 0) { printf("%d ", pop(&stk)); } printf("\n%d", position); 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 deleteElementX(int x) { node *p=list->head; if (p->data==x) // If x is equal with the data in the first node we use the function deletefirst. { deleteFirst(); } else { while ((p!=NULL)&& (p!=list->tail)) { p=p->next; } if (p->data==x) { if (p==list->tail)// if it coincide with the tail we use deleteLast. deleteLast(); else { p->next->prev=p->prev; p->prev->next=p->next; // We reassemble the list. free(p); list->length--; } } } }
void deleteX(int x) { node* curr=(node*)malloc(sizeof(node)); if(sent->head->data==x) deleteFirst(); if(sent->tail->data==x) deleteLast(); if(sent->tail==sent->head && sent->head->data==x) { free(sent->head); sent->tail=NULL; sent->head=NULL; } curr=sent->head; int i; for(i=0; i<sent->len; i++) { curr=curr->next; if(curr->data==x) { curr->prev->next = curr->next; curr->next->prev = curr->prev; node* aux = (node*)malloc(sizeof(node)); aux= curr; curr = curr->next; free(aux); curr = curr->prev; } } }
void deleteCurrent(ListRef L) { if (L == NULL) { printf( "Error: Called deleteCurrent() on NULL ListRef\n"); //exit(EXIT_FAILURE); } else if (isEmpty(L)) { printf( "Error: Called deleteCurrent() on empty ListRef\n"); //exit(EXIT_FAILURE); } else if (offEnd(L)) { printf( "Error: Called deleteCurrent() on ListRef without current \n"); //exit(EXIT_FAILURE); } if (L->current == L->first) { deleteFirst(L); } else if(L->current == L->last) { deleteLast(L); } else { if (L->current->next != NULL) { L->current->next->prev = L->current->prev; } if (L->current->prev != NULL) { L->current->prev->next = L->current->next; } L->current->next = NULL; L->current->prev =NULL; freeNode(&(L->current)); L->length--; } L->current = NULL; }
void deleteAtPosition(node **head, int pos) { int len = size(head); if (pos <= 0 || pos > (len)) { printf("Position is not Valid\n"); return; } if(pos == 1) { deleteFirst(head); } else if(pos == (len)) { deleteLast(head); } else { // execution reach in this location if position is between 2 and len - 1 node *curr = (*head)->next; node *prev = *head; int i = 2; // start from 2nd node while(i != pos) { i ++; prev = curr; curr = curr->next; } prev->next = curr->next; // curr points to old node at pos free(curr); } // size--; }
int DList_deleteData(DList* list , int index){ if(index >= list->length) return 0; if(index == 0) deleteFirst(list); else if(index == list->length-1) deleteLast(list); else deleteMiddle( list , index); list->length--; return 1; };
void management::kick(std::vector< std::string > eventData) { output::instance().addOutput("void management::kick(std::vector< std::string > eventData)", 11); std::string channelName = eventData[2]; deleteFirst(channelName, ":"); std::string userName = eventData[3]; leaveChannel(channelName, userName); }
NodeT* TreeFromList(NodeL** head) { NodeT* p; if(strcmp((*head)->data, "*")==0) { deleteFirst(head); return NULL; } else { p=createNodeT(atoi((*head)->data)); deleteFirst(head); p->left=TreeFromList(head); p->right=TreeFromList(head); } return p; }
void management::part(std::vector< std::string > eventData) { output::instance().addOutput("void management::part(std::vector< std::string > eventData)", 11); std::string channelName = eventData[2]; deleteFirst(channelName, ":"); std::string userName = eventData[0]; nickFromHostmask(userName); leaveChannel(channelName, userName); }
/* empty the list, free up the nodes */ void makeEmpty(ListRef L) { if (L == NULL) { printf( "Error: Called makeEmpty() on NULL ListRef\n"); //exit(EXIT_FAILURE); } while (!isEmpty(L)) { deleteFirst(L); } }
/** \brief Remove the first item of a list * * \param[in,out] head Pointer to a list * \param[in] freeFunc Function pointer for freeing the memory of the list item * * Removes the first element of the given list and frees all associated memory. * If freeFunc is NULL, it tries to directly free the data of each node. * For complex nodes (e.g. structs) the user needs to provide a node-specific free() function. */ void removeFirst(NODE **head, listFuncPtr freeFunc) { dbg_printf(P_DBG, "Removing first item..."); if (*head != NULL) { if(freeFunc) { freeFunc((*head)->data); } else { am_free((*head)->data); } deleteFirst(head); } }
int main() { sent = (list*)malloc(sizeof(list)); sent->head = NULL; sent->tail = NULL; sent->len = 0; FILE* fin= fopen("input.txt", "r"); char caz[20]; int n; while(fscanf(fin, "%s", caz)>0) { if(strcmp(caz, "AF")==0) { fscanf(fin, "%d", &n); addFirst(n); } else if(strcmp(caz, "AL")==0) { fscanf(fin, "%d", &n); addLast(n); } else if(strcmp(caz, "DF")==0) deleteFirst(); else if(strcmp(caz, "DL")==0) deleteLast(); else if(strcmp(caz, "DOOM_THE_LIST")==0) doomTheList(); else if(strcmp(caz, "DE")==0) { fscanf(fin, "%d", &n); deleteX(n); } if(strcmp(caz, "PRINT_ALL")==0) printList(); else if(strcmp(caz, "PRINT_F")==0) { fscanf(fin, "%d", &n); printFirstX(n); } else if(strcmp(caz, "PRINT_L")==0) { fscanf(fin, "%d", &n); printLastX(n); } } fclose(fin); return 0; }
/*This method deletes all the nodes from the list*/ void makeEmpty(ListHndl L) { if(L == NULL) { printf("Error: List is already empty.\n"); exit(1); } while(!isEmpty(L)) { deleteFirst(L); } }
// int size = 0; int main() { //Initialize Crcular linked list with NULL head node *head = NULL; int data = 10; // printf("Enter: "); // scanf("%d",&data); insertLast(&head, data); insertFirst(&head, 5); print(&head); printf("Size: %d\n", size(&head)); printf("Insert at position 1\n"); insertAtPosition(&head, 1,1); print(&head); printf("Size: %d\n", size(&head)); printf("Insert at position 4\n"); insertAtPosition(&head, 15,4); print(&head); printf("Size: %d\n", size(&head)); deleteFirst(&head); print(&head); printf("Size: %d\n", size(&head)); deleteLast(&head); print(&head); printf("Size: %d\n", size(&head)); deleteAtPosition(&head,2); print(&head); printf("Size: %d\n", size(&head)); deleteAtPosition(&head,1); print(&head); printf("Size: %d\n", size(&head)); printf("New Circular linked list\n"); node *head1 = NULL; createList(&head1); print(&head1); printf("Enter data want to search: "); scanf("%d", &data); search(&head1,data); printf("Sort a circular linked list\n"); sort(&head1); print(&head1); }
/* remove all elements from list */ void deleteAll(ListD *L) { if (L == NULL) { perror("Invalid list!"); return; } while (L->head != NULL) { deleteFirst(L); } }
int main() { fn(); node* list = NULL; insertAtStart(&list,2); insertAtStart(&list,3); insertAtStart(&list,4); insertAtStart(&list,5); traverseForward(list); deleteFirst(&list); traverseForward(list); return 0; }
int deleteList (list_t list) { if (list == NULL) { // list safety return 0; }else { //while (deleteFirst()) {;} while (list->length >= 1) { deleteFirst(list); } free(list); list = NULL; return 1; }// end list safety }
// deletes the elements in the current node void deleteCurrent(ListRef L) { if(L->first==L->last) makeEmpty(L); if(!isEmpty(L)) { if(L->current==L->last) deleteLast(L); else if(L->current==L->first) deleteFirst(L); else { L->current->next->prev=L->current->prev; L->current->prev->next=L->current->next; } } }
bool management::nickFromHostmask(std::string& data) { std::vector< std::string > who; who = glib::split(data); if (deleteFirst(who[0], ":")) { size_t pos; pos = who[0].find("!"); data = who[0].substr(0, pos); return true; } else { return false; } }
int List::deleteAt(int pos) { if (pos <= 0) return deleteFirst(); else if (pos >= getSize()) return deleteLast(); else { ListItem *del = first; while (pos-- > 0) { del = del->getNext(); } int val = del->getContent(); delete del; currentSize--; return val; } }
void deleteAfter(List &L, address &P, address &Prec) { if(Prec==NULL) { cout<<"Data TIDAK DITEMUKAN"<<endl; } else if (Prec==First(L)) { deleteFirst(L,P); } else { P = Next(Prec); Next(Prec) = Next(P); Prev(Next(P))=Prec; Next(P) = NULL; Prev(P) = NULL; } }
int main() { printf("start of program: "); heapReport(); FILE *inputFile = openInputFile("input.txt"); FILE *outputFile = openOutputFile("output.txt"); LinkedList list; // Read 10 integers from the input file, adding them // alternately to the beginning and end of the list. // After each change to the list, write the contents of the // list to the output file. fprintf(outputFile, "CREATING LIST:\n"); int count; for (count = 1; count <= 5; count++) { list = readAndAddToStart(list, inputFile); printList(list, outputFile); list = readAndAddToEnd(list, inputFile); printList(list, outputFile); } // end for // Show heap space used on standard input printf("after creating list: "); heapReport(); fprintf(outputFile, "\nWHILE DELETING FROM LIST:\n"); int start = 1; // 1 (true) means addint to start while (list != NULL) { if (start) list = deleteFirst(list); else list = deleteLast(list); start = !start; printList(list, outputFile); } // end for // Show heap space again; if delete functions have freed space // it should be zero. printf("after emptying list: "); heapReport(); fclose(inputFile); fclose(outputFile); printf("after closing files: "); heapReport(); return 0; } // end main
void deleteElementByKey(int x) { //the previous node to the one that has to be deleted will be memorized NODE *prevNode=NULL; int found=0; if (head!=NULL) { NODE *currentNode,*aux; currentNode=head; //the list is traversed up to the node which has to be deleted while ((!found)&&(currentNode!=NULL)) { //the case in which the node to be deleted is a head or a tail are taken into consideration if (currentNode->data==x) { if (currentNode==head) { deleteFirst(); found=1; } else { if (currentNode==tail) { deleteLast(); found=1; } else { aux=currentNode; prevNode->next=currentNode->next; currentNode=prevNode->next; free(aux); found=1; } } } else { //the previous node is linked to the element following the one due to be deleted prevNode=currentNode; currentNode=currentNode->next; } } } }
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--; } } }