C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 08:31 PM
Cfa77 Cfa77 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 9 Cfa77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 34 sec
Reputation 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");
}

Reply With Quote
  #2  
Old November 15th, 2012, 09:40 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 29th, 2012, 05:35 PM
Cfa77 Cfa77 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 9 Cfa77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 34 sec
Reputation Power: 0
Ok thank you!

Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [HELP] C Programming

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap