Thread: a double linked list loop

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    72

    Angry a double linked list loop

    please help me identifying the problem in the following program's do while loop.
    Code:
    #include<stdio.h> //implemening double linked list
    typedef struct _auistud{
    	char name[20];
    	int gpa,id;
    	struct _auistud *next;
    	struct _auistud *previous;
    }auistud;
    int 
    main(void){
    	auistud *_new,*rear,*head;
    	char ans;
    	_new=(auistud*)malloc(sizeof(auistud));
    	printf("please enter the name\n");
    	scanf("%s",_new->name);
    	printf("please enter the id\n");
    	scanf("%d",&_new->id);
    	printf("please enter the gpa\n");
    	scanf("%d",&_new->gpa);
    	head=_new;
    	rear=head;
    	do{
    		_new=(auistud*)malloc(sizeof(auistud));
    			printf("please enter the name\n");
    		scanf("%s",_new->name);
    		printf("please enter the id\n");
    		scanf("%d",&_new->id);
    		printf("please enter the gpa\n");
    		scanf("%d",&_new->gpa);
    		rear->next=_new;
    		_new->previous=rear;	
    		printf("please enter yes or no\n");
    		scanf("%c",&ans);
    	}while(ans=='y');
    	rear->next=NULL;
    return(0);
    }
    proud to be from aui www.aui.ma and also a great elton john's fan

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    do{
        _new=(auistud*)malloc(sizeof(auistud));
        printf("please enter the name\n");
        scanf("&#37;s",_new->name);
        printf("please enter the id\n");
        scanf("%d",&_new->id);
        printf("please enter the gpa\n");
        scanf("%d",&_new->gpa);
        rear->next=_new;
        _new->previous=rear;	
        printf("please enter yes or no\n");
        scanf("%c",&ans);
    }while(ans=='y');
    One problem, you aren't reseting rear anywhere in the loop, it remains pointing to the first node (the head) created prior to the loop. _new->previous thus always points to head.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM