本文整理汇总了C++中SortedListPtr类的典型用法代码示例。如果您正苦于以下问题:C++ SortedListPtr类的具体用法?C++ SortedListPtr怎么用?C++ SortedListPtr使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SortedListPtr类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例2: 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;
}
示例3: SLRemove
int SLRemove(SortedListPtr list, void *newObj)
{
//If there is no data in the list, or no data at the head node,
if(!list || list->head == NULL || !newObj){ //or no data in the new object, nothing to remove.
return 0;
}
Node *ptr = list->head;
Node *prev = NULL;
while(ptr != NULL){
if(list->cf(ptr->data, newObj) == 0){ //If the Object we are looking to remove is the head node
if(prev == NULL){ //Remove the head node and have the node that comes after take its place
list->head = list->head->next;
if(list->head){
list->head->counter++; //increment counter so the SortedListPtr is pointing to the new head
}
ptr->counter--;
ptr->moveRef = 1;
if(ptr->counter <= 0){
list->df(ptr->data); //If no object is found at the current node, destroy it.
if(ptr->next !=NULL){
ptr->next->counter--; //If there is a ptr to the next node, decrement the reference counter
}
free(ptr); //Free the memory at the ptr if there was no data in the node
return 1;
}
}
else{
prev->next = ptr->next; //If we do not delete the head node, we must find the node we need to delete
if(ptr->next != NULL){ //If we are at the current node and it is not what we are looking for,
ptr->next->counter++; //and the node has a next, we increment reference counter and go to the next node
ptr->counter--;
ptr->moveRef = 1;
}
if(ptr->counter <=0){
list->df(ptr->data);
if(ptr->next != NULL){
ptr->next->counter--;
}
free(ptr);
return 1;
}
}
}
prev = ptr;
ptr = ptr->next;
}
return 0;
}
示例4: 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;
}
示例5: 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;
}
示例6: SLInsert_T
/*SLInsert_T is a function that should only be called internally by SLInsert.
it is used for the recursive cases of insertion.*/
static int SLInsert_T(SortedListPtr list, ListNode prevNode, ListNode curNode, void *newObj){
/*If traversed to node that doesn't exist, failure occurs*/
if (curNode == NULL) return 0;
if ( list->cf(newObj, curNode->item) > 0) {
/*If nowhere else to go, insert at end of list*/
if (curNode->next == NULL){
ListNode lnNew = NodeCreate(newObj, curNode->next);
curNode->next = lnNew;
return 1;
}
/*If new obj is less than current, keep iterating through*/
return SLInsert_T(list, curNode, curNode->next, newObj);
}
/*At this point, new obj is less than or equal current node's obj, insert before it*/
ListNode lnNew = NodeCreate(newObj, curNode->next);
/*At this point, it is in list node and pointing to
predecessor's sucessor*/
prevNode->next = lnNew;
lnNew->next = curNode;
/*Now predecessor points to new object's list node,
and new list node points to the predecessor's previous
successor*/
return 1;
}
示例7: SLInsert
/*-1 if first obj smaller, 0 if equal, 1 if 2nd obj is smaller*/
int SLInsert(SortedListPtr list, void *newObj){
if (newObj == NULL) return 0; /*Disallow null objects storage*/
ListNode curNode = list->begin;
if (curNode == NULL) {
ListNode lnNew = NodeCreate(newObj, list->begin);
list->begin = lnNew;
return 1;
} else if ( list->cf(newObj, curNode->item) < 0) {
/*New obj is greater than head node, becomes new head node*/
ListNode lnNew = NodeCreate(newObj, list->begin);
if (lnNew == NULL) return 0;
list->begin = lnNew;
lnNew->next = curNode;
/*Now predecessor points to new object's list node,
and new list node points to the predecessor's previous
successor*/
return 1;
} else{
return SLInsert_T(list, list->begin, curNode, newObj);
}
}
示例8: SLRemove_T
/*SLRemove_T is a function that should only be called internally by SLRemove.
it is used for the recursive cases of deletion.*/
static ListNode SLRemove_T(SortedListPtr list, ListNode curNode, void *newObj){
if (curNode == NULL) return NULL; /*Unsuccessful*/
/*Compare with comparator function, then remove*/
if ( list->cf(newObj, curNode->item) == 0) {
/*Found node, remove*/
ListNode lsTemp;
lsTemp = curNode->next;
if (curNode->refCount == 1){
free (curNode->item);
free (curNode);
}
return lsTemp;
}
curNode->next = SLRemove_T(list, curNode->next, newObj);
return curNode;
}
示例9: SLRemove
int SLRemove(SortedListPtr list, void *newObj){
ListNode curNode = list->begin;
if ( list->cf(newObj, curNode->item) == 0) {
/*Node to remove is at head*/
list->begin = curNode->next;
if (curNode->refCount == 1){
free (curNode->item);
free (curNode);
}
return 1;
} else {
if ( SLRemove_T(list, curNode, newObj) == NULL){
return 0; /*Unsuccessful*/
} else return 1;
}
}
示例10: SLInsert
int SLInsert(SortedListPtr list, void *newObj)
{
//If there is no list or no new data to insert into the node, return successfully
if(!list || !newObj){
return 0;
}
Node *newNode = nodeMake(newObj); //Create a node object to store the data to be inserted
if(list->head == NULL || list->cf(list->head->data, newObj) < 0){
newNode->next = list->head; //If the data entered is greater than the data held at the head node
list->head = newNode; //Make the new node the head node and make the head the new node
return 1;
}
Node *ptr = list->head; //If the new node is not greater, iterate through the list to determine where to enter the new data
Node *prev= NULL;
while(ptr != NULL){ //While the list is the not at the last node
if(list->cf(ptr->data, newObj) == 0){ //if the data is a duplicate, do not add the node to the list
return 0;
}
else if(list->cf(ptr->data, newObj) < 0){ //if newObj is bigger, insert
Node *newNode = nodeMake(newObj);
if(prev == NULL){ //if the data is bigger than that of the data held at the head node
Node *temp = ptr; //Make the new data the head node and point the old head to the head->next node
list->head = newNode;
newNode->next = temp;
}
prev->next = newNode;
newNode->next = ptr;
newNode->counter++;
return 1;
}
else if(list->cf(ptr->data, newObj) > 0){ //As long as the data is smaller, we must continue to traverse through the list
prev = ptr;
ptr = ptr->next;
}
}
prev->next = newNode;
return 1;
}
示例11: SLDestroy
/*
* SLDestroy destroys a list, freeing all dynamically allocated memory.
*
* You need to fill in this function as part of your implementation.
*/
void SLDestroy(SortedListPtr list) {
Node *temp;
// Runs through the entire list, deleting the front node every time through.
while (list->front != NULL) {
temp = list->front;
list->front = list->front->next;
// Destroying the content pointed to by the node and then freeing the node.
list->destroyFunct(temp->content);
free(temp);
}
// Freeing the list after we destroy all the nodes.
free(list);
}
示例12: SLDestroy
void SLDestroy(SortedListPtr list)
{
node *p = list->base->next;
// destroy every node in the list until reach the end of the list
while (p != list->base) {
p->prev->next = p->next;
p->next->prev = p->prev;
list->DF(p->data);
free(p);
p = list->base->next;
}
free(list->base);
free(list);
}
示例13: SLRemove
int SLRemove(SortedListPtr list, void *newObj)
{
node *p = list->base->next;
// find the first item equal to newObj
while (p != list->base) {
if (list->CF(newObj, p->data) == 0) {
break;
}
p = p->next;
}
// if not find an item equal to newObj
if (p == list->base)
return 0;
p->next->prev = p->prev;
p->prev->next = p->next;
//if there is no iterator "pointing" to this item, free it.
if (p->refByItrCount == 0) {
list->DF(p->data);
free(p);
}
p->next = NULL;
p->prev = NULL;
return 1;
}
示例14: SLDestroy
void SLDestroy(SortedListPtr list)
{
if(!list){
return;
}
//As long as the list is still exsistant, iterate through destroying all node data.
Node *ptr = NULL; //Ptr to the node now points to null
while(list->head != NULL){
list->df(list->head->data); //Use the destroy function
ptr = list->head;
list->head = list->head->next; //The new head of the list is the next node
free(ptr); //free the memory that was allocated for the node ptr
}
free(list); //Free the memory that was allocated for the list
}
示例15: SLInsert
/*
* SLInsert looks through the list until it finds the end of the list or
* a value that is less that newObj. If the value of newObj is already
* in the list, then SLInsert tells the caller that they're an idiot and
* returns zero. If the value of newObj is valid, it is added to the list.
* If it was inserted at the beginning of the list, the list's head pointer
* points to it. Otherwise, the preceeding node points to the node that was
* just added. Finally, the recently added node's numPointers variable
* is incremented.
*/
int SLInsert( SortedListPtr list, void *newObj ) {
NodePtr prev = NULL;
NodePtr current = list->head;
while ( current != NULL && list->cf(current->data, newObj) < 0 ) {
prev = current;
current = current->next;
}
NodePtr n = NCreate( newObj, list->df, current );
if ( prev == NULL ) {
list->head = n;
} else {
prev->next = n;
}
n->numPointers++;
return 1;
}