Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    infinite loop

    Hello

    I have written an infinite loop but i cannot understand wy?
    I want to add in the front of a linked list a new struct and after that i want to print it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int element;
    
    struct cell {
    	element e;
    	struct cell *p;
    };
    
    typedef struct cell* cel;
    
    cel addInFront (cel l, element i);
    void writeList (cel l);
    
    int main (void){
    
    	cel list;
    
    	list = NULL;
    	
    	list = addInFront (list, 5);
    	list = addInFront(list, 4);
    	writeList (list);
    	
    
    	return 0;
    }
    
    void writeList (cel l){
    
    	while (l != (cel )NULL){
    	printf("\t %d", l->e);
    	writeList (l->p);
    	}
    
    }
    
    
    
    cel addInFront ( cel l, element i){
    
    	cel p;
    
    	p = (cel) malloc (sizeof(struct cell));
    	p->e = i;
    	p->p = l;
    	return p;
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Check that loop in debugger.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Owk i have updated my code a little

    Now i have no longer a infinite loop but my loop doesnt stop at the right time.
    Wanted output : 1 2 3 4 5
    output : 1 2 3 4 5 5 4 5 5 4 5 5 4 5 5 2 3 4 5 4 5 5 3 4 5 5 4 5 5 stop

    I dont see the problem? Because my list is 1 2 3 4 5 NULL and it needs to stop at NULL

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int element;
    
    struct cell {
    	element e;
    	struct cell *p;
    };
    
    typedef struct cell* cel;
    
    cel addInFront (cel l, element i);
    void writeList (cel l);
    
    int main (void){
    
    	cel list;
    
    	list = (cel) NULL;
    	
    	list = addInFront (list, 5);
    	list = addInFront (list, 4);
    	list = addInFront (list, 3);
    	list = addInFront (list, 2);
    	list = addInFront (list, 1);
    
    	writeList (list);
    	
    
    	return 0;
    }
    
    void writeList (cel l){
    
    	while (l != (cel)NULL){
    		printf("\t %d", l->e);
    		l = l->p;
    		writeList (l);
    	}
    
    }
    
    
    
    cel addInFront ( cel l, element i){
    
    	cel p;
    
    	p = (cel) malloc (sizeof(struct cell));
    	p->e = i;
    	p->p = l;
    	return p;
    
    
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thescratchy
    I dont see the problem? Because my list is 1 2 3 4 5 NULL and it needs to stop at NULL
    The problem is that you are calling writeList recursively in the loop. Remove that recursive call and your problem should be solved.

    By the way, you might want to use names that are more descriptive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thanx that was indeed the problem

    Owk ill use other names next time. Thanx for the hint

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM