Exemplo n.º 1
0
static SPList quickList(int size, ...) {
	va_list items;
	SPList list = spListCreate();
	va_start(items, size);
	for (int i = 0; i < size; i++) {
		spListInsertLast(list, va_arg(items, SPListElement));
	}
	va_end(items);
	return list;
}
Exemplo n.º 2
0
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element){

	// variables declaration//
	int i;
	SPList source_list = source->list;
	SPListElement elem_to_comp;
	int list_size = spListGetSize(source_list);

	// check for bad arguments or allocation problems//
	if (!source || !element || !source_list){
		return SP_BPQUEUE_INVALID_ARGUMENT;
	}


	// insertion process//
	if (spBPQueueIsEmpty(source)){
		spListInsertFirst(source_list, element);
		return SP_BPQUEUE_SUCCESS;
	}
	else{
		elem_to_comp = spListGetFirst(source_list);

		/*starts from the bottom and loop for next source list element up until
		you find the one which is bigger than the inserted one and insert the element
		before that one. you will stop after you ran out of elements to look for */
        
        for(i=0; i<list_size; i++){
            if (spListElementGetValue(element) <= spListElementGetValue(elem_to_comp)){// element is smaller than next elem, insert before current
                if (spBPQueueIsFull(source)){
                    if (spListInsertBeforeCurrent(source_list, element) == SP_LIST_OUT_OF_MEMORY){
                        return SP_BPQUEUE_OUT_OF_MEMORY;
                    }
                    spListGetLast(source_list);
                    spListRemoveCurrent(source_list);
                    return SP_BPQUEUE_SUCCESS;
                }
                else{
                    if (spListInsertBeforeCurrent(source_list, element) == SP_LIST_OUT_OF_MEMORY){
                        return SP_BPQUEUE_OUT_OF_MEMORY;
                    }
                    return SP_BPQUEUE_SUCCESS;
                }
            }else{
                elem_to_comp = spListGetNext(source_list);
            }
        }
    }
    if (!spBPQueueIsFull(source)){//queue isn't full but this item is the biggest
        if (spListInsertLast(source_list, element)== SP_LIST_OUT_OF_MEMORY){
            return SP_BPQUEUE_OUT_OF_MEMORY;
        }
        return SP_BPQUEUE_SUCCESS;
    }
    return SP_BPQUEUE_SUCCESS; // the element is larger than all others
}
Exemplo n.º 3
0
static bool testListInsertLast() {
	ASSERT_TRUE(SP_LIST_NULL_ARGUMENT == spListInsertLast(NULL, NULL));
	SPListElement e1 = spListElementCreate(1, 1.0);
	SPListElement e2 = spListElementCreate(2, 2.0);
	SPListElement e3 = spListElementCreate(3, 3.0);
	SPListElement e4 = spListElementCreate(4, 4.0);
	SPList list2 = quickList(3, e2, e3, e4);
	ASSERT_TRUE(SP_LIST_SUCCESS == spListInsertLast(list2, e1));
	ASSERT_TRUE(4 == spListGetSize(list2));
	ASSERT_TRUE(spListElementCompare(e2, spListGetFirst(list2))==0);
	ASSERT_TRUE(spListElementCompare(e3, spListGetNext(list2))==0);
	ASSERT_TRUE(spListElementCompare(e4, spListGetNext(list2))==0);
	ASSERT_TRUE(spListElementCompare(e1, spListGetNext(list2))==0);
	ASSERT_TRUE(spListGetNext(list2) == NULL);
	spListDestroy(list2);
	spListElementDestroy(e1);
	spListElementDestroy(e2);
	spListElementDestroy(e3);
	spListElementDestroy(e4);
	return true;
}
Exemplo n.º 4
0
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element) {
	if (source == NULL || element == NULL) {
		return SP_BPQUEUE_INVALID_ARGUMENT;
	}

	bool wasFull = spBPQueueIsFull(source);

	SPList list = source->innerList;

	bool wasInserted = false;
	// Advance iterator to the proper element's place
	SP_LIST_FOREACH(SPListElement, currentElement, list) {

		if (spListElementCompare(element, currentElement) < 0) {

			// Modifying while iterating - bad practice but is more efficient here.
			SP_LIST_MSG returnMSG = spListInsertBeforeCurrent(list, element);

			switch (returnMSG) {
			case SP_LIST_OUT_OF_MEMORY:
				return SP_BPQUEUE_OUT_OF_MEMORY;
			default:
				break;
			}
			wasInserted = true;
			break;
		}
	}

	if (wasInserted) {
		// Remove the last element if needed
		if (wasFull) {
			spListGetLast(list);
			spListRemoveCurrent(list);
		}
		return SP_BPQUEUE_SUCCESS;
	} else {
		if (spBPQueueIsFull(source)) {
			return SP_BPQUEUE_FULL;
		} else {
			// The item is largest than everything, insert last
			SP_LIST_MSG returnMSG = spListInsertLast(list, element);
			switch (returnMSG) {
			case SP_LIST_OUT_OF_MEMORY:
				return SP_BPQUEUE_OUT_OF_MEMORY;
			default:
				return SP_BPQUEUE_SUCCESS;
			}
		}
	}
}
Exemplo n.º 5
0
SP_BPQUEUE_MSG spBPQueueInsertNotEmptyButLast(SPBPQueue source, SPListElement element) {
	SP_LIST_MSG retVal;
	retVal = spListInsertLast(source->queue, element);

	if (retVal == SP_LIST_OUT_OF_MEMORY)
		return SP_BPQUEUE_OUT_OF_MEMORY;

	spListElementDestroy(source->maxElement);
	source->maxElement = spListElementCopy(element);

	if (source->maxElement == NULL)
		return SP_BPQUEUE_OUT_OF_MEMORY;

	return SP_BPQUEUE_SUCCESS;
}
Exemplo n.º 6
0
SP_LIST_MSG spListInsertAfterCurrent(SPList list, SPListElement element) {
	if (list == NULL || element == NULL) {
		return SP_LIST_NULL_ARGUMENT;
	}
	if (list->current == NULL) {
		return SP_LIST_INVALID_CURRENT;
	} else if (list->current == list->tail->previous) {
		return spListInsertLast(list, element);
	} else {
		Node tempCurrent = list->current;
		list->current = list->current->next;
		SP_LIST_MSG res = spListInsertBeforeCurrent(list, element);
		list->current = tempCurrent;
		return res;
	}
}
Exemplo n.º 7
0
SPList spListCopy(SPList list) {
	if (list == NULL) {
		return NULL;
	}
	SPList copyList = spListCreate();
	if (copyList == NULL) {
		return NULL;
	}
	SPListElement currentElement = spListGetFirst(list);
	while (currentElement) {
		if (spListInsertLast(copyList, currentElement) != SP_LIST_SUCCESS) {
			spListDestroy(copyList);
			list->current = NULL;
			return NULL;
		}
		currentElement = spListGetNext(list);
	}
	list->current = NULL;
	copyList->current = NULL;
	return copyList;
}
Exemplo n.º 8
0
    SPListElement e5 = spListElementCreate(5, 5.0);
	SPListElement arr[5] = {e1,e2,e3,e4,e5};
	int i = 1;
	SPList list = quickList(3, e2, e3, e4);
	SP_LIST_FOREACH(SPListElement,e,list){
		ASSERT_TRUE(spListElementCompare(e,arr[i]) == 0);
		i++;
	}
	ASSERT_TRUE(spListInsertFirst(list,e1) == SP_LIST_SUCCESS);
	i=0;
	SP_LIST_FOREACH(SPListElement,e,list){
		ASSERT_TRUE(spListElementCompare(e,arr[i]) == 0);
		i++;
	}
	ASSERT_TRUE(i==4);//number of elements = 4
	ASSERT_TRUE(spListInsertLast(list,e5) == SP_LIST_SUCCESS);
	i=0;
	SP_LIST_FOREACH(SPListElement,e,list){
		ASSERT_TRUE(spListElementCompare(e,arr[i]) ==  0);
		i++;
	}
	ASSERT_TRUE(i==5);//number of elements should be 5
	ASSERT_TRUE(spListClear(list) == SP_LIST_SUCCESS);
	SP_LIST_FOREACH(SPListElement,e,list){
		ASSERT_TRUE(false);//Should not enter this loop because list is empty
	}
	spListDestroy(list);
	spListElementDestroy(e1);
	spListElementDestroy(e2);
	spListElementDestroy(e3);
	spListElementDestroy(e4);