infinite loop

This is a discussion on infinite loop within the C Programming forums, part of the General Programming Boards category; Hello I have written an infinite loop but i cannot understand wy? I want to add in the front of ...

  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
    20,960
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21