int main() { DLL * dll = createDLL(); append(&dll, 1, 1000); append(&dll, 2, 2000); append(&dll, 3, 3000); append(&dll, 4, 4000); append(&dll, 5, 5000); traverseDLL(dll); // 1 2 3 4 5 removeTail(&dll); removeHead(&dll); traverseDLL(dll); // 2 3 4 prepend(&dll, 7, 7000); prepend(&dll, 8, 8000); prepend(&dll, 9, 9000); traverseDLL(dll); // 9 8 7 2 3 4 insertAfterPosition(1, &dll, 99, 9999); traverseDLL(dll); // 9 99 8 7 2 3 4 removeAt(2, &dll); traverseDLL(dll); // 9 8 7 2 3 4 return 0; }
bool SList::removeFirst (int contents) { SLNode* temp=head; SLNode* temp2=temp; while (temp!=NULL && temp->getContents()!=contents) { temp2=temp; temp=temp->getNextNode(); } if (temp==NULL) { return false; } else if (temp==head) { removeHead(); return true; } else if (temp->getNextNode()==NULL) { removeTail(); return true; } else if (temp->getContents()==contents) { temp2->setNextNode(temp->getNextNode()); delete temp; temp=NULL; size--; return true; } return false; }
bool SList::removeFirst(int contents) { SLNode* toRemove = NULL; SLNode* nodeMark = head; SLNode* lagMark = NULL; for (unsigned int i = 0; i < size; i++){ if (nodeMark->getContents() != contents && nodeMark->getNextNode() != NULL){ lagMark = nodeMark; nodeMark = nodeMark->getNextNode(); } else if (nodeMark->getContents() == contents){ toRemove = nodeMark; } } if (toRemove == NULL) { return false; } if (lagMark == NULL){ removeHead(); } else if (toRemove->getNextNode() == NULL) { removeTail(); } else { size--; nodeMark = nodeMark->getNextNode(); lagMark->setNextNode(nodeMark); delete toRemove; } toRemove = NULL; nodeMark = NULL; lagMark = NULL; return true; }
void SingleLinkedList<T>::remove(int pos){ pos--; int iCount = 0; if (IsEmpty()) cout << "Danh sach rong! khong the xoa." << endl; else{ Node* temp = pHead; Node* temp2 = temp; int iCount = 0; if (pos == 0) removeHead(); else if (pos < getLength()){ while (iCount < pos && temp->pNext != NULL){ temp2 = temp; temp = temp->pNext; iCount++; } if (temp == pTail) removeTail(); else{ temp = temp->pNext; temp2->pNext = temp; } } else return; } }
//------------------------------------------------------------------------------ // remove(Item*) -- Removes the Item from the list. // (Ownership passed to caller -- does not unref()) //------------------------------------------------------------------------------ Object* List::remove(List::Item* item) { Object* value = nullptr; if (headP == item) value = removeHead(); else if (tailP == item) value = removeTail(); else if (item != nullptr) { value = item->getValue(); num--; Item* p = item->getPrevious(); Item* n = item->getNext(); n->previous = p; p->next = n; delete item; } return value; }
static void test_removeTail(void **state) { const int a = 1001; LinkedList *list = createList(); assert_non_null(list); for (int i = 0; i < a; i++) { insertTop(list, malloc(sizeof(int))); } assert_int_equal(listLength(list), a); for (int i = a; i > 0; i--) { removeTail(list); } assert_int_equal(listLength(list), 0); destroyList(list); }
void SList::clear() { for (unsigned int i = 0; i < size;){ removeTail(); } }
bigInteger mulBigInt(bigInteger a, bigInteger b) { /* Declarations */ int ta=0,tb=0,r=0,group=0,ins=0; long i=1,j=0; bigInteger result=NULL; bigInteger temp=NULL; Element* l1=malloc(sizeof(Element)); Element* l2=malloc(sizeof(Element)); result=malloc(sizeof(nb)); /* No null bigInteger ? */ if(a!=NULL && b!= NULL) { /* Easier to work with DlinkedList instead of bigInteger */ l1=a->absvalue; l2=b->absvalue; } else if(a==NULL || b==NULL) { l1=insertTail(l1,0); l1=removeHead(l1); result->absvalue=l1; return result; } else { l1=insertTail(l1,0); l1=removeHead(l1); result->absvalue=l1; return result; } temp=malloc(sizeof(nb)); /* If a list is null */ if(l1==NULL || l2==NULL) { return NULL; } /* Make sur to be at the begining of the list */ while(l1->prev != NULL) { l1=l1->prev; } while(l2->prev != NULL) { l2=l2->prev; } /* We need to compute or simple multiplication for each package of b->absvalue */ while(l2 != NULL) { /* Temporary variable */ temp->absvalue=NULL; r=0; /* Put as many 0 as necessary */ for(i=0; i<j; i++) { temp->absvalue=insertTail(temp->absvalue,0); } /* Do the multiplication for each package of a */ while(l1 != NULL) { /* Do the basic multiplication then check for the rest */ ta=l1->value; tb=l2->value; group=0; ins=0; group=group + (ta*tb)+r; l1=l1->next; if(group>9999) { ins=group%10000; r=(group-ins)/10000; /* Insert the 4 figures' package in a DlinkedList, as an element */ temp->absvalue=insertTail(temp->absvalue,ins); } else { r=0; /* Insert the 4 figures' package in a DlinkedList, as an element */ temp->absvalue=insertTail(temp->absvalue,group); } } /* Don't forget the final rest */ if(r>0) { temp->absvalue=insertTail(temp->absvalue,r); } l1=a->absvalue; result=sumBigInt(temp,result); l2=l2->next; j++; } /* Remove useless 0 */ l1=result->absvalue; while(l1->next!=NULL) { l1=l1->next; } while(l1->value==0 && l1->prev!=NULL) { l1=l1->prev; removeTail(l1); } /* Put the right sign to the result */ if(a->sign==b->sign) { result->sign=FALSE; } else { result->sign=TRUE; } return result; }
bigInteger diffBigInt(bigInteger a, bigInteger b) { /* Declarations */ int ta,tb,r=0,diff; bigInteger result=NULL; Element* temp=malloc(sizeof(Element)); Element* l1=malloc(sizeof(Element)); Element* l2=malloc(sizeof(Element)); result=malloc(sizeof(nb)); /* No null bigInteger ? */ if(a!=NULL && b!= NULL) { /* Easier to work with DlinkedList instead of bigInteger */ l1=a->absvalue; l2=b->absvalue; } else { if(a==b) { l1=insertTail(l1,0); removeHead(l1); result->absvalue=l1; return result; } else if(a==NULL) { return b; } else { return a; } } /* Is it really a soustraction ? Let's check that */ if(a->sign==TRUE&&b->sign==FALSE) { a->sign=FALSE; result=sumBigInt(a,b); result->sign=TRUE; a->sign=TRUE; } else if(a->sign==TRUE&&b->sign==TRUE) { a->sign=FALSE; b->sign=FALSE; result=diffBigInt(b,a); a->sign=TRUE; b->sign=TRUE; } else if(a->sign==FALSE&&b->sign==TRUE) { b->sign=FALSE; result=sumBigInt(a,b); b->sign=TRUE; } else { /* If b>a, do b-a and put a '-' before the result */ if(compareBigInt(a,b)==-1) { temp=l1; l1=l2; l2=temp; result->sign=TRUE; } /* Equal ? so no need to compute anything */ else if(compareBigInt(a,b)==0) { result->absvalue=insertTail(result->absvalue,0); result->sign=FALSE; return result; } /* While the two aren't null */ while(l1!=NULL || l2!=NULL) { /* Make sure to work with a valid value for l1 */ if(l1==NULL) { ta=0; } else { ta=l1->value-r; l1=l1->next; } /* Make sure to work with a valid value for l2 */ if(l2==NULL) { tb=0; } else { tb=l2->value; l2=l2->next; } /* Do the soustraction 4 by 4 */ if(ta>tb) { diff=ta-tb; r=0; } else if(tb>ta) { diff=(10000+ta)-tb; r=1; } else { diff=0; r=0; } /* Insert the 4 figures' package in a DlinkedList, as an element */ result->absvalue=insertTail(result->absvalue,diff); } temp=result->absvalue; /* Remove useless 0 */ while(temp->next!=NULL) { temp=temp->next; } while(temp->value==0 && temp->prev!=NULL) { temp=temp->prev; removeTail(temp); } } return result; }
void SingleLinkedList<T>::clear(){ while (!IsEmpty()) removeTail(); }