Example #1
0
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;
}
Example #2
0
int flushCache(int expected, queue* qPtr, sortedPacketCache** cache){
  printf("flushing cache");
  int newExpected = expected + 1;
  if(*cache == NULL)
    return ++expected;
  sortedPacketCache* tmp = *cache;
  while(tmp != NULL){
    printf("Cache seq %d ", tmp->seq);
    tmp = tmp->next;
  }
  printf("\n");
  
  while(*cache != NULL && (*cache)->seq <= newExpected){
    if((*cache)->seq == newExpected)
      newExpected++;
    enqueue(qPtr, removeHead(cache));
  }
  tmp  = *cache;
  while(tmp != NULL){
    printf("After Cache seq %d ", tmp->seq);
    tmp = tmp->next;
  }
  printf("\n");
  printf("old expected %d new expected %d\n", expected, newExpected);
  return newExpected;
}
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;
	}
}
Example #4
0
void SList::clear()
{
    while (head != NULL)
    {
        removeHead();
    }
}//clear the entire contents of the list, freeing all memory
Example #5
0
bool SList::removeFirst (int target) {
	if (head == NULL)
		return false;
	else {
		SLNode* trailer = NULL;
		SLNode* leader = head;
		while (leader != NULL && leader->getContents() != target) {
			trailer = leader;
			leader = leader->getNextNode();
		}
		if (leader == NULL) {
			return false;
		}
		else if (leader == head) {
			removeHead();
			return true;
		}
		else {
			trailer->setNextNode(leader->getNextNode());
			delete leader;
			--numNodes;
			return true;
		}
	}
}
	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;
        
	}
Example #7
0
Foam::SLListBase::link* Foam::SLListBase::remove(SLListBase::link* it)
{
    SLListBase::iterator iter = begin();
    SLListBase::link *prev = &(*iter);

    if (it == prev)
    {
        return removeHead();
    }

    nElmts_--;

    for (++iter; iter != end(); ++iter)
    {
        SLListBase::link *p = &(*iter);

        if (p == it)
        {
            prev->next_ = p->next_;

            if (p == last_)
            {
                last_ = prev;
            }

            return it;
        }

        prev = p;
    }

    return 0;
}
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;
}
Example #9
0
File: ape.cpp Project: edowson/awb
ASIM_INST 
INST_BUF_CLASS::removeHeadAndAbort()
{
  ASSERTX(!is_buf_empty());
  ASIM_INST inst = removeHead();
  free_insts_and_reset();
  return inst;
}
Example #10
0
void Foam::LList<LListBase, T>::clear()
{
    label oldSize = this->size();
    for (label i=0; i<oldSize; i++)
    {
        removeHead();
    }

    LListBase::clear();
}
Example #11
0
 void set(int key, int value) {
     //key already exist
     if(get(key)!=-1){
         key2Node[key]->val=value;
         return;
     }
     //not exist
     if(isFull()) removeHead();
     insertToEnd(key,value);
 }
