コード例 #1
0
ファイル: ex-3.7-2.cpp プロジェクト: riophae/exercises
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;
}
コード例 #2
0
ファイル: ex-3.7-2.cpp プロジェクト: riophae/exercises
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;
  }
}
コード例 #3
0
ファイル: List.c プロジェクト: zyl111ok/DSAlgorithm
/**
*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;
}
コード例 #4
0
ファイル: texture.cpp プロジェクト: stefanha/map-files
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_;
}
コード例 #5
0
ファイル: List.c プロジェクト: zyl111ok/DSAlgorithm
/**
*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;
}
コード例 #6
0
ファイル: list.c プロジェクト: TimofonicJunkRoom/plucker
/* 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--;
        }
    }
}
コード例 #7
0
ファイル: List.c プロジェクト: zyl111ok/DSAlgorithm
/**
*Find the last Node int the List
**/
Position FindLast(List L)
{
	Position P=L->Next;
	while(!IsLast(P))
		P=P->Next;
	return P;
}
コード例 #8
0
ファイル: poly.cpp プロジェクト: stefanha/map-files
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_ );
	}
}
コード例 #9
0
ファイル: _List.c プロジェクト: BitVoyage/MyAlgorithm
Position
FindPrevious(int X, List L){
    Position P;
    P = L;
    while(!IsLast(P, L) && P->next->element != X)
        P = P->next;
    return P;
}
コード例 #10
0
ファイル: texture.cpp プロジェクト: stefanha/map-files
Texture::~Texture ( )
{
	if ( !IsLast ( ) )
	{
		delete m_pNext;
		m_pNext = NULL;
	}
}
コード例 #11
0
ファイル: list.c プロジェクト: githubcai/Data-Structures
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);
    }
}
コード例 #12
0
ファイル: nsGenConList.cpp プロジェクト: ahadzi/celtx
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");
}
コード例 #13
0
ファイル: _List.c プロジェクト: BitVoyage/MyAlgorithm
/* 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);
    }
}
コード例 #14
0
ファイル: list.c プロジェクト: tianyayuan/data_struct
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);
    }
}
コード例 #15
0
ファイル: a.c プロジェクト: buzzLY/C_Directory
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);
	}

}
コード例 #16
0
ファイル: list.c プロジェクト: shawnelee88/dsacc
/*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);       
	}
}
コード例 #17
0
ファイル: List.c プロジェクト: zyl111ok/DSAlgorithm
/**
*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);
	}
}
コード例 #18
0
ファイル: list.cpp プロジェクト: GeorgeZhuo/Datastructure
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);
    }
}
コード例 #19
0
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 );
    }
}
コード例 #20
0
ファイル: 3_table.c プロジェクト: zjsrose/zjsheng_blog
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);
  }
}
コード例 #21
0
ファイル: list.c プロジェクト: alyadan/answer-of-D
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);
	}
}
コード例 #22
0
ファイル: 7.a.c プロジェクト: devilTian/Fragment
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);
	}
}
コード例 #23
0
ファイル: cursor.c プロジェクト: niceforbear/lib
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);
    }
}
コード例 #24
0
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");
    }
}
コード例 #25
0
ファイル: list.c プロジェクト: 4179e1/misc
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);
	}
}
コード例 #26
0
ファイル: linklist.c プロジェクト: joizhang/study
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");
    }
}
コード例 #27
0
ファイル: list.c プロジェクト: huangqiheng/andpack
        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 );
            }
        }
コード例 #28
0
ファイル: testpolynomial.c プロジェクト: leechannl/learning
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");
    }
}
コード例 #29
0
ファイル: poly.cpp プロジェクト: stefanha/map-files
Poly::~Poly ( )
{
	if ( !IsLast ( ) )
	{
		delete m_pNext;
		m_pNext = NULL;
	}

	if ( verts != NULL )
	{
		delete [] verts;
		verts = NULL;
		m_iNumberOfVertices = 0;
	}
}
コード例 #30
0
ファイル: List.c プロジェクト: zyl111ok/DSAlgorithm
/**
*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;
}