static void DeleteList()//START PROBLEM 3 { int boolean = 1; int content; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); AppendNode(&head, content); } else { boolean = 0; } } //Now delete the list if(yesno("Would you like to delete the list (Y/N)?")) { Delete_List(&head); } if(head == NULL) { printf("The head node has been set to NULL.\n"); } }//END PROBLEM 3
void Reallocate_Neighbor_List( reax_list *far_nbrs, int n, int num_intrs, MPI_Comm comm ) { Delete_List( far_nbrs, comm ); if(!Make_List( n, num_intrs, TYP_FAR_NEIGHBOR, far_nbrs, comm )){ fprintf(stderr, "Problem in initializing far nbrs list. Terminating!\n"); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } }
int main() { int array[LINK_LENGTH] = { 1, 3, 4, 2, 9, 6, 7, 5, 0, 8 }; LinkList L; int i = 0; NODE *node; printf("Array is: "); while (i < LINK_LENGTH) { printf("%d ", array[i]); i++; } printf("\n"); //0. create printf("Create_List\n"); L = Create_List(array); Print_List(L); //1. find printf("\n"); printf("Find_List\n"); Print_List(L); node = Find_List(L, 3); Print_NODE(node); //2. update printf("\n"); Print_List(L); printf("Update_List\n"); Update_List(L, 3, 23); Print_List(L); //3. insert printf("\n"); Print_List(L); printf("Insert_List\n"); Insert_List(L, 3, 99); Print_List(L); //4. delete printf("\n"); Print_List(L); printf("Delete_List\n"); Delete_List(L, 8); Print_List(L); //5. free printf("\n"); Print_List(L); printf("Free resource!\n"); Free_List(L); Print_List(L); return 0; }
void BF_GUI_FtpPanel_DeleteTask::Run() { iOperIndex = 0; iOperCount = loNode.CountAll(true); SetProgress(iOperIndex,iOperCount); Delete_List(&loNode); // reload panel_nodes BMessenger oMessenger(poPanel); BMessage oMessage(BF_FTP_MSG_DIR_RELOAD_START); oMessenger.SendMessage(&oMessage); }
int Reallocate_Bonds_List( reax_system *system, reax_list *bonds, int *total_bonds, int *est_3body, MPI_Comm comm ) { int i; *total_bonds = 0; *est_3body = 0; for( i = 0; i < system->N; ++i ){ *est_3body += SQR(system->my_atoms[i].num_bonds); // commented out - already updated in validate_lists in forces.c // system->my_atoms[i].num_bonds = MAX( Num_Entries(i,bonds)*2, MIN_BONDS ); *total_bonds += system->my_atoms[i].num_bonds; } *total_bonds = (int)(MAX( *total_bonds * SAFE_ZONE, MIN_CAP*MIN_BONDS )); Delete_List( bonds, comm ); if(!Make_List(system->total_cap, *total_bonds, TYP_BOND, bonds, comm)) { fprintf( stderr, "not enough space for bonds list. terminating!\n" ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } return SUCCESS; }
int Reallocate_HBonds_List( reax_system *system, reax_list *hbonds, MPI_Comm comm ) { int i, id, total_hbonds; total_hbonds = 0; for( i = 0; i < system->n; ++i ) if( (id = system->my_atoms[i].Hindex) >= 0 ) { // commented out - already updated in validate_lists in forces.c // system->my_atoms[i].num_hbonds = MAX(Num_Entries(id,hbonds)*SAFER_ZONE, // MIN_HBONDS); total_hbonds += system->my_atoms[i].num_hbonds; } total_hbonds = (int)(MAX( total_hbonds*SAFER_ZONE, MIN_CAP*MIN_HBONDS )); Delete_List( hbonds, comm ); if( !Make_List( system->Hcap, total_hbonds, TYP_HBOND, hbonds, comm ) ) { fprintf( stderr, "not enough space for hbonds list. terminating!\n" ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } return total_hbonds; }
void ReAllocate( reax_system *system, control_params *control, simulation_data *data, storage *workspace, reax_list **lists, mpi_datatypes *mpi_data ) { int i, j, k, p; int num_bonds, est_3body, nflag, Nflag, Hflag, mpi_flag, ret, total_send; int renbr, newsize; reallocate_data *realloc; reax_list *far_nbrs; sparse_matrix *H; grid *g; neighbor_proc *nbr_pr; mpi_out_data *nbr_data; MPI_Comm comm; char msg[200]; realloc = &(workspace->realloc); g = &(system->my_grid); comm = mpi_data->world; #if defined(DEBUG) fprintf( stderr, "p%d@reallocate: n: %d, N: %d, numH: %d\n", system->my_rank, system->n, system->N, system->numH ); fprintf( stderr, "p%d@reallocate: local_cap: %d, total_cap: %d, Hcap: %d\n", system->my_rank, system->local_cap, system->total_cap, system->Hcap); fprintf( stderr, "p%d: realloc.num_far: %d\n", system->my_rank, realloc->num_far ); fprintf( stderr, "p%d: realloc.H: %d, realloc.Htop: %d\n", system->my_rank, realloc->H, realloc->Htop ); fprintf( stderr, "p%d: realloc.Hbonds: %d, realloc.num_hbonds: %d\n", system->my_rank, realloc->hbonds, realloc->num_hbonds ); fprintf( stderr, "p%d: realloc.bonds: %d, num_bonds: %d\n", system->my_rank, realloc->bonds, realloc->num_bonds ); fprintf( stderr, "p%d: realloc.num_3body: %d\n", system->my_rank, realloc->num_3body ); #endif // IMPORTANT: LOOSE ZONES CHECKS ARE DISABLED FOR NOW BY &&'ing with 0!!! nflag = 0; if( system->n >= DANGER_ZONE * system->local_cap || (0 && system->n <= LOOSE_ZONE * system->local_cap) ) { nflag = 1; system->local_cap = MAX( (int)(system->n * SAFE_ZONE), MIN_CAP ); } Nflag = 0; if( system->N >= DANGER_ZONE * system->total_cap || (0 && system->N <= LOOSE_ZONE * system->total_cap) ) { Nflag = 1; system->total_cap = MAX( (int)(system->N * SAFE_ZONE), MIN_CAP ); } if( Nflag ) { /* system */ #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating system and workspace -"\ "n=%d N=%d local_cap=%d total_cap=%d\n", system->my_rank, system->n, system->N, system->local_cap, system->total_cap ); #endif ret = Allocate_System( system, system->local_cap, system->total_cap, msg ); if( ret != SUCCESS ) { fprintf( stderr, "not enough space for atom_list: total_cap=%d", system->total_cap ); fprintf( stderr, "terminating...\n" ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } /* workspace */ DeAllocate_Workspace( control, workspace ); ret = Allocate_Workspace( system, control, workspace, system->local_cap, system->total_cap, comm, msg ); if( ret != SUCCESS ) { fprintf( stderr, "no space for workspace: local_cap=%d total_cap=%d", system->local_cap, system->total_cap ); fprintf( stderr, "terminating...\n" ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } } renbr = (data->step - data->prev_steps) % control->reneighbor == 0; /* far neighbors */ if( renbr ) { far_nbrs = *lists + FAR_NBRS; if( Nflag || realloc->num_far >= far_nbrs->num_intrs * DANGER_ZONE ) { if( realloc->num_far > far_nbrs->num_intrs ) { fprintf( stderr, "step%d-ran out of space on far_nbrs: top=%d, max=%d", data->step, realloc->num_far, far_nbrs->num_intrs ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } newsize = static_cast<int> (MAX( realloc->num_far*SAFE_ZONE, MIN_CAP*MIN_NBRS )); #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating far_nbrs: num_fars=%d, space=%dMB\n", system->my_rank, (int)(realloc->num_far*SAFE_ZONE), (newsize*sizeof(far_neighbor_data)/(1024*1024)) ); #endif Reallocate_Neighbor_List( far_nbrs, system->total_cap, newsize, comm ); realloc->num_far = 0; } } #if defined(PURE_REAX) /* qeq coef matrix */ H = workspace->H; if( nflag || realloc->Htop >= H->m * DANGER_ZONE ) { if( realloc->Htop > H->m ) { fprintf( stderr, "step%d - ran out of space on H matrix: Htop=%d, max = %d", data->step, realloc->Htop, H->m ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating H matrix: Htop=%d, space=%dMB\n", system->my_rank, (int)(realloc->Htop*SAFE_ZONE), (int)(realloc->Htop * SAFE_ZONE * sizeof(sparse_matrix_entry) / (1024*1024)) ); #endif newsize = static_cast<int> (MAX( realloc->Htop*SAFE_ZONE, MIN_CAP*MIN_NBRS )); Reallocate_Matrix( &(workspace->H), system->local_cap, newsize, "H", comm ); //Deallocate_Matrix( workspace->L ); //Deallocate_Matrix( workspace->U ); workspace->L = NULL; workspace->U = NULL; realloc->Htop = 0; } #endif /*PURE_REAX*/ /* hydrogen bonds list */ if( control->hbond_cut > 0 ) { Hflag = 0; if( system->numH >= DANGER_ZONE * system->Hcap || (0 && system->numH <= LOOSE_ZONE * system->Hcap) ) { Hflag = 1; system->Hcap = MAX( system->numH * SAFER_ZONE, MIN_CAP ); } if( Hflag || realloc->hbonds ) { ret = Reallocate_HBonds_List( system, (*lists)+HBONDS, comm ); realloc->hbonds = 0; #if defined(DEBUG_FOCUS) fprintf(stderr, "p%d: reallocating hbonds: total_hbonds=%d space=%dMB\n", system->my_rank, ret, (int)(ret*sizeof(hbond_data)/(1024*1024))); #endif } } /* bonds list */ num_bonds = est_3body = -1; if( Nflag || realloc->bonds ){ Reallocate_Bonds_List( system, (*lists)+BONDS, &num_bonds, &est_3body, comm ); realloc->bonds = 0; realloc->num_3body = MAX( realloc->num_3body, est_3body ); #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating bonds: total_bonds=%d, space=%dMB\n", system->my_rank, num_bonds, (int)(num_bonds*sizeof(bond_data)/(1024*1024)) ); #endif } /* 3-body list */ if( realloc->num_3body > 0 ) { #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating 3body list: num_3body=%d, space=%dMB\n", system->my_rank, realloc->num_3body, (int)(realloc->num_3body * sizeof(three_body_interaction_data) / (1024*1024)) ); #endif Delete_List( (*lists)+THREE_BODIES, comm ); if( num_bonds == -1 ) num_bonds = ((*lists)+BONDS)->num_intrs; realloc->num_3body = (int)(MAX(realloc->num_3body*SAFE_ZONE, MIN_3BODIES)); if( !Make_List( num_bonds, realloc->num_3body, TYP_THREE_BODY, (*lists)+THREE_BODIES, comm ) ) { fprintf( stderr, "Problem in initializing angles list. Terminating!\n" ); MPI_Abort( comm, CANNOT_INITIALIZE ); } realloc->num_3body = -1; } #if defined(PURE_REAX) /* grid */ if( renbr && realloc->gcell_atoms > -1 ) { #if defined(DEBUG_FOCUS) fprintf(stderr, "reallocating gcell: g->max_atoms: %d\n", g->max_atoms); #endif for( i = g->native_str[0]; i < g->native_end[0]; i++ ) for( j = g->native_str[1]; j < g->native_end[1]; j++ ) for( k = g->native_str[2]; k < g->native_end[2]; k++ ) { // reallocate g->atoms sfree( g->cells[i][j][k].atoms, "g:atoms" ); g->cells[i][j][k].atoms = (int*) scalloc( realloc->gcell_atoms, sizeof(int), "g:atoms", comm); } realloc->gcell_atoms = -1; } /* mpi buffers */ // we have to be at a renbring step - // to ensure correct values at mpi_buffers for update_boundary_positions if( !renbr ) mpi_flag = 0; // check whether in_buffer capacity is enough else if( system->max_recved >= system->est_recv * 0.90 ) mpi_flag = 1; else { // otherwise check individual outgoing buffers mpi_flag = 0; for( p = 0; p < MAX_NBRS; ++p ) { nbr_pr = &( system->my_nbrs[p] ); nbr_data = &( mpi_data->out_buffers[p] ); if( nbr_data->cnt >= nbr_pr->est_send * 0.90 ) { mpi_flag = 1; break; } } } if( mpi_flag ) { #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating mpi_buf: old_recv=%d\n", system->my_rank, system->est_recv ); for( p = 0; p < MAX_NBRS; ++p ) fprintf( stderr, "p%d: nbr%d old_send=%d\n", system->my_rank, p, system->my_nbrs[p].est_send ); #endif /* update mpi buffer estimates based on last comm */ system->est_recv = MAX( system->max_recved*SAFER_ZONE, MIN_SEND ); system->est_trans = (system->est_recv * sizeof(boundary_atom)) / sizeof(mpi_atom); total_send = 0; for( p = 0; p < MAX_NBRS; ++p ) { nbr_pr = &( system->my_nbrs[p] ); nbr_data = &( mpi_data->out_buffers[p] ); nbr_pr->est_send = MAX( nbr_data->cnt*SAFER_ZONE, MIN_SEND ); total_send += nbr_pr->est_send; } #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d: reallocating mpi_buf: recv=%d send=%d total=%dMB\n", system->my_rank, system->est_recv, total_send, (int)((system->est_recv+total_send)*sizeof(boundary_atom)/ (1024*1024))); for( p = 0; p < MAX_NBRS; ++p ) fprintf( stderr, "p%d: nbr%d new_send=%d\n", system->my_rank, p, system->my_nbrs[p].est_send ); #endif /* reallocate mpi buffers */ Deallocate_MPI_Buffers( mpi_data ); ret = Allocate_MPI_Buffers( mpi_data, system->est_recv, system->my_nbrs, msg ); if( ret != SUCCESS ) { fprintf( stderr, "%s", msg ); fprintf( stderr, "terminating...\n" ); MPI_Abort( comm, INSUFFICIENT_MEMORY ); } } #endif /*PURE_REAX*/ #if defined(DEBUG_FOCUS) fprintf( stderr, "p%d @ step%d: reallocate done\n", system->my_rank, data->step ); MPI_Barrier( comm ); #endif }
int main(int argc, char ** argv) { linked_list_t * mylist = Init_List(); for (int iterator = 0; iterator < 10; ++iterator) { Insert_At_Beginning(mylist, iterator); } if (10 != Count(mylist)) { printf("Found %d items on the stack, instead of 10\n", Count(mylist)); exit(1); } int expectedStacktop = 9; int stacktop = 0; while (! Empty(mylist)) { Remove_From_Beginning(mylist, &stacktop); if (stacktop != expectedStacktop) { printf("Expected %d at the top of the stack but got %d\n", expectedStacktop, stacktop); exit(1); } if ( Count(mylist) != stacktop) { printf("Count(mylist) returned an unexpected number of items: %d\n", Count(mylist)); exit(1); } --expectedStacktop; } if (0 != stacktop) { printf("Expected 0 now that we should be at the bottom of the stack, but got %d\n", stacktop); exit(1); } // If we got this far without calling exit, everything seems good. printf("Stack test of list worked for functions operating at the front.\n"); // Now let's try it at the end of the list for (int iterator = 0; iterator < 10; ++iterator) { Insert_At_End(mylist, iterator); } if (10 != Count(mylist)) { printf("Found %d items on the stack, instead of 10\n", Count(mylist)); exit(1); } expectedStacktop = 9; stacktop = 0; while (! Empty(mylist)) { Remove_From_End(mylist, &stacktop); if (stacktop != expectedStacktop) { printf("Expected %d at the top of the stack but got %d\n", expectedStacktop, stacktop); exit(1); } if ( Count(mylist) != stacktop) { printf("Count(mylist) returned an unexpected number of items: %d\n", Count(mylist)); exit(1); } --expectedStacktop; } if (0 != stacktop) { printf("Expected 0 now that we should be at the bottom of the stack, but got %d\n", stacktop); exit(1); } // If we got this far without calling exit, everything seems good. printf("Stack test of list worked for functions operating at the end.\n"); if (!Empty(mylist)) { printf("Expected stack to be empty, but it is not.\n"); exit(1); } int data = 0; for (int iterator = 0; iterator < 20; ++iterator) { data = rand() % 1000; printf("Adding %d to the list.\n", data); Insert_in_order(mylist, data); } Traverse(mylist, intPrinter); if (Count(mylist) != 20) { printf("Expected count of list after random insertion, should be 20 but it was %d\n", Count(mylist)); exit(1); } Delete_List(mylist); free(mylist); mylist = NULL; return 0; }