当前位置: 首页>>代码示例>>C++>>正文


C++ SortedListPtr::compareF方法代码示例

本文整理汇总了C++中SortedListPtr::compareF方法的典型用法代码示例。如果您正苦于以下问题:C++ SortedListPtr::compareF方法的具体用法?C++ SortedListPtr::compareF怎么用?C++ SortedListPtr::compareF使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SortedListPtr的用法示例。


在下文中一共展示了SortedListPtr::compareF方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: SLRemove

int SLRemove(SortedListPtr list, void *newObj)
{

	SortedListIteratorPtr ptr = SLCreateIterator(list);
	if(ptr == NULL)
	{
		return 0;
	}

	listItem* cur;
	listItem* prev;


	//checking at head
	int compcur = list->compareF(newObj,list->head->data);
	if(compcur == 0)
	{
		//move head
		list->head = list->head->next;
		//if been viewed once, free
		if(ptr->current->viewers==1)
		{
			freeItem(ptr->current);
		}
		SLDestroyIterator(ptr);
		return 1;
	}

	cur = ptr->current->next;
	prev = ptr->current;

	while(ptr->current != NULL && prev != NULL)
	{
		compcur = list->compareF(newObj, SLNextItem(ptr));
		if(compcur == 0)
		{
			prev->next = cur->next;
			if(cur->viewers == 1)
			{
				freeItem(cur);
			}
			SLDestroyIterator(ptr);
			return 1;
		}

		prev = cur;
		cur = cur->next;


	}
	SLDestroyIterator(ptr);
	printf("we should not have gotten here");
	return 0;





}
开发者ID:CS214Spring2016,项目名称:Assignment1,代码行数:59,代码来源:sorted-list.c

示例2: SLInsert

int SLInsert(SortedListPtr list, void *newObj)
{
	SortedListIteratorPtr iPtr = SLCreateIterator(list);

	if(iPtr == NULL)
	{
		return 0;
	}

	listItem *temp;
	listItem *swap;
	temp = (listItem*)malloc(sizeof(listItem));

	if(temp == NULL){
		fprintf(stderr, "Out of memory\n");
		exit(EXIT_FAILURE);
	}
	temp->data = newObj;
	temp->next = NULL;
	
	if(list->head == NULL)
	{
		printf("first item\n");
		list->head = temp;

	}
	else{

		while(iPtr->current->next != NULL){
			int compcurr = list->compareF(temp->data,iPtr->current->data);
			int compnext = list->compareF(temp->data, iPtr->current->next->data );
			if(compcurr==0){
				printf("already here\n");

				return 0;
			}
			else if(compcurr == -1 && compnext == 1){//found the right place
				printf("Put in middle\n");
				swap = iPtr->current->next;//save the next one
				iPtr->current->next = temp;//put it in the right place
				iPtr->current = iPtr->current->next;//iterate
				iPtr->current->next = swap; //place the saved one
				//SLDestroyIterator(iPtr);
				return 1;
			}
			else
				iPtr->current = iPtr->current->next;
		}
		int compcurr = list->compareF(temp->data, iPtr->current->data);
		if(compcurr == -1){
			printf("put at end\n");
			iPtr->current->next = temp;//put it in the right place
			iPtr->current = iPtr->current->next;//iterate
			//SLDestroyIterator(iPtr);
			return 1;
		}
		else{
			printf("put at beginning\n");
			swap = list->head;
			list->head = temp;
			iPtr->current =  list->head;
			iPtr->current->next = swap;
			//SLDestroyIterator(iPtr);

			return 1;

		}
	}


	return 1;
}
开发者ID:CS214Spring2016,项目名称:Assignment1,代码行数:72,代码来源:sorted-list.c


注:本文中的SortedListPtr::compareF方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。