Beispiel #1
0
static
void BubbleForward(My402List *pList, My402ListElem **pp_elem1, My402ListElem **pp_elem2)
    /* (*pp_elem1) must be closer to First() than (*pp_elem2) */
{
    My402ListElem *elem1=(*pp_elem1), *elem2=(*pp_elem2);
    void *obj1=elem1->obj, *obj2=elem2->obj;
    My402ListElem *elem1prev=My402ListPrev(pList, elem1);
/*  My402ListElem *elem1next=My402ListNext(pList, elem1); */
/*  My402ListElem *elem2prev=My402ListPrev(pList, elem2); */
    My402ListElem *elem2next=My402ListNext(pList, elem2);

    My402ListUnlink(pList, elem1);
    My402ListUnlink(pList, elem2);
    if (elem1prev == NULL) {
        (void)My402ListPrepend(pList, obj2);
        *pp_elem1 = My402ListFirst(pList);
    } else {
        (void)My402ListInsertAfter(pList, obj2, elem1prev);
        *pp_elem1 = My402ListNext(pList, elem1prev);
    }
    if (elem2next == NULL) {
        (void)My402ListAppend(pList, obj1);
        *pp_elem2 = My402ListLast(pList);
    } else {
        (void)My402ListInsertBefore(pList, obj1, elem2next);
        *pp_elem2 = My402ListPrev(pList, elem2next);
    }
}
Beispiel #2
0
int insertionSort(My402List *pList, My402SortElem *pKey){

	My402ListElem *current = My402ListLast(pList);
	My402ListElem *prevCurrent = current;
	while(current != NULL &&
		current->obj != NULL && 
		 ((My402SortElem *)current->obj)->transTime > pKey->transTime){
	
		prevCurrent = current;
		current = My402ListPrev(pList, current);	
	}
	if(current != NULL && current->obj != NULL){
		if(((My402SortElem *)current->obj)->transTime ==  pKey->transTime){
			fprintf(stderr,"\nTime stamp %lld is repeated for some entries",(long long)pKey->transTime);	
			//clean up an return
			unlinkSortElements(pList);	
			return FALSE;
		
		}
	}
	if(current == NULL){
		My402ListInsertBefore(pList, pKey, prevCurrent);
	}else{
		My402ListInsertAfter(pList, pKey, current);
	}
	return TRUE;
}
static
void BubbleSortBackwardList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
    int i=0;

    if (My402ListLength(pList) != num_items) {
        fprintf(stderr, "List length is not %1d in BubbleSortBackwardList().\n", num_items);
        exit(1);
    }
    for (i=0; i < num_items; i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *prev_elem=NULL;

        for (elem=My402ListLast(pList), j=0; j < num_items-i-1; elem=prev_elem, j++) {
            int cur_val=(int)(elem->obj), prev_val=0;

            prev_elem=My402ListPrev(pList, elem);
            prev_val = (int)(prev_elem->obj);

            if (cur_val < prev_val) {
                BubbleBackward(pList, &elem, &prev_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
Beispiel #4
0
void unlinkSortElements(My402List *pList){

        My402ListElem *current = My402ListLast(pList);
        My402ListElem *currentPrev = current;
        while(currentPrev != NULL){

                //obtain the prev node first and then unlink    
                currentPrev = My402ListPrev(pList,current);
		free(current->obj);
                My402ListUnlink(pList, current);
                current = currentPrev;
        }
}
Beispiel #5
0
void insertionSort(My402List *pList, My402SortElem *pKey){

	My402ListElem *current = My402ListLast(pList);
	My402ListElem *prevCurrent = current;
	while(current != NULL &&
		current->obj != NULL && 
		 ((My402SortElem *)current->obj)->transTime > pKey->transTime){
	
		prevCurrent = current;
		current = My402ListPrev(pList, current);	
	}
	if(current == NULL){
		My402ListInsertBefore(pList, pKey, prevCurrent);
	}else{
		My402ListInsertAfter(pList, pKey, current);
	}
}
void insertionSort(My402List pList, char *pKey){

	My402ListElem *current = My402ListLast(pList);
	My402ListElem *prevCurrent = current;
	while(current != NULL &&
		current->obj != NULL && 
		atoi(current->obj) > atoi(pKey)){
	
		prevCurrent = current;
		current = My402ListPrev(pList, current);	
	}
	if(current == NULL){
		insertBefore(pList, pKeyElem, prevCurrent);
	}else{
		insertAfter(pList, pKeyElem, current);
	}
	
}
Beispiel #7
0
void sort(My402List* final_list)        
{
    My402ListElem *first_ptr, *second_ptr, *innerelemminus;
    Data *inner, *innerminus, *temp;
    int i=0;
    
    for(first_ptr=My402ListFirst(final_list);first_ptr!=NULL;first_ptr=My402ListNext(final_list,first_ptr))
    {
        i=0;
        for(second_ptr=My402ListLast(final_list);second_ptr!=My402ListFirst(final_list);second_ptr=My402ListPrev(final_list,second_ptr))      
        {
            inner=(Data*)second_ptr->obj;
            innerelemminus= My402ListPrev(final_list,second_ptr);
            innerminus=(Data*)innerelemminus->obj;
            
                       
            if(inner->time==innerminus->time)
            {
                printf("ERROR:Two transaction with same timestamp\n");
                exit(0);
            }
            if(innerminus->time > inner->time)
            {
                temp = (Data*) innerelemminus->obj;
                innerelemminus->obj = second_ptr->obj;
                second_ptr->obj = (void*)temp;
                i=1;
                
            }
            
        }
        
    }
  
    
}
Beispiel #8
0
char* pBalance(My402List *list, My402ListElem *elem, char *bal)
{
    int balance = 0;
    char temp[15];

    /*
     * 14 characters for balance including "(", ")"
     * 1 extra for the null character
     */

    ListEntry* Entry = NULL, *prevEntry = NULL;
    Entry = (ListEntry *)elem->obj;
    if(My402ListPrev(list, elem)){
        prevEntry = (ListEntry *)elem->prev->obj;
    }
    if(Entry->trans_type[0] == '+') {
        if(prevEntry) {
            Entry->balance =  prevEntry->balance + Entry->trans_amt;
        }else {
            Entry->balance = Entry->trans_amt;
        }
    }else{
        if(prevEntry) {
            Entry->balance = prevEntry->balance - Entry->trans_amt;
        }else {
            Entry->balance = (Entry->trans_amt) * (-1);
        }
    }

    if(Entry->balance < 0){
        balance = (Entry->balance) * (-1);
        if(balance >= 1000000000) {
            strncpy(bal, "(?,???,???.??", 15);
            strcat(bal, ")");
            return bal;
        }
        snprintf(temp, 15, "%d", balance);

        bal = pValue(bal, temp);
        strncpy(temp, bal, 15);
        memset(bal, 0, 15);
        snprintf(bal, 15, "(%12s",temp);

        strncat(bal,")", 15);
    } else{
        balance = Entry->balance;
        if(balance >= 1000000000) {
            strncpy(bal, "?,???,???.?? ", 15);
            return bal;
        }
        snprintf(temp, 15, "%d", balance);

        bal = pValue(bal, temp);
        memset(temp, 0 , 15);
        strncpy(temp, bal, 15);
        memset(bal, 0, 15);
        snprintf(bal, 15, "%13s",temp);
        strncat(bal," ", 15);
    }
    return bal;
}