List MultiplyPolynomial(List PA, List PB) { // Both lists of PA & PB are assumed to be in ascending order. #if _DEBUG int MoveCount = 0; #endif List PResult = MakeNode(); Position P = First(PA), Q; while (P != NULL) { Q = First(PB); Position R = First(PResult); while (Q != NULL) { int Exponent = P->Exponent + Q->Exponent; int Coefficient = P->Coefficient * Q->Coefficient; if (IsLast(PResult)) { R = PResult->Next = MakeNode(); } else { while (!IsLast(R) && R->Next->Exponent <= Exponent) { #if _DEBUG // If PA has less elements than PB, then we can move less here ++MoveCount; #endif R = R->Next; } if (R->Exponent < Exponent) { Position TmpCell = MakeNode(); TmpCell->Next = R->Next; R = R->Next = TmpCell; #if _DEBUG } else if (R->Exponent > Exponent) { std::cout << "The input polynomials may be not in ascending order!" << std::endl; #endif } } R->Exponent = Exponent; R->Coefficient += Coefficient; Q = Q->Next; } P = P->Next; } #if _DEBUG std::cout << "MoveCount: " << MoveCount << std::endl; #endif return PResult; }
void PrintList(List L) { Position P = First(L); while (P != NULL) { if (P->Coefficient > 1) { std::cout << P->Coefficient; } if (P->Exponent > 0) { if (P->Exponent > 1) { std::cout << "x^" << P->Exponent; } else { std::cout << "x"; } } if (!IsLast(P)) { std::cout << " + "; } else { std::cout << std::endl; } P = P->Next; } }
/** *Find the previous Node in the List */ Position FindPrevious(int X,List L) { Position P=L; while(!IsLast(P->Next)&&P->Next->Element!=X) P=P->Next; return P; }
void Texture::SetNext ( Texture *pTexture_ ) { if ( IsLast ( ) ) { m_pNext = pTexture_; return; } // // Insert the given list // if ( pTexture_ != NULL ) { Texture *pTexture = pTexture_; while ( !pTexture->IsLast ( ) ) { pTexture = pTexture->GetNext ( ); } pTexture->SetNext ( m_pNext ); } m_pNext = pTexture_; }
/** *Find the X value in the linkedList Return null if not find */ Position Find(int X,List L) { Position P=L->Next; while(!IsLast(P) && P->Element!=X) P=P->Next; return P; }
/* remove item from list */ void ListTakeOut ( LinkedList list, /* list handler */ MemPtr item /* list item */ ) { if ( list != NULL && item != NULL ) { ListNode* node; ListNode* prev; node = list->first; prev = NULL; while ( node != NULL && node->data != item ) { prev = node; node = node->next; } if ( node != NULL ) { if ( IsFirst( list, node ) ) list->first = node->next; if ( IsLast( list, node ) ) list->last = prev; if ( prev != NULL ) prev->next = node->next; SafeMemPtrFree( node ); list->count--; } } }
/** *Find the last Node int the List **/ Position FindLast(List L) { Position P=L->Next; while(!IsLast(P)) P=P->Next; return P; }
void Poly::WritePoly ( ofstream &ofsFile_ ) const { /* Polygon: 1 uint Texture ID 1 Plane Polygon plane 1 uint Number of vertices x Vertex Vertices */ ofsFile_.write ( ( char * )&TextureID, sizeof ( unsigned int ) ); ofsFile_.write ( ( char * )&plane.n.x, sizeof ( double ) ); ofsFile_.write ( ( char * )&plane.n.y, sizeof ( double ) ); ofsFile_.write ( ( char * )&plane.n.z, sizeof ( double ) ); ofsFile_.write ( ( char * )&plane.d, sizeof ( double ) ); unsigned int ui = ( unsigned int )GetNumberOfVertices ( ); ofsFile_.write ( ( char * )&ui, sizeof ( ui ) ); for ( int i = 0; i < GetNumberOfVertices ( ); i++ ) { ofsFile_.write ( ( char * )&verts[ i ].p.x, sizeof ( double ) ); ofsFile_.write ( ( char * )&verts[ i ].p.y, sizeof ( double ) ); ofsFile_.write ( ( char * )&verts[ i ].p.z, sizeof ( double ) ); ofsFile_.write ( ( char * )&verts[ i ].tex[ 0 ], sizeof ( double ) ); ofsFile_.write ( ( char * )&verts[ i ].tex[ 1 ], sizeof ( double ) ); } if ( !IsLast ( ) ) { GetNext ( )->WritePoly ( ofsFile_ ); } }
Position FindPrevious(int X, List L){ Position P; P = L; while(!IsLast(P, L) && P->next->element != X) P = P->next; return P; }
Texture::~Texture ( ) { if ( !IsLast ( ) ) { delete m_pNext; m_pNext = NULL; } }
void Delete(ElementType X, List L){ Position P, TmpCell; P = FindPrevious(X, L); if(!IsLast(P, L)){ TmpCell = P->Next; P->Next = TmpCell->Next; free(TmpCell); } }
void nsGenConList::Insert(nsGenConNode* aNode) { if (mFirstNode) { // Check for append. if (NodeAfter(aNode, Prev(mFirstNode))) { PR_INSERT_BEFORE(aNode, mFirstNode); } else { // Binary search. // the range of indices at which |aNode| could end up. // (We already know it can't be at index mSize.) PRUint32 first = 0, last = mSize - 1; // A cursor to avoid walking more than the length of the list. nsGenConNode *curNode = Prev(mFirstNode); PRUint32 curIndex = mSize - 1; while (first != last) { PRUint32 test = (first + last) / 2; if (last == curIndex) { for ( ; curIndex != test; --curIndex) curNode = Prev(curNode); } else { for ( ; curIndex != test; ++curIndex) curNode = Next(curNode); } if (NodeAfter(aNode, curNode)) { first = test + 1; // if we exit the loop, we need curNode to be right ++curIndex; curNode = Next(curNode); } else { last = test; } } PR_INSERT_BEFORE(aNode, curNode); if (curNode == mFirstNode) { mFirstNode = aNode; } } } else { // initialize list with first node PR_INIT_CLIST(aNode); mFirstNode = aNode; } ++mSize; NS_ASSERTION(aNode == mFirstNode || NodeAfter(aNode, Prev(aNode)), "sorting error"); NS_ASSERTION(IsLast(aNode) || NodeAfter(Next(aNode), aNode), "sorting error"); }
/* Delete first occurance of X * */ void Delete(int X, List L){ Position P,tmp; P = FindPrevious(X, L); if(!IsLast(P, L)){ tmp = P->next; P->next = tmp->next; free(tmp); } }
void Delete(int x,List L)//删除链表中的x元素 { Position P,TmpCell; P=FindPrevious(x, L); if (!IsLast(P, L)) { TmpCell=P->Next; P->Next=TmpCell->Next; free(TmpCell); } }
void Delete(int x,list l){ position p,temp; p = FindPrevious(int x,list l); if(!IsLast(p,l)){ temp = p->next; p->next = temp->next; free(temp); } }
/*delete first occurrence of e from list L*/ void Delete(List L, ElemType e) { Position P, tmp; P = FindPrevious(L, e); if(!IsLast(P, L)){ //e is found, delete it tmp = P->next; P->next = tmp->next; free(tmp); } }
/** *delete the node where the value=x */ void Delete(int X,List L) { Position P ,TmpNode; P=FindPrevious(X,L); if(!IsLast(P)) { TmpNode=P->Next; P->Next=TmpNode->Next; free(TmpNode); } }
void Delete(int x, List L) { Position p, Tmp; p = FindPrevious(x, L); if (!IsLast(p, L)) { Tmp = p->Next; p->Next = Tmp->Next; free(Tmp); } }
void Delete( ElementType X, adjustList L ) { Position Pre; Pre = FindPrevious( X, L ); if( !IsLast( Pre, L ) ) { Position TmpCell = Pre->Next; Pre->Next = TmpCell->Next; free( TmpCell ); } }
void Delete(List L, ElementType X){ Position P, tmpCell; P = FindPrevious(L, X); if ( !IsLast(L,P)) { tmpCell = P->next; P->next = tmpCell->next; free(tmpCell); } }
void Delete(int x, List L) { Position P, TemCell; P = FindPrevious(x, L); if( !IsLast(P,L)){ TemCell = P->Next; P->Next = TemCell->Next; free(TemCell); } }
void Delete(ElementType X, List L) { Position p, temp; p = FindPrevious(X, L); if (IsLast(p->next, L)) { p->next = NULL; } else { // error: p->next = p->next->next; temp = p->next; p->next = temp->next; free(temp); } }
void Delete(ElementType X, List L){ Position P, TmpCell; P = FindPrevious(X, L); if(!IsLast(P, L)){ // Assumption of header use TmpCell = CursorSpace[P].Next; CursorSpace[P].Next = CursorSpace[TmpCell].Next; CursorFree(TmpCell); } }
void PrintList(const List L) { Position P = Header(L); if(IsEmpty(L)) { printf("Empty List!\n"); }else { do{ P = Advance(P); printf("%d ", Retrieve(P)); }while(!IsLast(P, L)); printf("\n"); } }
void Delete (ListElementType X, List L) { Position P, TmpCell; P = FindPrevious (X, L); if (!IsLast (P, L)) { TmpCell = CursorSpace[P].Next; CursorSpace[P].Next = CursorSpace[TmpCell].Next; CursorFree (TmpCell); } }
void ListTraverse(const List list) { Position p = Header(list); if(IsEmpty(list)) printf("Empty list\n"); else { do{ p = Advence(p); printf("%d ", Retrieve(p)); } while(!IsLast(p, list)); printf("\n"); } }
void Delete( ElementType X, List L ) { Position P, TmpCell; P = FindPrevious( X, L ); if( !IsLast( P, L ) ) /* Assumption of header use */ { /* X is found; delete it */ TmpCell = P->Next; P->Next = TmpCell->Next; /* Bypass deleted cell */ free( TmpCell ); } }
void PrintPolynomial(const Polynomial Poly) { Polynomial Tmp; if (IsLast(Poly)) { printf("empty polynomial\n"); } else{ Tmp = Poly; do { Tmp = Tmp->Next; printf("%d*x^%d\t", Tmp->Coefficient, Tmp->Exponent); } while(Tmp->Next != NULL); printf("\n"); } }
Poly::~Poly ( ) { if ( !IsLast ( ) ) { delete m_pNext; m_pNext = NULL; } if ( verts != NULL ) { delete [] verts; verts = NULL; m_iNumberOfVertices = 0; } }
/** *Find the Node in the certain positin */ int FindPosition(int Pos,List L) { int count=1; Position P=L->Next; while(count<Pos && !IsLast(P)) { P=P->Next; count++; if(P==NULL) { printf("%s\n","out of space!" ); break; return -1; } } return P->Element; }