Exemplo n.º 1
0
int addList(List* l, ItemType e, int index) {
    if (index > sizeList(l) || index < 0) {
        return 0;
    }
    Node* node = createNode();
    node->data = e;
    node->next = NULL;
    if (isEmptyList(l)) {
        l->first = node;
        l->last = node;
    } else {
        Node* iterador = createNode();
        iterador = l->first;
        int i = 0;
        while (i < index) {
            i++;
            iterador = iterador->next;
        }
        if (index == 0) {
            node->next = iterador;
            l->first = node;
        } else if (index == sizeList(l)) {
            iterador->next = node;
            l->last = node;
        } else {
            node->next = iterador->next;
            iterador->next = node;
        }
        free(iterador);
    }
    l->size++;
    return 1;
}
Exemplo n.º 2
0
/* List Node Test function */
void list_node_test(void)
{
	Node_t *pList = NULL;
	elemType data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	elemType *ret_data;

	initList(&pList);
	pList = createList(pList, data, 10L);
	printList(pList);
	lk_print("The size of pList: %ld.\n", sizeList(pList));
	lk_print("getElement(pList, 4) = %d.\n", getElement(pList, 4));
	ret_data = getDataFromList(pList, 4);
	lk_print("ret_data[4] = %d.\n", *ret_data);
	setDataByPosition(pList, 4, 0);
	printList(pList);
	insertHeadNode(&pList, 9);
	lk_print("The size of pList: %ld.\n", sizeList(pList));
	printList(pList);
	insertLastNode(&pList, 0);
	printList(pList);
	lk_print("The size of pList: %ld.\n", sizeList(pList));
	insertNodeByPosition(&pList, 14, 8);
	printList(pList);
	deleteNodeByPosition(&pList, 12);
	printList(pList);
	swapTwoNodeElemInList(&pList, 10, 10);
	printList(pList);
	clearList(pList);
	lk_print("done\n");
}
Exemplo n.º 3
0
/* 12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0 */
int insertNodeByPosition(Node_t **pHead, long pos, elemType x)
{
	Node_t *insertNode, *p;
	long i = 1;

	p = *pHead;
	if(p == NULL || pos < 0 || (pos > sizeList(*pHead) + 1))
		return 0;
	insertNode = (Node_t *)malloc(sizeof(Node_t));
	if(insertNode == NULL)
		return 0;
	insertNode->element = x;
	if(pos == 0)
	{
		insertNode->next = *pHead;
		*pHead = insertNode;
		return 1;
	}
	while(i < pos - 1)
	{
		p = p->next;
		i++;
	}
	insertNode->next = p->next;
	p->next = insertNode;

	return 1;
}
Exemplo n.º 4
0
/* 16.从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行 */
int deleteNodeByPosition(Node_t **pHead, long pos)
{
	Node_t *delNode, *p;
	long i = 1;

	p = *pHead;
	if(p == NULL || pos < 0 || pos > sizeList(p))
		return 0;
	if(pos == 0)
	{
		*pHead = (*pHead)->next;
		free(p);
		return 1;
	}
	while(i < pos - 1)
	{
		p = p->next;
		i++;
	}
	delNode = p->next;
	p->next = delNode->next;
	free(delNode);

	return 1;
}
Exemplo n.º 5
0
/* 18.交换2个元素的位置 */
int swapTwoNodeElemInList(Node_t **pHead, long pos_m, long pos_n)
{
	Node_t *p1, *p2;
	elemType temp;
	long i = 1;

	p1 = *pHead;
	if(pos_m > pos_n)
	{
		i = pos_m;
		pos_m = pos_n;
		pos_n = i;
	}
	if((p1 == NULL) || (pos_m == pos_n) || pos_m < 1 || pos_n > sizeList(p1))
		 return 0;
	//printf("pos_m:%ld, pos_n:%ld.\n", pos_m, pos_n);
	i = 1;
	while(i++ < pos_m) p1 = p1->next;
	p2 = p1->next;
	while(i++ < pos_n) p2 = p2->next;

	temp = p1->element;
	p1->element = p2->element;
	p2->element = temp;

	return 1;
}
Exemplo n.º 6
0
/* Renvoi 1 si c'est un palindrome, 0 dans les autres cas */
int isPalindrome(t_ptr_liste liste) {
    int res = 1;
    int med = sizeList(liste);
    int cpt;
    int i;
    t_ptr_liste tmp = liste;
    t_ptr_liste debutListe = liste;

    if(med%2 == 0)
        med = med/2;
    else
        med = (med+1)/2;

    cpt = med-1;
    for(i = 0; i < med; ++i)
        tmp = tmp->next;

    while(tmp != NULL && res == 1) {
        for(i = 0; i < cpt-1; ++i)
            debutListe = debutListe->next;
    
        if(tmp->value != debutListe->value)
            res = 0;
        tmp = tmp->next;
        debutListe = liste;

        cpt = cpt - 1;
    }

    return res;
}
Exemplo n.º 7
0
void Prefs_PageSizes::restoreDefaults(struct ApplicationPrefs *prefsData)
{
	PageSize ps(prefsData->docSetupPrefs.pageSize);
	QStringList sizeList(ps.sizeList());
	QStringList activeSizeList(ps.activeSizeList());
	activeSizesListWidget->clear();
	availableSizesListWidget->clear();

	for (int i = 0; i < activeSizeList.count(); ++i)
	{
		QListWidgetItem* lwi=new QListWidgetItem();
		PageSize ps2(activeSizeList.at(i));
		lwi->setText(ps2.nameTR());
		lwi->setToolTip(QString("%1 x %2 %3").arg(ps2.originalWidth()).arg(ps2.originalHeight()).arg(ps2.originalUnit()));
		activeSizesListWidget->addItem(lwi);
	}

	for (int i = 0; i < sizeList.count(); ++i)
	{
		if (!activeSizeList.contains(sizeList.at(i)))
		{
			QListWidgetItem* lwi=new QListWidgetItem();
			PageSize ps2(sizeList.at(i));
			lwi->setText(ps2.nameTR());
			lwi->setToolTip(QString("%1 x %2 %3").arg(ps2.originalWidth()).arg(ps2.originalHeight()).arg(ps2.originalUnit()));
			availableSizesListWidget->addItem(lwi);
		}
	}
}
Exemplo n.º 8
0
int removeList(List* l, int index, ItemType* e) {
    if (index > l->size || index < 0) {
        return 0;
    }
    Node *node, *removed;
    node = createNode();
    removed = createNode();
    node = l->first;
    int i = 0;
    while (i < index) {
        i++;
        node = node->next;
    }
    if (index == 0) {
        removed = l->first;
        l->first = removed->next;
    } else if (index == sizeList(l)) {
        removed = node->next;
        node->next = NULL;
        l->last = node;
    } else {
        removed = node->next;
        node->next = removed->next;
    }
    if (isEmptyList(l)) {
        l->first = NULL;
        l->last = NULL;
    }
    *e = removed->data;
    free(node);
    free(removed);
    l->size--;
    return 1;
}
Exemplo n.º 9
0
 void reorderList(ListNode* head) {
   ListNode* first = nullptr, *second = nullptr;
   ListNode** tail = &first;
   int id = 0, size = sizeList(head);
   for (ListNode* i = head; i != nullptr;) {
     ListNode* next = i->next;
     if (id < size / 2) {
       *tail = i;
       tail = &i->next;
     } else {
       i->next = second;
       second = i;
     }
     i = next;
     id++;
   }
   *tail = nullptr;
   tail = &head;
   id = 0;
   while (first != nullptr && second != nullptr) {
     ListNode** k = (id & 1) == 1 ? &second : &first;
     *tail = *k;
     tail = &(*k)->next;
     *k = (*k)->next;
     id++;
   }
   *tail = (first == nullptr ? second : first);
 }
