Exemplo n.º 1
0
/* return 0 successfully, otherwise failed              */
int changeElem(Node *pHead, int pos, elemType* pElem) {
   int place = 1;
   if (pHead == NULL) {
      printf("pHead is NULL, input valid list header !\n");
      return __LINE__;
   }

   if (pos < 1 || pos > sizeofList(pHead)) {
      printf("input position is invalid \n");
      return __LINE__;
   }

   
   if (pos == place) {
      memcpy(&(pHead->element), pElem, sizeof(elemType));
      printf("change %d element successfully !", pos);
      return 0;
   }

   while (place < pos) {
      place++;
      pHead = pHead->next;
   }

   memcpy(&(pHead->element), pElem, sizeof(elemType));
   printf("change %d element successfully !", pos);
   return 0;
  
}
void deleteQuestion(LISTA *l,int code){

	LISTA *tmp;
	int count = 0;
	int tam = sizeofList(first);

	while (l != NULL){
		if(l->code != code){
			tmp = l;
			l = l->next;
			count++;
		}

		else{
			if (count == 0){
				tmp = first;
				if (first->next != NULL){
					first = tmp->next;
					tmp->next = NULL;
					
					free(tmp);
					
					
					count++;
					}
			}

			else {
				if(l->next != NULL){
					tmp->next = l->next;
					l->next = NULL;
					free(l);
				}
				
				if (l->next == NULL){
					tmp->next = NULL;
					free(l);
				}

			}
			break;
		}
	}
	if (count == tam)
		printf("Error: El codigo ingresado no se encuentra en la base de datos.\n");
}
Exemplo n.º 3
0
/* return List header Node *                              */
Node * insertElem(Node *pHead, int pos, elemType* pElem) {
   Node *pTemp = NULL;
   Node *pPrev = NULL;

   Node *pTempH = pHead;

   int place = 1;

   if (pHead == NULL) {
      printf("pHead is NULL, input valid list header !\n");
      return NULL;
   }

   if (pos < 1 || pos > sizeofList(pHead)) {
      printf("input position is invalid \n");
      return NULL;
   }

   while (place < pos) {
      place++;
      pPrev = pHead;
      pHead = pHead->next;
   }

   pTemp = (Node *)malloc(sizeof(Node));
   if (pTemp == NULL) {
      printf("memory malloc() failed, insert failed... \n");
      return NULL;
   }

   memset(pTemp, 0, sizeof(Node));
   memcpy(&(pTemp->element), pElem, sizeof(elemType));

   (pPrev != NULL) ? (pPrev->next = pTemp) : (pTempH = pTemp);
   pTemp->next = pHead;

   printf("insert at %d successfully !\n", pos);
   return pTempH;

}