#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    [HELP] C Programming


    Hi!

    I was wondering why this program is not working. When it gets to function uniao() it enters in an infinite loop. Can somebody please help me????

    Here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    #define NUM 4

    typedef struct NODO{

    int id;
    struct NODO *nseg;

    }Nodo;

    Nodo *makeNode(int n);
    Nodo *push(Nodo *L, Nodo *nv);
    Nodo *makeList();
    Nodo *insertLast(Nodo *L, Nodo *nv);
    Nodo *pop(Nodo **L);
    Nodo *uniao(Nodo **s1, Nodo **s2);
    int totalElementos(Nodo *L);
    void printList(Nodo *L);


    int main(int argc, char *argv){

    Nodo *Pilha1 = NULL;
    Nodo *Pilha2 = NULL;
    Nodo *Pilha3 = NULL;
    Nodo *nN = NULL;
    Pilha1 = makeList();
    Pilha2 = makeList();
    Pilha3 = uniao(&Pilha1, &Pilha2);
    printList(Pilha3);

    }

    Nodo *makeNode(int n){

    Nodo *L;
    L = (Nodo *)malloc(sizeof(Nodo));
    L->id = n;
    L->nseg = NULL;
    return L;
    }

    Nodo *push(Nodo *L, Nodo *nv){
    if(L == NULL)
    return nv;
    nv->nseg = L;
    return nv;
    }

    Nodo *makeList(){

    int i,n;

    Nodo *L = NULL;
    Nodo *aux;


    for(i=0;i<NUM;i++){
    printf("Introduza o id: \n");
    scanf("%d",&n);
    aux = makeNode(n);
    L = push(L,aux);
    }
    return L;

    }

    Nodo *insertLast(Nodo *L, Nodo *nv){

    Nodo *aux = L;

    if(L == NULL)
    return nv;
    while(L->nseg!=NULL)
    L = L->nseg;
    L->nseg = nv;
    nv->nseg = NULL;
    return aux;
    }

    Nodo *pop(Nodo **L){

    Nodo *aux = *L;

    if(*L == NULL)
    return *L;
    *L = (*L)->nseg;
    aux->nseg = NULL;

    return (*L);
    }

    Nodo *removeFirst(Nodo *L){

    Nodo *aux = L;

    if(L == NULL)
    return NULL;
    L = L->nseg;
    free(aux);
    return L;
    }

    Nodo *uniao(Nodo **s1, Nodo **s2){

    Nodo *aux;
    Nodo *s3 = NULL;

    while(*s1 != NULL || *s2 != NULL){

    printf("Estou no ciclo\n");

    *s1 = pop(s1);
    s3 = push(s3, *s1);
    *s2 = pop(s2);
    s3 = push(s3, *s2);


    }
    return (s3);
    }

    void printList(Nodo *L){

    if(L == NULL)
    printf("NULL");

    while(L!=NULL){
    printf("%d ----> ", L->id);
    L = L->nseg;
    }
    printf("NULL.\n");
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    Find the significant changes to pop and to uniao.
    Code:
    Nodo*pop(Nodo**L) {
      Nodo *aux = *L;
      if(*L == NULL)
        return NULL;
      *L = (*L)->nseg;
      aux->nseg = NULL;
      return aux;
    }
    Nodo*uniao(Nodo**s1,Nodo**s2) {
      Nodo*s3 = NULL,*aux;
      while(*s1 != NULL || *s2 != NULL) {
        aux = pop(s1);
        s3 = push(s3,aux);
        aux = pop(s2);
        s3 = push(s3,aux);
      }
      return s3;
    }
    int main(int argc,char*argv[]) {
      Nodo*Pilha1 = NULL,*Pilha2 = NULL,*Pilha3 = NULL;
      Pilha1 = makeList();
      printList(Pilha1);
      Pilha2 = makeList();
      printList(Pilha2);
      Pilha3 = uniao(&Pilha1, &Pilha2); 
      printList(Pilha3);
      return 0;			                /* inserted */
    }
    main returns an integer regarding its status. Fulfill your promise to the compiler, and honor your operating system.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Ok thank you!

    Originally Posted by b49P23TIvg
    Find the significant changes to pop and to uniao.
    Code:
    Nodo*pop(Nodo**L) {
      Nodo *aux = *L;
      if(*L == NULL)
        return NULL;
      *L = (*L)->nseg;
      aux->nseg = NULL;
      return aux;
    }
    Nodo*uniao(Nodo**s1,Nodo**s2) {
      Nodo*s3 = NULL,*aux;
      while(*s1 != NULL || *s2 != NULL) {
        aux = pop(s1);
        s3 = push(s3,aux);
        aux = pop(s2);
        s3 = push(s3,aux);
      }
      return s3;
    }
    int main(int argc,char*argv[]) {
      Nodo*Pilha1 = NULL,*Pilha2 = NULL,*Pilha3 = NULL;
      Pilha1 = makeList();
      printList(Pilha1);
      Pilha2 = makeList();
      printList(Pilha2);
      Pilha3 = uniao(&Pilha1, &Pilha2); 
      printList(Pilha3);
      return 0;			                /* inserted */
    }
    main returns an integer regarding its status. Fulfill your promise to the compiler, and honor your operating system.

IMN logo majestic logo threadwatch logo seochat tools logo