The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
[HELP] C Programming
Discuss [HELP] C Programming in the C Programming forum on Dev Shed. [HELP] C Programming C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 15th, 2012, 08:31 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 9
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");
}
|

November 15th, 2012, 09:40 PM
|
 |
Contributing User
|
|
|
|
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!
|

November 29th, 2012, 05:35 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 9
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. |
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|