本文整理汇总了C++中SortedListPtr::compareFunct方法的典型用法代码示例。如果您正苦于以下问题:C++ SortedListPtr::compareFunct方法的具体用法?C++ SortedListPtr::compareFunct怎么用?C++ SortedListPtr::compareFunct使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedListPtr
的用法示例。
在下文中一共展示了SortedListPtr::compareFunct方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SLRemove
int SLRemove(SortedListPtr list, void *newObj) {
Node *curr;
Node *prev;
curr = list->front;
prev = NULL;
// Search through the list for the node to be removed.
while (curr != NULL) {
// If found remove it from the list.
if (list->compareFunct(curr->content, newObj) == 0) {
// If it is in the front of the list.
if (prev == NULL) {
list->front = curr->next;
curr->ptrCounter--;
if (list->front != NULL) {
list->front->ptrCounter++;
}
}
else {
prev->next = curr->next;
curr->ptrCounter--;
if (prev->next != NULL) {
prev->next->ptrCounter++;
}
}
// If its pointer counter falls to zero, delete it.
if (curr->ptrCounter <= 0) {
list->destroyFunct(curr->content);
if (curr->next != NULL) {
curr->next->ptrCounter--;
}
free(curr);
}
return 1;
}
/*
* If the current content is larger than the content to be removed, move on.
* The node to be removed must lie after this node.
*/
else if (list->compareFunct(curr->content, newObj) > 0) {
prev = curr;
curr = curr->next;
}
/*
* If the current content is less than the content to be removed, simply
* stop. The content to be removed doesn't exist in the list. Return 0.
*/
else {
return 0;
}
}
// Otherwise you couldn't find the element in the list.
return 0;
}
示例2: SLInsert
int SLInsert(SortedListPtr list, void *newObj) {
Node *newNode;
Node *curr;
Node *prev;
curr = list->front;
prev = NULL;
// Creating the node to be inserted.
newNode = (Node *)malloc(sizeof(Node));
// If malloc fails print an error statement and return 0.
if (newNode == NULL) {
//printf("Error: Out of memory.\n");
return 0;
}
// Set the values of the new node.
newNode->content = newObj;
newNode->ptrCounter = 0;
// If the list is empty just put the node in front.
if (list->front == NULL) {
list->front = newNode;
newNode->ptrCounter++;
newNode->next = NULL;
return 1;
}
// Searching through the list for the correct position of the new node.
while (curr != NULL) {
// If a duplicate is found print an error statement and return 0.
if (list->compareFunct(curr->content, newObj) == 0) {
//printf("Error: Duplicate value insertion.\n");
return 0;
}
/*
* If the current content is "smaller" than the content to be inserted, then
* the content to be inserted must come right before the current content.
*/
else if (list->compareFunct(curr->content, newObj) < 0) {
// Check to see if the new node is to be put in the front of the list.
if (prev == NULL) {
list->front = newNode;
newNode->ptrCounter++;
newNode->next = curr;
return 1;
}
// Otherwise just insert the new node.
else {
prev->next = newNode;
newNode->ptrCounter++;
newNode->next = curr;
return 1;
}
}
/*
* If the current content is "bigger" than the content to be inserted, then
* simply continue, fr the correct position must be somewhere after the
* current node.
*/
else {
prev = curr;
curr = curr->next;
}
}
/*
* If you get to the end of the non-empty list and all the content was larger,
* then just throw the new node at the end (it is smaller than everything else
* in the list.
*/
if (curr == NULL && prev != NULL) {
prev->next = newNode;
newNode->ptrCounter++;
newNode->next = curr;
return 1;
}
// Otherwise you somehow failed.
return 0;
}