void printlevel(struct BT *root) { if(!root) return; struct BT **Q=createQ(); printf("\n\n"); nQ(Q,root); while(root) { root=dQ(Q); printf("[%d]\t",root->data); if(root->l) { nQ(Q,root->l);} if(root->r) nQ(Q,root->r); } printf("\n\n"); }
int main() { pthread_mutex_init(&qu, 0); int check = init_pool(4); sem_init(&make, 0, 19); sem_init(&use, 0, 0); createQ(); pthread_create(&producer, 0, produce, NULL); pthread_create(&consumer, 0, consume, NULL); pthread_join(producer, 0); pthread_join(consumer, 0); }
int main() { FILE *in; in=fopen("input.txt", "r"); createQ(in); NodeT *root = createTree(); prettyPrint(root, 0); return 0; }
int main () { Q *q = createQ(10); enQ(q, 0x0, 10); enQ(q, 0x0, 20); deQ(q); deQ(q); enQ(q, 0x0, 30); enQ(q, 0x0, 40); enQ(q, 0x0, 50); struct qNode *n = deQ(q); if (n != NULL) printf("Dequeued item is %d", n->key); return 0; }
main() { struct Q *queue=createQ(MAX); struct BT *rt=NULL; int i; for(i=0;i<10;i++) insert(&rt,queue,i); printlevel(rt); printf("\n"); }
void printlevel(struct BT *root) { if(!root) return; struct Q *q=createQ(MAX); nq(q,root); while(!isEmpty(q)) { root=dq(q); printf("[%d]\t",root->data); if(root->l) nq(q,root->l); if(root->r) nq(q,root->r); } }
int main(int argc, char *argv[]) { bool rFlag = false; // Flag to tell if -r is specified int height = 3; // HEIGHT of the Rubik's square int width = 3; // WIDTH of the Rubik's square int maxlength; // MAXLENGTH of a series of moves char *initial = NULL; // The INITIAL string char *goal = NULL; // The GOAL string /* INPUT ERROR CHECKING */ // Check for correct number of args if (argc < 4 || argc > 7) { DIE("RubikSq: RubikSq [-r] [HEIGHT WIDTH] MAXLENGTH INITIAL GOAL"); } if (argc == 5 || argc == 7) { // Check to make sure the first arg is "-r" if (strcmp(argv[1], "-r") != 0) { DIE("RubikSq: Invalid [-r] Flag"); } rFlag = true; } if (argc == 6 || argc == 7) { char *check = argv[argc-5]; // Used to check for non-numeric chars in the args for (int i = 0; i < strlen(check); i++) { if (!isdigit(check[i])) { DIE("RubikSq: Invalid HEIGHT"); } } char *end = NULL; // End pointer for strtol height = strtol(argv[argc-5], &end, 10); if (*end != '\0') { DIE("RubikSq: Invalid HEIGHT"); } char *check2 = argv[argc-4]; // Used to check for non-numeric chars in the args for (int i = 0; i < strlen(check2); i++) { if (!isdigit(check2[i])) { DIE("RubikSq: Invalid WIDTH"); } } char *end2 = NULL; // End pointer for strtol width = strtol(argv[argc-4], &end2, 10); if (*end2 != '\0') { DIE("RubikSq: Invalid WIDTH"); } } char *check3 = argv[argc-3]; // Used to check for non-numeric chars in the args for (int i = 0; i < strlen(check3); i++) { if (!isdigit(check3[i])) { DIE("RubikSq: Invalid MAXLENGTH"); } } char *end3 = NULL; // End pointer for strtol maxlength = strtol(argv[argc-3], &end3, 10); if (*end3 != '\0') { DIE("RubikSq: Invalid MAXLENGTH"); } // Check for non-alpha chars in the args initial = argv[argc-2]; for (int i = 0; i < strlen(initial); i++) { if (!isalpha(initial[i])) { DIE("RubikSq: Invalid INITIAL"); } } // Check for non-alpha chars in the args goal = argv[argc-1]; for (int i = 0; i < strlen(goal); i++) { if (!isalpha(goal[i])) { DIE("RubikSq: Invalid GOAL"); } } if (maxlength < 0) { DIE("RubikSq: MAXLENGTH < 0"); } if (height < 2 || height > 5 || width < 2 || width > 5) { DIE("RubikSq: HEIGHT/WIDTH must be between 2 and 5, inclusive"); } if (strlen(initial) != strlen(goal)){ DIE("RubikSq: Length of INITIAL does not equal length of GOAL"); } if ((height*width) != strlen(initial)) { DIE("RubikSq: HEIGHT*WIDTH does not match length of INITIAL/GOAL"); } // If GOAL and INITIAL are the same, print and exit if (strcmp(initial, goal) == 0) { printf("%s\n", initial); exit(0); } // Make an alphabetized copy of INITIAL char *initialSort = malloc(strlen(initial)+1); // An alphabetized version of INITIAL strcpy(initialSort, initial); sortAlphabetical(initialSort); // Make an alphabetized copy of GOAL char *goalSort = malloc(strlen(goal)+1); // An alphabetized version of GOAL strcpy(goalSort, goal); sortAlphabetical(goalSort); if (strcmp(initialSort, goalSort) != 0) { DIE("RubikSq: INITIAL and GOAL do not contain the same letters"); } // Make sure all letters are between A and L, inclusive if (initialSort[0] < 'A' || initialSort[strlen(initialSort)-1] > 'L' || goalSort[0] < 'A' || goalSort[strlen(goalSort)-1] > 'L') { DIE("RubikSq: INITIAL/GOAL contain invalid letters"); } /* ALGORITHM */ Trie dict; // The dictionary trie for storing past positions createT(&dict); addT(&dict, goal, NULL, 0, 0); Queue moves; // The queue of positions to analyze createQ(&moves); enQ(&moves, goal); // Main loop while (!isEmptyQ(&moves)) { char *pos = NULL; // The current position P deQ(&moves, &pos); int len = getLengthT(&dict, pos, 0); // The length of P in the dictionary if (len < maxlength) { int total; // The total number of possible moves // If -r is specified, there are more possible moves if (rFlag) { total = (width-1)*height + (height-1)*width; } else { total = width + height; } char *primes[total]; // The array of possible positions P' char *copy; // Used to find all P' to put in the array // If the -r flag is specified, we can move right or down multiple times if (rFlag) { for (int i = 1; i <= height; i++) { copy = pos; for (int j = 1; j < width; j++){ copy = moveRight(i, width, copy); primes[(i-1)*(width-1)+j-1] = copy; } } for (int i = 1; i <= width; i++) { copy = pos; for (int j = 1; j < height; j++){ copy = moveDown(i, width, height, copy); primes[height*(width-1)+(i-1)*(height-1)+j-1] = copy; } } } // Otherwise, we can only make one move else { for (int i = 1; i <= height; i++) { primes[i-1] = moveRight(i, width, pos); } for (int i = 1; i <= width; i++) { primes[i+height-1] = moveDown(i, width, height, pos); } } // P' checking for loop for (int i = 0; i < total; i++) { // If P' is INITIAL, print the moves and exit if (strcmp(primes[i], initial) == 0) { printf("%s\n", primes[i]); printf("%s\n", pos); while (getFromT(&dict, pos, 0) != NULL) { pos = getFromT(&dict, pos, 0); printf("%s\n", pos); } // Free the sorted INITIAL and GOAL free(initialSort); free(goalSort); exit(0); } // Else if P' is not in the dictionary, add it to the dict and queue else if (!isMemberT(&dict, primes[i], 0)) { addT(&dict, primes[i], pos, len+1, 0); enQ(&moves, primes[i]); } // Else free the storage else { free(primes[i]); } } } } // Free the sorted INITIAL and GOAL free(initialSort); free(goalSort); return 0; }
int main() { /* test */ int i,j,k,l,m,n; int exit = 1; int dir = 0; graph* gr=NULL; myQueue * q = NULL; while(exit) { printf("\nTEST MENU\n"); printf("*********\n"); printf("1.Create and print a graph\n"); printf("2.DFS\n"); printf("5.Exit\n"); scanf("%d",&m); switch(m) { case 1: printf("Enter no of nodes\n"); scanf("%d",&k); printf("Enter 0:UnDirected 1:Directed\n"); scanf("%d",&dir); gr=(graph*)createGraph(k); // addVertices(gr,0,1,dir,1); addVertices(gr,0,4,dir,4); addVertices(gr,1,2,dir,2); addVertices(gr,1,3,dir,3); addVertices(gr,1,4,dir,4); addVertices(gr,2,3,dir,3); addVertices(gr,3,4,dir,4); printGraph(gr); break; case 2: graph_DFS(gr,0); break; case 3: q = createQ(10); enQ(q,1); enQ(q,3); enQ(q,5); deQ(q); enQ(q,7); enQ(q,9); enQ(q,11); deQ(q); deQ(q); deQ(q); break; case 5: exit = 0; break; } } return 0; }