int deleteMiddle(List* l, int position) { // position invalid if(l->nElements <= position || position < 0){ printf("it is not possible to delete in position %d \n",position); return FALSE; } else{ // posisiton valid if(position == 0){ //delete in the first element deleteFront(l); return TRUE; } int i = 0; Node* ant; Node* aux; aux = l->head; do{ ant = aux; aux = aux->next; i++; }while(i < position); ant->next = aux->next; free(aux); l->nElements--; return TRUE; } }
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; }
bool ServerClientList::removeClient( PlayerID client_id ) { ServerClientListData *client_data_ptr; if ( front == 0 ) return( false ); client_data_ptr = front; if ( client_data_ptr->client_id == client_id ) { deleteFront(); return( true ); } while( client_data_ptr->next != 0 ) { if ( client_data_ptr->next->client_id == client_id ) { deleteAfter( client_data_ptr ); return( true ); } client_data_ptr = client_data_ptr->next; } return( false ); }
void llist::deleteIth(int I, el_t& Old) { // case(exception) // --------------- if(I < 1 || I > Count) throw OutOfRange(); // case(I is the front) // -------------------- else if(I == 1) deleteFront(Old); // case(I is the rear) // ------------------- else if(I == Count) deleteRear(Old); // case(I is any node between front and rear) // ------------------------------------------ else { Node *temp = Front; Node *del; for(int j = 1; j < I - 1; j++) temp = temp->Next; del = temp->Next; Old = del->Elem; temp->Next = del->Next; delete del; Count--; } }
//runs the DFS algorithm on the Graph G, using the List S to order the vertices //and store the stack of finished vertices. //Pre: Length of S = order of G, S has all the correct numbers in it(does not check this) void DFS(Graph G, List S){ if( G == NULL ) { printf("Graph Error: calling DFS() on NULL Graph reference\n"); exit(1); } if( S == NULL ) { printf("Graph Error: calling DFS() on NULL List reference\n"); exit(1); } if( length(S) != getOrder(G) ) { printf("Graph Error: calling DFS() on wrong length list\n"); exit(1); } for(int i = 1; i <= getOrder(G); i++) { G->vcolor[i]='w'; G->vparent[i]=NIL; } int time; time =0; int k; k=0; moveTo(S, getOrder(G)-1); for(int i = 1; i <= getOrder(G); i++) { int u; u = front(S); deleteFront(S); if (G->vcolor[u] == 'w'){ k=k+1; time = visit(G, S, u, &time, k); G->numcc=k; } } }
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); }
/* DFS() * Pre: List S length == Number of vetices in G * Pos: finds the strongly connected components */ void DFS(GraphRef G, ListRef S){ if(G == NULL){ fprintf(stderr,"Graph Error: calling DFS on NULL Graph"); exit(1); } if(getLength(S) != getOrder(G)){ fprintf(stderr,"DFS Error: ListRef size != Graph size"); exit(1); } int i; int u; int j; for(i=1; i<=getOrder(G);i++){ G->color[i] = WHITE; G->parent[i] = NIL; } j =1; moveTo(S,getLength(S)-1); while(!offEnd(S) && j<=getOrder(G)){ u = getFront(S); if(G->color[u] == WHITE){ Visit(G,S,u); } deleteFront(S); j++; } }
// clear() // Removes every entry in the List void clear(List L) { if (L == NULL) { printf("List Error: clear() called on NULL List reference\n"); exit (1); } else { while (length(L)!=0) {deleteFront(L);} } }
// freeList() // Frees all heap memory associated with List *pL, and sets *pL to NULL.S void freeList(List* pL) { if( pL != NULL && *pL != NULL ){ while( !isEmpty(*pL) ){ deleteFront(*pL); } myFree(*pL); *pL = NULL; } }
void freeList(ListRef* pL){ if(pL!=NULL && *pL==NULL) {return;} while( !isEmpty(*pL)) { deleteFront(*pL); } free(*pL); *pL = NULL; }
// freeList() // Frees all heap memory associated with List *pL, and sets *pL to NULL. void freeList(List* pL){ if(pL!=NULL && *pL!=NULL) { while (length(*pL) > 0) { deleteFront(*pL); } free(*pL); *pL = NULL; } }
void revList(List L){ if(length(L) == 0){ return; } int tmp = front(L); deleteFront(L); revList(L); append(L,tmp); }
// clear() // resets list back to empty state. void clear(List L) { while(L->length >= 1) { deleteFront(L); } L->cursor = NULL; L->index = -1; }
void deleteItem() { struct node *before; struct node *search; struct node *current; int countbefore=1; int countafter=0; int countlist=0; int countitem=0; int item=10; while(item<0||item>9) { printf("Enter number to be deleted: "); scanf("%d",&item); } current=head; while(current->next!=NULL) { current=current->next; countlist++; } //printf("%d\n",countlist); search=head; while(item!=search->data) //search for the node to be deleted { search=search->next; countitem++; } if(countitem==0) //if ang e delete kay naa sa pinaka una { deleteFront(); } else if(countitem==countlist) // if ang e delete kay naa sa pinaka tomoy { deleteRear(); } else { before=head; while(countbefore<countitem) //point the before pointer to the node before the node to be deleted { before=before->next; countbefore++; } current=before->next->next; before->next=current; //delete the node to be deleted from the list } }
/* * makeEmpty() * Sets L to the empty state. * Post: isEmpty(L). */ void makeEmpty(ListRef L) { if ( L == NULL ) { printf("List Error: calling makeEmpty() on NULL ListRef\n"); exit(1); } if ( L->front == NULL) { return; } while( !isEmpty(L) ) { deleteFront(L); } }
// clears the list void clear(List L) { if ( L == NULL ) { printf("List Error: clear() called on NULL list\n"); exit(1); } if ( L->front == NULL) { return; } while( !isEmpty(L) ) { deleteFront(L); } }
// Re-sets List L to the empty state. void clear(List L) { if(L==NULL) { printf("List Error: calling clear() on NULL List reference\n"); exit(1); } if(!isEmpty(L)){ int k = length(L); for(int i = 0; i < k; i++){ deleteFront(L); } } }
// Resets this List to its original empty state. // Pre: none void clear(List L){ if( L==NULL ){ printf("List Error: calling clear() on NULL List reference"); exit(1); } while( L->front!=NULL ){ deleteFront(L); } //Function clear() should have a loop that calls deleteFront() or // deleteBack() while the list is not empty. L->front = NULL; L->back = NULL; L->cursor = NULL; L->length = 0; L->index = -1; }
//runs the BFS algorithm on the Graph G with source s, setting the color, distance, //parent, and source fields of G accordingly. void BFS(Graph G, int s) { if( G == NULL ) { printf("Graph Error: calling BFS() on NULL Graph reference\n"); exit(1); } if((1>s) || (s > getOrder(G))) { printf("Graph Error: calling BFS() on invalid vertex value\n"); exit(1); } //start actual BFS algorithm for (int i = 1; i <= getOrder(G); i++) { G->vcolor[i] = 'w'; G->vdistance[i] = INF; G->vparent[i] = NIL; } G->source = s; G->vcolor[s]= 'g'; G->vdistance[s] = 0; List Q; Q = newList(); append(Q,s); while (length(Q) != 0) { moveTo(Q,0); int u; u = getElement(Q); deleteFront(Q); moveTo(G->vneighbor[u],0); while (getIndex(G->vneighbor[u]) != -1) { int v = getElement(G->vneighbor[u]); if(G->vcolor[v] == 'w') { G->vcolor[v] = 'g'; G->vdistance[v] = G->vdistance[u] + 1; G->vparent[v] = u; append(Q, v); } moveNext(G->vneighbor[u]); } G->vcolor[u] = 'b'; } freeList(&Q); }
int main(int argc, char* argv[]){ FILE *out; out = fopen(argv[2], "w"); List A = newList(); List B = newList(); List C = NULL; int i; for(i=1; i<=5; i++){ append(A,i); prepend(B,i); } printList(out,A); printf("\n"); printList(stdout,B); printf("\n"); for(moveTo(A,0); getIndex(A)>=0; moveNext(A)){ printf("%d ", getElement(A)); } printf("\n"); for(moveTo(B,length(B)-1); getIndex(B)>=0; movePrev(B)){ printf("%d ", getElement(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"); printf("A's front = %d\n", front(A)); printf("A's back = %d\n", back(A)); deleteFront(A); deleteBack(A); printf("A's front = %d\n", front(A)); printf("A's back = %d\n", back(A)); moveTo(A,4); insertBefore(A,-1); moveTo(A,3); insertAfter(A,-2); moveTo(A,2); delete(A); printList(stdout,A); printf("\n"); // printf("%d\n", length(A)); clear(A); printf("%d\n", length(A)); printf("entering A \n"); freeList(&A); //clear(B); printf("A free, entering B \n"); freeList(&B); //clear(C); printf("A/B free, entering C \n"); freeList(&C); fclose(out); return(0); }
llist::~llist() { char placeholder; while(!isEmpty()) deleteFront(placeholder); }
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); }
void BFS ( Graph G, int s ) { // pre-condition if ( G == NULL ) { printf ( "Graph Error: BFS() on NULL graph" ); exit(1); } // pre-condition if ( s < 1 || s > getOrder(G) ) { printf ( "Graph Error: s undefined" ); exit(1); } // this part of the code was strictly from the textbook // on page 595 from section 22.2. Also mentioned in pa4 pdf // received help from a cs student G->source = s; int i, j, k; for ( i = 1; i <= getOrder(G); i++ ) { G->color[i] = WHITE; G->d[i] = INF; G->p[i] = NIL; } G->color[s] = GRAY; G->d[s] = 0; G->p[s] = NIL; List L = newList(); // Q != /0 append(L, s); // received help on this while loop from a student while( length(L) != 0) { j = front(L); deleteFront(L); moveTo(G->adjacency[j], 0); while(getIndex(G->adjacency[j]) != -1) { k = getElement(G->adjacency[j]); // need help with the for loop // ask professor if(G->color[k] == WHITE) { G->color[k] = GRAY; G->d[k] = (getDist(G, j) +1); G->p[k] = j; append(L, k); } moveNext(G->adjacency[j]); } G->color[j] = BLACK; } // free the list after use freeList(&L); }
// freeList() // Frees all heap memory associated with List *pL, and sets *ppL to NULL void freeList(List* pL) { if (pL == NULL || *pL == NULL) {return;} while (length(*pL) != 0) {deleteFront(*pL);} free(*pL); *pL = NULL; }
void askUser() { int num; int x=10; int i; char choices; system("cls"); printf("\n[1]-DISPLAY ARRAY\n[2]-INSERT REAR\n[3]-INSERT FRONT\n[4]-INSERT INTO\n[5]-DELETE FRONT\n[6]-DELETE REAR\n[7]-DELETE ITEM\n[8]-DELETE ALL ITEM\n[9]-MAKE UNIQUE\n"); choices=getch(); switch(choices) { case '1': displayArray(); getch(); askUser(); break; case '2': num=askNum(); for(i=0;i<num;i++) { while(x<0||x>9) { printf("Enter number: "); scanf("%d",&x); } insertRear(x); x=10; displayArray(); } getch(); askUser(); break; case '3': num=askNum(); for(i=0;i<num;i++) { while(x<0||x>9) { printf("Enter number: "); scanf("%d",&x); } insertFront(x); x=10; displayArray(); } getch(); askUser(); break; case '4': displayArray(); insertInto(); displayArray(); getch(); askUser(); break; case '5': deleteFront(); displayArray(); getch(); askUser(); break; case '6': deleteRear(); displayArray(); getch(); askUser(); break; case '7': displayArray(); deleteItem(); displayArray(); getch(); askUser(); break; case '8': displayArray(); deleteAllItem(); displayArray(); getch(); askUser(); break; case '9': displayArray(); makeUnique(); displayArray(); getch(); askUser(); break; default: getch(); break; } }
void askUser() { int num; int x=10; int i; char choices; struct node *current; current=head; system("cls"); printf("\n[1]-ENQUEUE\n[2]-DEQUEUE\n[3]-FRONT\n[4]-REAR\n"); choices=getch(); switch(choices) { case '1': num=askNum(); for(i=0;i<num;i++) { while(x<0||x>9) { printf("Enter number: "); scanf("%d",&x); } insertRear(x); x=10; displayArray(); } getch(); askUser(); break; case '2': deleteFront(); displayArray(); getch(); askUser(); break; case '3': if(head!=NULL) { printf("%d",head->data); } else { printf("List is empty"); } getch(); askUser(); break; case '4': while(current->next!=NULL) { current=current->next; } printf("%d",current->data); getch(); askUser(); break; default: getch(); break; } }