Exemplo n.º 1
0
/* Delete the min element and insert the new item x; by doing a delete
   and an insert together you can save a heapify() call */
void PQ_deleteMinAndInsert(PQueue* pq, elemType elt) {
  
  assert(pq && pq->elements); 
   PQ_DEBUG {printf("PQ_deleteMinAndinsert: "); printElem(elt); printf("\n"); fflush(stdout);}
  pq->elements[0] = elt;
  heapify(pq, 0);
}
Exemplo n.º 2
0
/* print the elements in the queue */
void PQ_print(PQueue* pq) {
  printf("PQ: "); fflush(stdout);
  unsigned int i;
  for (i=0; i< mymin(10, pq->cursize); i++) {
    printElem(pq->elements[i]);
  }
  printf("\n");fflush(stdout);
}
Exemplo n.º 3
0
void AVL_tree<T>::printPre(Node_T *p) {
    if(!p)  return;
    printElem(p);
    cout << "{";
    if(p->m_pChildren[0])   printPre(p->m_pChildren[0]);
    cout << ", ";
    if(p->m_pChildren[1])   printPre(p->m_pChildren[1]);
    cout << "} ";
}
Exemplo n.º 4
0
/* Set *elt to the min element in the queue and delete it from queue; 
   return value: 1 if exists a min, 0 if not   */
int PQ_extractMin(PQueue* pq, elemType* elt) {

  assert(pq && pq->elements);
  if (!pq->cursize) {
    return 0;
  }
  *elt = pq->elements[0];
  pq->elements[0] = pq->elements[--pq->cursize];
  heapify(pq, 0);

  PQ_DEBUG {printf("PQ_extractMin: "); printElem(*elt); printf("\n"); fflush(stdout);}
  return 1;
}
//!-----------------------------------------
void dumpTree(treeElem_t *node, const unsigned int mode, int depth, FILE *output)
{
    assert(node);
    assert(mode == DEBUG || mode || NODEBUG);
    //ASSERT_OK(node);

    printElem(mode, node, depth, output);

    if (node->left)
        dumpTree(node->left, mode, ++depth, output);

    depth--;

    if (node->right)
            dumpTree(node->right, mode, ++depth, output);
}
Exemplo n.º 6
0
/* Insert */
void PQ_insert(PQueue* pq, elemType elt) {
  
  unsigned int ii;
  assert(pq && pq->elements); 
 
  PQ_DEBUG {printf("PQ_insert: "); printElem(elt); printf("\n"); fflush(stdout);}
  if (pq->cursize==pq->maxsize) {
    PQ_grow(pq);
  }
  assert(pq->cursize < pq->maxsize);
  for (ii = pq->cursize++;
       ii && (compare_element(pq->elements[heap_parent(ii)], elt) > 0);
       ii = heap_parent(ii)) {
    pq->elements[ii] = pq->elements[heap_parent(ii)];
  }
  pq->elements[ii] = elt;
}                                       
Exemplo n.º 7
0
	void FlatZincSpace::print() {
		firstStageSolutions.growTo(scenario->getVal());
		vec<int>& x = firstStageSolutions[scenario->getVal()-1];
		x.growTo(firstStageVars.size());
		for (int i=0; i<firstStageVars.size(); i++) {
			x[i] = firstStageVars[i]->getVal();
		}
		
		if (output == NULL) return;
		for (unsigned int i=0; i< output->a.size(); i++) {
			AST::Node* ai = output->a[i];
			if (ai->isArray()) {
				AST::Array* aia = ai->getArray();
				int size = aia->a.size();
				std::cout << "[";
				for (int j=0; j<size; j++) {
					printElem(aia->a[j]);
					if (j<size-1) cout << ", ";
				}
				std::cout << "]";
			} else if (ai->isCall("ifthenelse")) {
				AST::Array* aia = ai->getCall("ifthenelse")->getArgs(3);
				if (aia->a[0]->isBool()) {
					if (aia->a[0]->getBool()) printElem(aia->a[1]);
					else printElem(aia->a[2]);
				} else if (aia->a[0]->isBoolVar()) {
					BoolView b = bv[aia->a[0]->getBoolVar()];
					if (b.isTrue()) printElem(aia->a[1]);
					else if (b.isFalse()) printElem(aia->a[2]);
					else std::cerr << "% Error: Condition not fixed." << std::endl;
				} else {
					std::cerr << "% Error: Condition not Boolean." << std::endl;        
				}
			} else {
				printElem(ai);
			}
		}
	}
