本文整理汇总了C++中SortedListPtr::compare方法的典型用法代码示例。如果您正苦于以下问题:C++ SortedListPtr::compare方法的具体用法?C++ SortedListPtr::compare怎么用?C++ SortedListPtr::compare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedListPtr
的用法示例。
在下文中一共展示了SortedListPtr::compare方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SLInsert
int SLInsert(SortedListPtr list, void *newObj)
{
// Create node cast link as a self referential node
SortedNodePtr newItem = malloc(sizeof(struct SortedNode));
// Set new item values
newItem->value = newObj;
newItem->count = 1;
newItem->link = NULL;
// Temp node to traverse through list
SortedNodePtr current = list->head;
// Insert
if(current == NULL)
{
// Point to new item and increment size of list
list->head = newItem;
list->counter++;
return 1;
}
else
{
if(list->compare(current->value, newObj) != 0)
{
if(list->compare(current->value, newObj) < 0)
{
// Add new item to front
newItem->link = list->head;
list->head = newItem;
//Increment size of list
list->counter++;
return 1;
}
// Sort and add
sort(list, newItem);
return 1;
}
}
// ERROR
free(newItem);
return 0;
}
示例2: SLRemove
int SLRemove(SortedListPtr list, void *newObj)
{
SortedNodePtr current = list->head, previous = NULL;
while(current->link != NULL && list->compare(current->value, newObj) != 0)
{
// Store current node into previous
previous = current;
// Move to next node
current = current->link;
}
// Only remove if count on node is one or lower
if(previous == NULL && current->count <= 1)
{
list->head = current->link;
list->destruct(current->value);
free(current);
// Decrement list size
list->counter--;
return 1;
}
else if(current->count <= 1)
{
// Previous points to node after the one being deleted
previous->link = current->link;
// Free the memory
list->destruct(current->value);
free(current);
// Decrement list size
list->counter--;
return 1;
}
else
{
// Make reference count go down by one
current->count--;
}
// Couldn't remove
return 0;
}