Exemplo n.º 10
0
void main()
{
    int a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    int i;
    struct List L;
    initList(&L, 5);
    for(i = 0; i < 10; i++){
        insertLastList(&L, a[i]);
    }
    insertPosList(&L, 11, 48);        insertPosList(&L, 1, 64);
    printf("%d ", getElem(&L, 1));
    traverseList(&L);
    printf("%d ", findList(&L, 10));
    updatePosList(&L, 3, 20);
    printf("%d ", getElem(&L, 3));
    traverseList(&L);
    deleteFirstList(&L);            deleteFirstList(&L);
    deleteLastList(&L);                deleteLastList(&L);
    deletePosList(&L, 5);            ;deletePosList(&L, 7);
    printf("%d ", sizeList(&L));
    printf("%d ", emptyList(&L));
    traverseList(&L);
    clearList(&L);
    return 0;
}
Exemplo n.º 11
0
// Utility function to display the entire list forward
static void displayListForward(LinkedListP list)
{
    int i, count = sizeList(list);
    printf("Count is: %i, ", count);
    printf("Here's the list forward:\n");
    firstItemList(list);
    for(i=0; i<count; i++)
    {
        printf("   %s ", getItemList(list));
        nextItemList(list);
    }
    printf("\n");
}
Exemplo n.º 12
0
// Utility function to display the entire list backward
static void displayListBackward(LinkedListP list)
{
    int i, count = sizeList(list);
    printf("Count is: %i, ", count);
    printf("Here's the list backward:\n");
    lastItemList(list);
    for( i=0; i<count; i++ )
    {
        printf("   %s ", getItemList(list));
        previousItemList(list);
    }
    printf("\n");
}
Exemplo n.º 13
0
QStringList PageSize::activeSizeList() const
{
	QStringList pageSizes=sizeList();
	if (PrefsManager::instance()->appPrefs.activePageSizes.count()==0)
		return QStringList(pageSizes);
	QStringList activePageSizes(PrefsManager::instance()->appPrefs.activePageSizes);
	QStringList activeSizes;
	for (int i = 0; i < activePageSizes.size(); ++i)
	{
		if (pageSizes.contains(activePageSizes.at(i)))
			activeSizes << activePageSizes.at(i);
	}
	return QStringList(activeSizes);
}
Exemplo n.º 14
0
QStringList PageSize::untransPageSizeList(const QStringList &transList)
{
	QStringList pageTRSizes=sizeTRList();
	QStringList pageSizes=sizeList();
	QStringList untranslatedList;
	for (int i = 0; i < transList.size(); ++i)
	{
		int j=pageTRSizes.indexOf(transList.at(i));
		if (j!=-1)
		{
			untranslatedList << pageSizes.at(j);
		}
	}
	return QStringList(untranslatedList);
}
Exemplo n.º 15
0
/* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
int setDataByPosition(Node_t *pHead, long pos, elemType x)
{
	Node_t *p;
	long i = 1;

	p = pHead;
	if(p == NULL || pos < 1 || pos > sizeList(p))
		return 0;
	while(i < pos && p != NULL)
	{
		p = p->next;
		i++;
	}
	p->element = x;
	return 1;
}
Exemplo n.º 16
0
/* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
elemType getElement(Node_t *pHead, long pos)
{
	long i = 1, lenOfList;
	Node_t *p;
	lenOfList = sizeList(pHead);
	if(pos < 1 || pos > lenOfList || pHead == NULL)
	{
		lk_print("pos < 1 or pHead = null, return.\n");
		return 0;
	}
	p = pHead;
	while(i < pos)
	{
		p = p->next;
		i++;
	}
	return p->element;
}
Exemplo n.º 17
0
QStringList PageSize::activeSizeTRList() const
{
	QStringList pageTRSizes=sizeTRList();
	QStringList pageSizes=sizeList();
	if (PrefsManager::instance()->appPrefs.activePageSizes.count()==0)
		return QStringList(pageTRSizes);
	QStringList activePageSizes(PrefsManager::instance()->appPrefs.activePageSizes);
	QStringList activeTRSizes;
	for (int i = 0; i < activePageSizes.size(); ++i)
	{
		int j=pageSizes.indexOf(activePageSizes.at(i));
		if (j!=-1)
		{
			activeTRSizes << pageTRSizes.at(j);
		}
	}
	return QStringList(activeTRSizes);
}
Exemplo n.º 18
0
int main() 
{ 
    int len; 
    int key,block,blk; 
    struct Node *mylist = NULL; 
    struct Node *p; 
    puts("输入数字开始创建链表,以小于零的数结束创建:"); 
    p = creat(mylist); 
    len = sizeList(p); 
    add[0] = (int *)p; 
    display(p); 
    puts("输入需要查找的数字:\n"); 
    scanf("%d",&blk); 
    puts("输入块的大小:\n"); 
    scanf("%d",&blk); 
    puts("输入需要查找的块:\n"); 
    scanf("%d",&block); 
    BlkSearch(p,key,block,blk); 
    return 0; 
} 
CrsMatrix_SubCopy::NewTypeRef
CrsMatrix_SubCopy::
operator()( OriginalTypeRef orig )
{
  origObj_ = &orig;

  //Error, must be local indices
  assert( orig.Filled() );

  //test maps, new map must be subset of old
  const Epetra_Map & oRowMap = orig.RowMap();
  const Epetra_Map & oColMap = orig.ColMap();

  int oNumRows = oRowMap.NumMyElements();
  (void) oNumRows; // Silence "unused variable" compiler warning.
  int oNumCols = oColMap.NumMyElements();
  int nNumRows = newRowMap_.NumMyElements();
  int nNumDomain = newDomainMap_.NumMyElements();

  bool matched = true;

  // Make sure all rows in newRowMap are already on this processor
  for( int i = 0; i < nNumRows; ++i )
    matched = matched && ( oRowMap.MyGID(newRowMap_.GID(i)) );
  if( !matched ) std::cerr << "EDT_CrsMatrix_SubCopy: Bad new_row_Map.  GIDs of new row map must be GIDs of the original row map on the same processor.\n";

  // Make sure all GIDs in the new domain map are GIDs in the old domain map
  if( !newRangeMap_.SameAs(newDomainMap_) ) {
    Epetra_IntSerialDenseVector pidList(nNumDomain);
    oColMap.RemoteIDList(newDomainMap_.NumMyElements(), newDomainMap_.MyGlobalElements(), pidList.Values(), 0);
    for( int i = 0; i < nNumDomain; ++i )
      matched = matched && ( pidList[i]>=0 );
  }

  if( !matched ) std::cout << "EDT_CrsMatrix_SubCopy: Bad newDomainMap.  One or more GIDs in new domain map are not part of original domain map.\n";
  assert( matched );


  // Next build new column map
  Epetra_IntSerialDenseVector pidList(oNumCols);
  Epetra_IntSerialDenseVector lidList(oNumCols);
  Epetra_IntSerialDenseVector sizeList(oNumCols);
  newDomainMap_.RemoteIDList(oColMap.NumMyElements(), oColMap.MyGlobalElements(), pidList.Values(), 0);
  int numNewCols = 0;
  Epetra_IntSerialDenseVector newColMapGidList(oNumCols);
  int * origColGidList = oColMap.MyGlobalElements();
  for( int i = 0; i < oNumCols; ++i )
    if (pidList[i] >=0)
      newColMapGidList[numNewCols++]= origColGidList[i];
  newColMap_ = Epetra_Map(-1, numNewCols, newColMapGidList.Values(), 0, oColMap.Comm());

  importer_ = new Epetra_Import(newRowMap_, oRowMap);

  Epetra_CrsMatrix * newMatrix = new Epetra_CrsMatrix(Copy, newRowMap_, newColMap_, 0);

  newObj_ = newMatrix;

  newObj_->Import(*origObj_, *importer_, Add);

  newObj_->FillComplete();

  return *newObj_;
}
Exemplo n.º 20
0
int main() {
    /* Création d'une liste */
    
    t_ptr_liste liste = creationListe();
    
    /* Affichage d'une liste */
    
    afficher(liste);

    /* Longeur de la liste */
    
    printf("Longeur de la liste = %d\n", sizeList(liste));
    
    /* Copie de la liste */

    t_ptr_liste newList = copyList(liste);
    printf("New Liste = ");
    afficher(newList);
    
    /* Recherche d'une élément dans une liste */
        
    printf("Pos de 4 = %d\n", findListe(liste, 4));
    
    /* Suppression d'un élément d'un liste */
    
    printf("Suppression de tous les 5 :\n");
    liste = delElem(liste, 5);
    afficher(liste);
    
    /* Insertion dans une liste ordonnée */
    
    t_ptr_liste listeOrd = creationListeOrdonne();
    afficher(listeOrd);
    
    /* Inverse d'une liste */
    
    printf("Inversion de la liste ordonnée :\n");
    listeOrd = inversionListeADT(listeOrd);
    afficher(listeOrd);

    /* Vérification si c'est un palindrome */
    t_ptr_liste palin = NULL;
    palin = ajout_liste(palin, 1);
    palin = ajout_liste(palin, 2);
    palin = ajout_liste(palin, 3);
    palin = ajout_liste(palin, 4);
    palin = ajout_liste(palin, 3);
    palin = ajout_liste(palin, 2);
    palin = ajout_liste(palin, 1);

    printf("Palin :\n");
    afficher(palin);
    printf("La liste palin est un paladrome ? ");
    (isPalindrome(palin) ? printf("Oui\n") : printf("Non\n"));

    t_ptr_liste paPalin = NULL;
    paPalin = ajout_liste(paPalin, 1);
    paPalin = ajout_liste(paPalin, 2);
    paPalin = ajout_liste(paPalin, 3);
    paPalin = ajout_liste(paPalin, 4);
    printf("Pas Palin :\n");
    afficher(paPalin);
    printf("La liste papalin est un paladrome ? ");
    (isPalindrome(paPalin) ? printf("Oui\n") : printf("Non\n"));

    /* Vérification que la liste est une suite de Fibonacci */
    t_ptr_liste fibList = NULL;
    fibList = ajout_liste(fibList, 3);
    fibList = ajout_liste(fibList, 2);
    fibList = ajout_liste(fibList, 1);
    fibList = ajout_liste(fibList, 1);
    fibList = ajout_liste(fibList, 0);
    printf("fibList :\n");
    afficher(fibList);
    printf("La liste fibList est une liste de Fibonacci ? ");
    (isFibonnacciList(fibList) ? printf("Oui\n") : printf("Non\n"));

    /* Fusion de deux liste triée */
    printf("Fusion de listeOrd et de paPalin\n");
    t_ptr_liste testFusion = fusionList(listeOrd, paPalin);
    afficher(testFusion);

    printf("Fusion de listeOrd et de paPalin (sans répétition)\n");
    testFusion = fusionListNoRepeat(listeOrd, paPalin);
    afficher(testFusion);

    /* Liste des sommes préfixe */
    t_ptr_liste listeSomme = NULL;
    listeSomme = ajout_liste(listeSomme, 3);
    listeSomme = ajout_liste(listeSomme, 5);
    listeSomme = ajout_liste(listeSomme, 4);
    listeSomme = ajout_liste(listeSomme, 2);
    listeSomme = ajout_liste(listeSomme, 1);
    listeSomme = sommeList(listeSomme);
    printf("La liste des sommes préfixe donne : \n");
    afficher(listeSomme);

    /* Libération de la mémoire */
    freeListe(listeOrd);
    freeListe(liste);
    freeListe(newList);
    freeListe(palin);
    freeListe(paPalin);
    freeListe(fibList);
    freeListe(testFusion);
    freeListe(listeSomme);

    return 0;
}
Exemplo n.º 21
0
int sizeListADT(t_ptr_liste liste) {
    if(est_vide(liste))
        return 0;
    return 1 + sizeList(queue_liste(liste));
}
Exemplo n.º 22
0
int sizeList(t_ptr_liste liste) {
    if(liste == NULL)
        return 0;
    return 1 + sizeList(liste->next);
}