Exemplo n.º 8
0
void BinTree<T>::printPos(Node_T *p) {
    if(!p)  return;
    if(p->m_pChildren[0])   printIn(p->m_pChildren[0]);
    if(p->m_pChildren[1])   printIn(p->m_pChildren[1]);
    printElem(p);
}
Exemplo n.º 9
0
int main()
{
    system("clear");
    BookList_t*List = malloc(sizeof(BookList_t));
    if (!List) return 0;
        List->first = NULL;
        List->last  = NULL;
    int temp = 0;
    int choice;
MainMenu:system ("clear");
    mainmenu();           //MainMenu Iniziale
    printf("\nChoice: ");
    scanf("%d", &choice);
    while(choice!=5)            //Riferito al mainmenu iniziale, esco solo con 5 -> END PROGRAM
    {
        switch (choice)
        {
                
//::::::::::::::::::::::::::::::::::::::::FUNC:INSERT:::::::::::::::::::::::::::::::::::::::::::
                
            case 1:
                while (1){
                system("clear");
                printf("Insert the element:\n1)At the top of the list.\n2)At the end of the list.\n\n0)Main Menu\n\n");
                scanf ("%d", &temp);
                if (temp == 1) {                //Insert Head________________________
                    system("clear");
                    printf("Enter the book:\n");
                    insHead(List);
                    printList(List);
                    printf("Press any number to continue..");
                    scanf("%d", &temp);
                    system("clear");
                    break;}
                else  if (temp == 2){           //Insert Tail________________________
                    system("clear");
                    printf("Enter the book:\n");
                    insTail(List);
                    printList(List);
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    system("clear");
                    break;}
                else if (temp == 0) {
                    goto MainMenu;
                }
                
                
                
                else {
                    printf ("Please Insert a valid Number\a\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                }
                }
//::::::::::::::::::::::::::::::::::::::::FUNC:REMOVE:::::::::::::::::::::::::::::::::::::::::::
                
                
            case 2:
                while (1){
                system("clear");
                printf("Remove the element:\n1)At the top of the list.\n2)At the end of the list \n3)Remove from id.\n\n0)Main Menu\n\n");
                scanf ("%d", &temp);
                if (temp == 1) {                //Remove Head________________________
                system ("clear");
                printf("Book removed successfully.\n");
                rmvHead(List);
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;}
                else if (temp == 2){            //Remove Tail________________________
                system ("clear");
                rmvTail(List);
                printf("Book removed successfully.\n");
                    printf("Press any number to continue..");
                    scanf("%d", &temp);
                    break;}
                else if (temp == 3){            //Remove From ID_____________________
                    system ("clear");
                    printf("Enter the book's id you want to delete:\n");
                    Cell_t* Ptr = searchId(List);
                    Cell_t *Temp = List->first;
                    if (Ptr != NULL)
                        while(Temp != Ptr)
                        {
                            Temp = Temp->pNext;
                            Temp->pNext = NULL;
                            free(Ptr);
                            Ptr = Temp;
                            printf("Press any number to continue..");
                            scanf("%d", &temp);
                            break;
                        }
                        }
                        else if (temp == 0) {
                            goto MainMenu;
                        }
                        else {
                            printf ("Please Insert a valid Number\a\n");
                            printf("Press any number to continue..\n");
                            scanf("%d", &temp);
                        }
                }

//::::::::::::::::::::::::::::::::::::FUNC:LOAN:RETURN:::::::::::::::::::::::::::::::::::::::::
                
            case 3:
                while (1){
                system("clear");
                printf("Which action do you want to do?:\n1)Loan a book.\n2)Return a book.\n\n0)Main Menu\n\n");
                scanf("%d", &temp);
                
                if (temp == 1){
                    system ("clear");   //Copyout________________
                    copyOut(List);
                    printf("Function worked correctly.\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;}
                else if (temp == 2){
                                        //Copyin________________________
                system ("clear");
                    copyIn(List);
                    printf("Function worked correctly.\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;
                }
                    else if (temp == 0)
                        goto MainMenu;
                
                else {
                    printf ("Please Insert a valid Number\a\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                }
                }
//::::::::::::::::::::::::::::::::::::::FUNC:SEE:LIST:::::::::::::::::::::::::::::::::::::::::::
            
            case 4:
                while (1){
                system("clear");
                printf("What do you want to see?\n1)The List\n2)A specific book\n3)To check if the book is in the list\n\n0)Main Menu\n\n");
                scanf ("%d", &temp);
                
                if (temp == 1){ //see the list_________________________________________
                    system ("clear");
                    printList(List);
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;
                }
                
                else if (temp ==2){ //see a specific book______________________________
                     system ("clear");
                     printf("Enter the id:\n");
                     Cell_t *p = searchId(List);
                    if(p != NULL){
                        printElem(p->book);
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                    else { printf("No book with this id available.\n");
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                }
                else if (temp==3){//To controll if the book is in the list_____________
                    system ("clear");
                    printf("Enter the id to see if the book is in the list:\n");
                    Cell_t *ren = searchId(List);
                    if (ren != NULL) {
                        printf("TRUE - In List\n");
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                    else {
                        printf("FALSE - Not in list\n");
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                    
                                }
                else if (temp == 0) {
                    goto MainMenu;
                                    }
                else {
                    printf ("Please Insert a valid Number\a\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                     }
            }
                
                    
                
        }
    
 
//::::::::::::::::::::::::::::::::::::::FINE:FUNZIONI:::::::::::::::::::::::::::::::::::::::::::
       
                
        system ("clear");
        mainmenu();
        printf("\nChoice: ");
        scanf("%d", &choice);
    } //Fine While
    system ("clear");
    int e = 0;
finalpiont: printf("\nExiting...\n\nAre you sure?\n1)Yes\n2)No\n");
        scanf("%d", &e);
        if (e == 1){
            system ("clear");
            printf("\nThanks for using -Book's Registration Program-\n\nCopyright © 2016 Matteo Peruzzi. All rights reserved.\n\n\n");
            return 0;
        }
    else if (e == 2) {
        system("clear");
        goto MainMenu;
    }
    else {
        system ("clear");
        printf ("Please Insert a valid Number\a\n");
        goto finalpiont;
    }

}