Example #12
0
//------------------------------------------------------------------------------
// clear() -- clear out (or empty) the list
//------------------------------------------------------------------------------
void List::clear()
{
    // Empty out the list ...
    while (!isEmpty()) {
        Object* p = removeHead(); // First remove them
        if (p != nullptr) {
            p->unref();     // and unref() them
        }
    }
    headP = nullptr;
    tailP = nullptr;
    num = 0;
}
Example #13
0
bigInteger factorial(unsigned long a)
{
    /* Declarations */
    Dlist tmp=malloc(sizeof(Element));
    bigInteger result=malloc(sizeof(nb));
    bigInteger temp=malloc(sizeof(nb));
    unsigned long i,j;
    char string[11];
    char stringr[11];

    /* Need to be a unsigned long int */
    if(a<0)
    {
        return NULL;
    } /* 0! = 1 */
    else if(a==0)
    {
        tmp=insertTail(tmp,1);
        tmp=removeHead(tmp);
        result->absvalue=tmp;
        return result;
    }
    else
    {
        i=1;
        j=0;
        /* Make an array of chars with a */
        while(a>=i)
        {
            string[j]=(a%(i*10)-a%i)/i+'0';
            i*=10;
            j++;
        }
        /* Make a bgiInteger with that array */
        for(i=0; i<j; i++)
        {
            stringr[i]=string[j-i-1];
        }
        stringr[i]='\0';
        result=newBigInteger(stringr);
        /* For k from 1 to a-i, do r=r*k */
        for(i=a-1; i>0; i--)
        {
            tmp=NULL;
            tmp=insertTail(tmp,a-i);
            temp->absvalue=tmp;
            result=mulBigInt(result,temp);
        }
        return result;
    }
}
Example #14
0
int main()
{
    Node *list=NULL;
	createList(&list);
	//printList(list);
	
	addItem(list,"1111");
	addItem(list,"2222");	
	addItem(list,"3333");
	printList(list);
	
	removeHead(&list);
	printList(list);
	reorderPrint(list);
	
	removeHead(&list);
	printList(list);
	reorderPrint(list);
	
//	reorderPrint(list);
	
    return SUCCESS;
}
Example #15
0
void SingleLinkedList<T>::quickSort(){
	Node *temp, *temp2;
	SingleLinkedList l1, l2;
	if (pHead == pTail)
		return;
	temp = pHead;
	pHead = pHead->pNext;
	while (pHead != NULL){
		temp2 = pHead;
		if (temp2->data >= temp->data){
			l1.addHead(temp2->data);
			removeHead();
		}
		else{
			l2.addHead(temp2->data);
			removeHead();
		}
	}
	l1.quickSort();
	l2.quickSort();
	SListAppend(l1);
	addHead(temp->data);
	SListAppend2(l2);
}
Example #16
0
//------------------------------------------------------------------------------
// 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;
}
void SList::removeTail () {
    if(head == NULL){
    
    }
    else if (head->getNextNode()==NULL) {
        removeHead();
    } else {
    SLNode* i = head;
    SLNode* j=NULL;
    while (i->getNextNode() !=NULL) {
        j=i;
        i=i->getNextNode();
    }
    delete i;
    j->setNextNode(NULL);
size--;
         }
}
Example #18
0
void SList::removeTail()
{
    if(head != NULL)
    {
        SLNode* lastNode = head;
        SLNode* nodeToDelete = head;
        if(lastNode->getNextNode() == NULL)
        {
            removeHead();
        }
        else
        {
            while(lastNode->getNextNode()->getNextNode() != NULL)
            {
                lastNode = lastNode -> getNextNode();
            }
            nodeToDelete = lastNode->getNextNode();
            lastNode-> setNextNode(NULL);
            delete nodeToDelete;
            size--;
        }
    }
}
Example #19
0
void SList::removeTail (){
	if (head == NULL) {
	     //DO NOTHING
		} else if (head->getNextNode() == NULL) {
			removeHead();
		} else {
			SLNode* i = head;
			SLNode* j = NULL;
			while (i->getNextNode() != NULL) {
				//j is i and then i moves over one. j tails the node before i. 
				j = i;
			//Starts at head and moves it over to the last node. 
				i = i->getNextNode();
			}
			//i is the tail.
			delete i;
			
			//need j to tail it and null out the last node. 
			j->setNextNode(NULL);
			size--;
	}
		     
}
bool SList::removeFirst(int target) {
    if (head == NULL)
        return false;
    else {
        SLNode* trailer = NULL;
        SLNode* spot = head;
        while (spot != NULL && spot -> getContents() != target) {
            trailer = spot;
            spot = spot -> getNextNode();
        }
    if (spot == NULL)
        return false;
    else if (trailer == NULL) {
        removeHead();
        return true;
    }
    else {
        trailer -> setNextNode(spot -> getNextNode());
        delete spot;
        size--;
        return true;
    }
    }
}
Example #21
0
void SList::clear (){
 	while (head != NULL) {
   		removeHead();
  }
}
Example #22
0
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;
}
Example #23
0
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;
}
Example #24
0
bigInteger quotientBigInt(bigInteger a, bigInteger b)
{
    /* Declarations */
    Dlist l=malloc(sizeof(Element));
    Dlist m=malloc(sizeof(Element));
    Dlist tmp=malloc(sizeof(Element));
    bigInteger plop=malloc(sizeof(nb));
    bigInteger temp=malloc(sizeof(nb));
    bigInteger result=malloc(sizeof(nb));
    int i=0,j=0, depart=0,k=0,compare,X=-1,n;
    int bsign=FALSE;
    int asign=FALSE;
    char* string=NULL;
    char* qr=NULL;
    /* No null bigInteger ? */
    if(a!=NULL && b!= NULL)
    {
        l=a->absvalue;
        m=b->absvalue;
    }
    else
    {
        result=sumBigInt(result,0);
        return result;
    }
    /* No null bigInteger ? (bis) */
    if(m->value == 0 && m->next==NULL)
    {
        tmp=insertTail(tmp,0);
        removeHead(tmp);
        result->absvalue=tmp;
        result->sign=FALSE;
        return result;
    }
    /* Store the signs of a and b, and remove them to the bigInteger to avoid issues with the other fonctions used into quotientBigInt */
    asign=a->sign;
    a->sign=FALSE;
    bsign=b->sign;
    b->sign=FALSE;

    /* If b > a, result will be < 1, not a bigInteger in fact, so 0 */
    if(compareBigInt(a,b) == -1 || isNull(b))
    {
        a->sign=asign;
        if(!isNull(b))
        {
            b->sign=bsign;
        }
        tmp=insertTail(tmp,0);
        removeHead(tmp);
        result->absvalue=tmp;
        result->sign=FALSE;
        return result;
    }


    /* Basic division ? */
    if(l->value<=9999 && l->next==NULL)
    {
        if(asign==bsign)
        {
            result->sign=FALSE;
        }
        else
        {
            result->sign=TRUE;
        }
        a->sign=asign;
        b->sign=bsign;
        tmp=insertTail(tmp,l->value/m->value);
        tmp=removeHead(tmp);
        result->absvalue=tmp;
        return result;
    }

    /* Count the number of characters in a and b and store it into i and j */
    while(m->next!=NULL)
    {
        i+=4;
        m=m->next;
    }
    if(m->value>999)
    {
        i+=4;
    }
    else if(m->value>99)
    {
        i+=3;
    }
    else if(m->value>9)
    {
        i+=2;
    }
    else
    {
        i+=1;
    }
    /* Dynamic allocation for the first number divided by b, we don't need more than the number of characters +1 in b */
    string=malloc((i+1)*sizeof(char));


    while(l->next!=NULL)
    {
        j+=4;
        l=l->next;
    }
    if(l->value>999)
    {
        j+=4;
    }
    else if(l->value>99)
    {
        j+=3;
    }
    else if(l->value>9)
    {
        j+=2;
    }
    else
    {
        j+=1;
    }
    /* Dynamic allocation for the quotient, can't be larger than a */
    qr=malloc(j*sizeof(char));

    /* i is the number of characters of b
    * We take the i first characters of a
    * Then we make an bigInteger with that */
    l=a->absvalue;
    while(l->next!=NULL)
    {
        l=l->next;
    }
    /* First 4 figures's package */
    depart=j%4;
    if(depart==3)
    {
        k+=3;
        string[2]=l->value%10+'0';
        string[1]=(l->value%100-l->value%10)/10+'0';
        string[0]=(l->value%1000-l->value%100)/100+'0';
        l=l->prev;
    }
    else if(depart==2)
    {
        k+=2;
        string[1]=l->value%10+'0';
        string[0]=(l->value%100-l->value%10)/10+'0';
        l=l->prev;
    }
    else if(depart==1)
    {
        k+=1;
        string[0]=l->value%10+'0';
        l=l->prev;
    }
    depart=i;

    /* All the others 4 figures' packages, except the last one */
    while(depart-k>=0)
    {
        string[k+3]=l->value%10+'0';
        string[k+2]=(l->value%100-l->value%10)/10+'0';
        string[k+1]=(l->value%1000-l->value%100)/100+'0';
        string[k]=(l->value-l->value%1000)/1000+'0';
        k+=4;
        if(l->prev!=NULL)
        {
            l=l->prev;
        }
    }

    /* The last one */
    if(k-i==3)
    {
        string[k]=l->value%10+'0';
    }
    else if(k-i==2)
    {
        string[k+1]=l->value%10+'0';
        string[k]=(l->value%100-l->value%10)/10+'0';
    }
    else if(k-i==1)
    {
        string[k+2]=l->value%10+'0';
        string[k+1]=(l->value%100-l->value%10)/10+'0';
        string[k]=(l->value%1000-l->value%100)/100+'0';
    }

    /* Don't forget to close the string */
    string[i]='\0';
    plop=newBigInteger(string);
    /* Our bigInteger is ready */

    /* If our new biginteger is < b, we need to take one more figure from a */
    if(compareBigInt(plop,b)==-1)
    {
        tmp=NULL;
        tmp=insertTail(tmp,10);
        temp->absvalue=tmp;
        plop=mulBigInt(plop,temp);

        X=getNumberN(a,j-i);
        tmp=NULL;
        tmp=insertTail(tmp,X);
        temp->absvalue=tmp;
        plop=sumBigInt(plop,temp);
        i++;
    }

    i--;
    n=0;
    /* Compute the soustraction, like in primary school */
    while(j-i>0)
    {
        i++;
        k=0;
        compare=0;
        /* How many times can I put the quotient ? */
        while(compare>=0 && k<10)
        {
            k++;
            tmp=NULL;
            tmp=insertTail(tmp,k);
            temp->absvalue=tmp;
            temp=mulBigInt(b,temp);

            compare=compareBigInt(plop,temp);
        }
        k--;

        /* Store the figures of the quotient into an array of char */
        tmp=NULL;
        tmp=insertTail(tmp,k);
        temp->absvalue=tmp;
        temp=mulBigInt(b,temp);

        qr[n]=k+'0';
        n++;
        plop=diffBigInt(plop,temp);

        tmp=NULL;
        tmp=insertTail(tmp,10);
        temp->absvalue=tmp;
        plop=mulBigInt(plop,temp);

        /* Go down the next number */
        if(j-i>0)
        {
            X=getNumberN(a,j-i);
        }
        tmp=NULL;
        tmp=insertTail(tmp,X);
        temp->absvalue=tmp;
        plop=sumBigInt(plop,temp);
    }
    /* Close the string */
    qr[n]='\0';
    /* Build the result bigInteger */
    result=newBigInteger(qr);

    a->sign=asign;
    b->sign=bsign;

    /* Put the right sign */
    if(a->sign==b->sign)
    {
        result->sign=FALSE;
    }
    else
    {
        result->sign=TRUE;
    }

    return result;

}
Example #25
0
bigInteger sumBigInt(bigInteger a, bigInteger b)
{
    /* Declarations */
    int ta,tb,r=0,sum;
    bigInteger result=NULL;
    Element* l1=malloc(sizeof(Element));
    Element* l2=malloc(sizeof(Element));
    result=malloc(sizeof(nb));
    /* If one or the two is/are null */
    if(a==NULL || b==NULL)
    {
        if(a==b)
        {
            l1=insertTail(l1,0);
            removeHead(l1);
            result->absvalue=l1;
            return result;
        }
        else if(a==NULL)
        {
            return b;
        }
        else if(b==NULL)
        {
            return a;
        }
    }
    /* Easier to work with DlinkedList instead of bigInteger */
    l1=a->absvalue;
    l2=b->absvalue;
    /* Same sign ? */
    if(a->sign==b->sign)
    {
        /* While the two aren't null */
        while(l1!=NULL || l2!=NULL)
        {
            sum=0;
            /* Make sure to work with a valid value for l1 */
            if(l1==NULL)
            {
                ta=0;
            }
            else
            {
                ta=l1->value;
                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;
            }
            /* Simple sum */
            sum=ta+tb+r;
            /* Rest or not ? */
            if(sum>9999)
            {
                r=1;
                sum-=10000;
            }
            else
            {
                r=0;
            }
            /* Insert the package in a DlinkedList, as an element */
            result->absvalue=insertTail(result->absvalue,sum);
        }
        /* Don't forget the final rest ! */
        if(r>0)
        {
            result->absvalue=insertTail(result->absvalue,1);
        }
        /* Put the right sign to the result */
        result->sign=a->sign;
    }
    else
    {
        /* Contrary signs, so that's a soustraction, let's call diffBigInt with some order and signs changes */
        if(a->sign==TRUE)
        {
            a->sign=FALSE;
            result=diffBigInt(b,a);
            a->sign=TRUE;
        }
        else
        {
            b->sign=FALSE;
            result=diffBigInt(a,b);
            b->sign=TRUE;
        }
    }

    return result;
}
Example #26
0
void SList::clear() {
	while(mHead != 0) {
		removeHead();
	}
}
Example #27
0
/* Función que comprueba si el formato del fichero de configuración es correcto*/
int formatOK(FILE * p, int * errorMemoria)
{

  int numNames = 0; // Número de nombres de la primera linea
  int numInterfaces = 0; // Número de interfaces
  char name[TAM]; // Almacena el nombre de las interfaces para compararlos con los de la primera línea
  char * interfaces = NULL; // Reserva dinámica de memoria para almacenar el nombre de las interfaces
  char head[TAM]; // Cadena que almacena la primera línea del fichero sin en el número de interfaces
  int oct1; // Recoge el primero octeto de la dirección
  int oct2; // Recoge el segundo octeto de la dirección
  int oct3; // Recoge el tercer octeto de la dirección
  int oct4; // Recoge el cuarto octeto de la dirección
  int i = 0; // Contador 1
  int j = 0;  // Contador 2
  int flag = 0; // Bandera 1
  int equal = 0; // Bandera 2
  NODO1 * tableForwarding = NULL; // Puntero al primer elemento de la lista con la tabla de reenvío
  NODO1 * forwardingAux = NULL; // Auxiliar lista con la tabla de reenvío
  NODO1 * dirRepeat = NULL; // Puntero a nodo para recorrer la lista e ir comparando
  NODO3 * first = NULL; // Puntero a lista con cabecera
  NODO3 * aux = NULL; // Auxiliar lista cabecera

  fscanf(p,"%d", &numInterfaces); // Almacena el número de interfaces que debe haber
  
  fgets(head,TAM,p); // Guarda en cabecera la primera línea del fichero (sin el número de interfaces)
  
  for(i=0; i<=TAM; i++)
    {
      if((isspace(head[i]) != 0) && ((head[i+1] >= 'a' && head[i+1] <= 'z') || (head[i+1] >= 'A' && head[i+1] <= 'Z') || (head[i+1] >= '0' && head[i+1] <= '9'))) // El número de nombres de la primera linea es igual al número de espacios
	numNames++;
      else if(head[i] == '\n')
	i = TAM +1; // Hace que finalice el bucle
    }
  i = 0; // Devuelve i a 0
  while(isspace(head[i]) != 0) // Quita los espacios iniciales de la cabecera
    {
      i++;
    }  

  if(numNames == numInterfaces) // Si el número de nombres es igual al de interfaces, hay que comprobar el resto de líneas, si no => Error
    {
      if(fscanf(p,"%d.%d.%d.%d",&oct1,&oct2,&oct3,&oct4) == 4) // Si las direcciones tienen el formato correcto, comprueba los nombres
	{
	  fgets(name, TAM, p);
	  while(isspace(name[j]) != 0) // Quita los espacios del nombre
	    {
	      j++;
	    }

	  while(!feof(p)) // Recorre todo el fichero para comprobar que los nombres han sido dados en la cabecera
	    {
	      while(head[i] != '\n' && equal == 0) 
		{
		  while((name[j] != head[i]) && (head[i] != '\n'))
		    {
		      i++;
		      j = 1;
		    }
		  if(head[i] == '\n')
		    equal = -1;
		  i++;
		  j++;
		  if(name[j] == '\n')
		    {
		      equal = 1;
		      j = 1;		
		      i = 1;
		    }
		}  
	      if((oct1 > 255)||(oct2 > 255)||(oct3 > 255)||(oct4 != 0)) // Comprueba que los octetos no sean mayores de 255
		equal = -1;
	      if((fscanf(p,"%d.%d.%d.%d",&oct1,&oct2,&oct3,&oct4) != 4) && (equal == -1)) // Si el formato de la dirección es erróneo o hay error en los nombres
		flag = 1; // => Error
	      fgets(name, TAM, p);
	      if(equal == 1)	 
		equal = 0;			     
	    }	
	} 
      else // Si el formato de las direcciones es incorrecto => Error
	flag = 1;      
    } 
  else // Si el número de nombres no es igual al de interfaces => Error
    flag = 1;

  if(flag == 0) // Si no ha habido error en los casos contemplados anteriormente, comprobamos otros errores
    {
      interfaces = (char *) calloc(TAM, sizeof(char)); // Reserva dinámica de memoria para almacena el nombre de las interfaces
      if(interfaces != NULL) // Reserva correcta
	{
	  fseek(p, 0, SEEK_SET); // Pone el puntero a fichero de nuevo al principio.
	  fscanf(p,"%d",&numInterfaces); // Almacena el número de interfaces
	  fgets(head, TAM, p); // Lee la primera línea para tener el puntero al comienzo de la tabla
	  fscanf(p,"%d.%d.%d.%d",&oct1,&oct2,&oct3,&oct4); // Almacena la dirección de la siguiente interfaz
	  fscanf(p,"%s",interfaces); // Nombre de la interfaz correspondiente a la dirección anterior
	  while(!feof(p)) // Crea una lista de numInterfaces nodos, para que sirva como tabla de reenvio
	    {
	      forwardingAux = makeNodeConf(oct1, oct2, oct3, oct4, interfaces, errorMemoria); // Crea los nodos
	      linksNodeConf(forwardingAux, &tableForwarding); // Enlaza la lista
	      fscanf(p,"%d.%d.%d.%d",&oct1,&oct2,&oct3,&oct4); // Almacena la dirección de la siguiente interfaz
	      fscanf(p,"%s",interfaces); // Nombre de la interfaz correspondiente a la dirección anterior
	    } 
	  dirRepeat = tableForwarding->sig; // El puntero para comprobar si está repetida va un nodo por delante
	  flag = checkRepeat(tableForwarding, dirRepeat); // Llamada para comprobar si hay alguna dirección repetida
	  if(flag == 0) // Si aún no se ha encontrado el error, seguimos comprobando
	    {
	      fseek(p, 0, SEEK_SET); // Pone el puntero a fichero de nuevo al principio	      
	      fscanf(p,"%d",&numInterfaces); // Almacena el número de interfaces
	      while(numInterfaces > 0) // Bucle para crear una lista con los nombres de la primera linea
		{
		  fscanf(p,"%s",interfaces); // Guarda el primer nombre
		  aux = makeNodeHead(interfaces, errorMemoria); // Crea los nodos de la lista
		  linksNodeHead(aux, &first); // Enlaza los nodos de la lista
		  numInterfaces--; 
		}
	      flag = checkInterface(tableForwarding,first); // Comprueba si hay algún nombre en la primera línea que luego no lleve dirección asignada
	    }
	}
      else // Error de reserva
	{
	printf("Error 6: error de memoria\n");	
	(*errorMemoria) = 1;	
	}    
    }
  free(interfaces); // Libera la reserva dinámica de memoria
  removeHead(&first); // Elimina la lista con la cabecera
  removeConf(&tableForwarding); // Elimina la lista con la tabla de reenvio
  return (flag);
}
Example #28
0
void clearCache(sortedPacketCache **cache){
  while((*cache) != NULL)
    removeHead(cache);
}