a double linked list loop

This is a discussion on a double linked list loop within the C Programming forums, part of the General Programming Boards category; please help me identifying the problem in the following program's do while loop. Code: #include<stdio.h> //implemening double linked list typedef ...

  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,799
    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, 03: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, 01:46 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM

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