Linked List problem

This is a discussion on Linked List problem within the C Programming forums, part of the General Programming Boards category; cool fixed up the code, however theres something REALLY REALLY REALLY WEIRD, possibly my compiler, for some reason if i ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    cool fixed up the code, however theres something REALLY REALLY REALLY WEIRD, possibly my compiler, for some reason if i dont have the part with the enter number at the start i get faults happening when the file is read in and it gets to the balance

    Code:
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      long socialnum;
      double balance;
      struct customer *next;
    }CUSTOMER; 
    
    int main() 
    { 
      FILE *infile; 
      CUSTOMER *bankrecords;
      CUSTOMER *tempptr;
    
    double balance;
    //FOR SOME WEIRD ARSE REASON WITHOUT THIS IT CRASHES, ANY IDEAS ANYONE?
    printf("press a number else it doesnt work, no idea why");
    scanf("%lf",&balance);
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
    
    // READ IN ALL THE FILE INTO THE LIST
        while(!feof(infile))
        {
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
          }
          else
          {
            tempptr = bankrecords;
            while(tempptr->next!=NULL)
              tempptr=tempptr->next;
            tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr=tempptr->next;
          }
          fscanf(infile,"%s %s %c %D %lf",tempptr->fname,tempptr->lname,&(tempptr->initial),&(tempptr->socialnum),&(tempptr->balance));
          tempptr->next=NULL;
        }
        fclose(infile);
    
    
    // PRINT ALL THE LIST
        tempptr = bankrecords;
        printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n"); 
        printf("-----------------------------------------------------\n\n"); 
        while(tempptr!=NULL)
        {
          printf("%s %c %s %ld %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
          tempptr=tempptr->next;
        }
      }
      return 1;
    }
    oviously you'll need to format the output, ie to do the account number properly do %15d and it will right align (i think) everything giving 15 spaces.

    also if you need to free up the memory yourself rather than letting the os do it then I suggest making it a double linked list, heaps easier to free large amounts of memory and quicker.
    Hope this helps

  2. #17
    Unregistered
    Guest
    <quote>code
    #include <stdio.h>

    typedef struct customer
    {
    char fname[20];
    char lname[20];
    char initial;
    long socialnum;
    double balance;
    struct customer *next;
    }CUSTOMER;

    int main()
    {
    FILE *infile;
    CUSTOMER *bankrecords;
    CUSTOMER *tempptr;
    clrscr();

    /*double balance;*/
    /*FOR SOME WEIRD ARSE REASON WITHOUT THIS IT CRASHES, ANY IDEAS ANYONE?*/
    /*printf("press a number else it doesnt work, no idea why");
    scanf("%lf",&balance);*/

    bankrecords=NULL;

    infile=fopen("data.dat","r");
    if(infile == NULL)
    {
    printf("Data file could not be opened.\n");
    }
    else
    {

    /* READ IN ALL THE FILE INTO THE LIST*/
    while(!feof(infile))
    {
    if(bankrecords==NULL)
    {
    bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
    tempptr = bankrecords;
    }
    else
    {
    tempptr = bankrecords;
    while(tempptr->next!=NULL)
    tempptr=tempptr->next;
    tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
    tempptr=tempptr->next;
    }
    fscanf(infile,"%s %s %c %D %lf",tempptr->fname,tempptr->lname,&(tempptr->initial),&(tempptr->socialnum),&(tempptr->balance));
    tempptr->next=NULL;
    }
    fclose(infile);


    /* PRINT ALL THE LIST */
    tempptr = bankrecords;
    printf("LAST\tMI\tFIRST\t SSN \t\tBALANCE \t\n");
    printf("-----------------------------------------------------\n\n");
    while(tempptr!=NULL)
    {
    printf("%s\t %c\t %s\t %ld\t %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
    tempptr=tempptr->next;
    }
    }
    getch();
    return 1;
    }

    </quote>

    the code is working without reading that double value, Bull. I think you better use a good ol C compiler, rather than a state-of-the art new win32 CPP compiler. You can find one such nice compiler at http://community.borland.com.

    Borland is giving away its TurboC 2.1 compiler for free. All u need to do is register there and download it from their Museum.

    Note: i made some changes in printf's which will be working fine on my system.

    --cheers.

  3. #18
    Unregistered
    Guest
    I need the data to print right because the data file im reading from is not formatted so it might have something like:

    Jones Mike K 857102563 214.36
    Matty Joe L 142036987 1487.24

    So when i output the data it does not line up correctly.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    typedef struct customer 
    { 
    	char fname[20]; 
    	char lname[20]; 
    	char initial;
    	int socialnum;
    	double balance;
    	struct customer *next;
    }CUSTOMER;
    
    int main() 
    { 
    	FILE *infile; 
    	CUSTOMER *bankrecords;
    	CUSTOMER *tempptr;
    	int track=1;
    
    	
    
    	
    
    
    	bankrecords=NULL;
    
    	infile=fopen("data.dat","r"); 
    	if(infile == NULL)
    	{ 
    		printf("Data file could not be opened.\n"); 
    	}
    	else
    	{
    
    	
    
    	 
        while(!feof(infile))
        {
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
          }
          else
          {
            tempptr = bankrecords;
            while(tempptr->next!=NULL)
              tempptr=tempptr->next;
            tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr=tempptr->next;
          }
          fscanf(infile,"%s %s %c %d %lf",&tempptr->lname,&tempptr->fname,&tempptr->initial,&tempptr->socialnum,&tempptr->balance);
          tempptr->next=NULL;
        }
        fclose(infile);
    
    
    // PRINT ALL THE LIST
        tempptr = bankrecords;
        printf("NUM\tADDRESS    LAST\tFIRST  MI   SSN        BALANCE    NEXT\n"); 
        printf("-----------------------------------------------------------\n\n"); 
        while(tempptr!=NULL)
        {
          printf("%d   %8X\t  %s \t%s   %c   %d   %.2lf  %8X\n",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance,tempptr->next);
    	  track++;
          tempptr=tempptr->next;
        }
      }
      return 0;
    }

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it doesn't line up right when you output it, you need to actually learn how to use printf:

    printf("%8d", 10 ); <--- will make this number take 8 spaces on the screen (unless it's longer than that)

    There are similar notations for all forms:

    %Ns
    %Nc
    %Nd
    %Nf

    Replace N with a number.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Unregistered
    Guest
    ok as of right now if my input file has the following:

    Johnson Mike J 253657410 19473.00
    Mack Johnny T 230459630 21045.00
    Jackson Lewis J 069741052 25.69

    And i run my program my output does this:

    Code:
    
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    ----------------------------------------------------------------
    
    1      430090   Johnson         Mike      J   253657410   19473.00    431E60
    2      431E60   Mack            Johnny    T   230459630   21045.00    431DF0
    3      431DF0   Jackson         Lewis     J    69741052   25.69       0
    Press any key to continue
    
    how can i get it to print like this:
    
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    ----------------------------------------------------------------
    
    1      430090   Johnson         Mike      J   253657410   19473.00    NULL
    
    
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    ----------------------------------------------------------------
    1      430090   Johnson         Mike      J   253657410   19473.00    431E60
    2      431E60   Mack            Johnny    T   230459630   21045.00    NULL
    
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    ----------------------------------------------------------------
    
    1     431DF0   Jackson         Lewis     J    69741052   25.69         431E62
    
    2     430090   Johnson         Mike      J   253657410   19473.00    431E60
    
    3      431E60   Mack            Johnny    T   230459630   21045.00    NULL
    It has to sort the names by alphabetical order by last names and if last names are the same go by first names.

  6. #21
    Unregistered
    Guest
    oh yeah here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    typedef struct customer 
    { 
    	char fname[20]; 
    	char lname[20]; 
    	char initial;
    	int  socialnum;
    	double balance;
    	struct customer *next;
    }CUSTOMER;
    
    int main() 
    { 
    	FILE *infile; 
    	CUSTOMER *bankrecords;
    	CUSTOMER *tempptr;
    	int track=1;
    
    	
    
    	
    
    
    	bankrecords=NULL;
    
    	infile=fopen("data.dat","r"); 
    	if(infile == NULL)
    	{ 
    		printf("Data file could not be opened.\n"); 
    	}
    
        while(!feof(infile))
        {
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
          }
          else
          {
            tempptr = bankrecords;
            while(tempptr->next!=NULL)
              tempptr=tempptr->next;
            tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr=tempptr->next;
          }
          fscanf(infile,"%s %s %c %d %lf",&tempptr->lname,&tempptr->fname,&tempptr->initial,&tempptr->socialnum,&tempptr->balance);
          tempptr->next=NULL;
        }
        fclose(infile);
    
    
    // PRINT ALL THE LIST
        tempptr = bankrecords;
        printf("NUM\tADDRESS    LAST\t     \tFIRST   MI    SSN        BALANCE    NEXT\n"); 
        printf("----------------------------------------------------------------\n\n"); 
        while(tempptr!=NULL)
        {
          printf("%d    %8X\t%-12s \t%-9s %1c   %9d   %.2lf  %8X\n",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance,tempptr->next);
    	  track++;
          tempptr=tempptr->next;
        }
      
    	return 0;
    }

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while(tempptr!=NULL)
        {
          printf("%d   %8X\t  %s \t%s   %c   %d   %.2lf  %8X\n",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance,tempptr->next);
    	  track++;
          tempptr=tempptr->next;
        }
    Modify it to something like:

    Code:
    while(tempptr!=NULL)
    {
        printf("%d   %8X\t  %s \t%s   %c   %d   %.2lf  ",
            track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance );
        if( tempptr->next == NULL )
            printf( "    NULL\n" );
        else
            printf("%8d\n", tempptr->next );
    
        track++;
        tempptr=tempptr->next;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Unregistered
    Guest
    ok but what about getting it to print out the way i showed in my last 2 posts?

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's a very simple looping process:
    Code:
    while( ptr1 != NULL )
    {
        print header text
        while( ptr2 != ptr1 )
        {
            print ptr2 infor
            ptr2 = ptr2->next;
        }
        print ptr1 information
        ptr1 = ptr1->next;
    }
    Should do the trick.

    Quzah.
    Last edited by quzah; 04-01-2002 at 05:15 PM.
    Hope is the first step on the road to disappointment.

  10. #25
    Unregistered
    Guest
    im not understanding what you typed. I mean i sort of understand but not completely

  11. #26
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    ok the easiest way to do that is probably to sort it as its being inserted into the list and then just print them out as normal, below is the code i posted which your using now but modified which sorts the list on insertion:

    Code:
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      long socialnum;
      double balance;
      struct customer *next;
    }CUSTOMER; 
    
    int main() 
    { 
      FILE *infile; 
      CUSTOMER *bankrecords;
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
      CUSTOMER temprecord;
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
    
    // READ IN ALL THE FILE INTO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
            tempptr->next=NULL;
          }
          else				// RECORDS ALREADY EXIST
          {
            tempptr = bankrecords;
            while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
              tempptr=tempptr->next;
    
            if(bankrecords==tempptr)	// RECORD BELONGS AT START OF LIST
            {
              tempptr2=tempptr;
              bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr = bankrecords;
            }
            else				// RECORD IS SOMEWHERE IN MIDDLE OR END
            {
              tempptr2=tempptr->next;
              tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr=tempptr->next;
            }
            tempptr->next=tempptr2;
          }
    
          //COPY ALL DATA FROM TEMP RECORD TO NEW ONE
          strcpy(tempptr->fname,temprecord.fname);
          strcpy(tempptr->lname,temprecord.lname);
          tempptr->initial = temprecord.initial;
          tempptr->socialnum = temprecord.socialnum;
          tempptr->balance = temprecord.balance;
        }
        fclose(infile);
    
    
    // PRINT ALL THE LIST
        tempptr = bankrecords;
        printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n"); 
        printf("-----------------------------------------------------\n\n"); 
    
        while(tempptr!=NULL)
        {
          printf("%s %c %s %ld %.2lf\n",tempptr->fname,tempptr->initial,tempptr->lname,tempptr->socialnum,tempptr->balance);
          tempptr=tempptr->next;
        }
      }
      return 1;
    }
    you can either cut bits out or create 2 new variables as:
    Code:
      CUSTOMER *tempptr2;
      CUSTOMER temprecord;
    and then change the code within the while(!feof(infile)) to
    Code:
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
            tempptr->next=NULL;
          }
          else				// RECORDS ALREADY EXIST
          {
            tempptr = bankrecords;
            while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
              tempptr=tempptr->next;
    
            if(bankrecords==tempptr)	// RECORD BELONGS AT START OF LIST
            {
              tempptr2=tempptr;
              bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr = bankrecords;
            }
            else				// RECORD IS SOMEWHERE IN MIDDLE OR END
            {
              tempptr2=tempptr->next;
              tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr=tempptr->next;
            }
            tempptr->next=tempptr2;
          }
    
          //COPY ALL DATA FROM TEMP RECORD TO NEW ONE
          strcpy(tempptr->fname,temprecord.fname);
          strcpy(tempptr->lname,temprecord.lname);
          tempptr->initial = temprecord.initial;
          tempptr->socialnum = temprecord.socialnum;
          tempptr->balance = temprecord.balance;
        }
    hope this helps

  12. #27
    Unregistered
    Guest
    ok I am continuing to have issues with printing out each record one at a time. When ever i modify my code i can never get it go print out one at a time instead it prints out all of them.

    The data i am using for input looks like this:

    Johnson Mike J 253657410 19473.00
    Mack Johnny T 230459630 21045.00
    Jackson Lewis J 169741052 2579.69
    Zunny Jack L 065523069 25144.36
    Ashberry Jim P 741803205 26390.54

    i want it to first print out the first persons info like this:

    Code:
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------------
    
    1      431DB0   Ashberry        Jim       P   741803205   26390.54    NULL
    Then print out:

    Code:
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------------
    
    1      431DB0   Ashberry        Jim       P   741803205   26390.54    431E90
    
    2      431E90   Jackson         Lewis     J   169741052   2579.69    NULL
    Then print:

    Code:
    NUM     ADDRESS    LAST         FIRST   MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------------
    
    1      431DB0   Ashberry        Jim       P   741803205   26390.54    431E90
    
    2      431E90   Jackson         Lewis     J   169741052   2579.69    431F00
    
    3      431F00   Mack            Johnny    T   230459630   21045.00    NULL

    And so on.....

    what am i doing wrong?

  13. #28
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    you basically need a counter and loop through until you are at the end, so through the first pass it will print 1, then 2, and so on, also I screwed a little of the code b4, below is all the code, works perfect on my pc, and sorts them properly now:

    Code:
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      long socialnum;
      double balance;
      struct customer *next;
    }CUSTOMER; 
    
    int main() 
    { 
      FILE *infile; 
      CUSTOMER *bankrecords;
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
      CUSTOMER temprecord;
      int counter1,counter2;
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
    
    // READ IN ALL THE FILE INTO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
            tempptr->next=NULL;
          }
          else				// RECORDS ALREADY EXIST
          {
            tempptr = bankrecords;
            while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
              tempptr=tempptr->next;
    
            if((bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord.fname)>0))	// RECORD BELONGS AT START OF LIST
            {
              tempptr2=tempptr;
              bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr = bankrecords;
            }
            else				// RECORD IS SOMEWHERE IN MIDDLE OR END
            {
              tempptr2=tempptr->next;
              tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr=tempptr->next;
            }
            tempptr->next=tempptr2;
          }
    
          //COPY ALL DATA FROM TEMP RECORD TO NEW ONE
          strcpy(tempptr->fname,temprecord.fname);
          strcpy(tempptr->lname,temprecord.lname);
          tempptr->initial = temprecord.initial;
          tempptr->socialnum = temprecord.socialnum;
          tempptr->balance = temprecord.balance;
        }
        fclose(infile);
    
    
    // PRINT ALL THE LIST
        tempptr = bankrecords;
    
        for(counter1=0;tempptr!=NULL;counter1++)
        {
          tempptr = bankrecords;
          printf("NUM\tADDRESS\tLAST\tFIRST\tMI    SSN        BALANCE    NEXT\n"); 
          printf("----------------------------------------------------------------\n\n"); 
          for(counter2=0;counter2<=counter1;counter2++)
          {
            printf("%d%5X\t%-12s\t%-9s\t%c   %9ld   %.2lf  ",counter2+1,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
            if(tempptr->next==NULL)
              printf("NULL\n");
            else
              printf("%5X\n",tempptr->next);
            tempptr=tempptr->next;
          }
          printf("\n\n");
        }
      }
      return 1;
    }
    hope this solves ya problem.

  14. #29
    Unregistered
    Guest
    Thank you that helped alot but when i try to move the print part of my code inside my while loop where i read in the data it gives me errors. I am trying to get it where after it reads one line of the file which is one customer to print out the contents of the linked list so far then it should go back and read in the next line then print the contents of the linked list which would be 2 customers and so on.

    here my error ridden code:

    Code:
    #include <stdio.h> 
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      long socialnum;
      double balance;
      struct customer *next;
    }CUSTOMER,LIST; 
    
    int main() 
    { 
      FILE *infile,*infile2; 
      CUSTOMER *bankrecords;
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
      CUSTOMER temprecord;
      LIST *bankrecords2;
      LIST *tempptr3;
      LIST *tempptr4;
      LIST temprecord2;
      int counter1,counter2,track=1;;
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
    
    // READ IN ALL THE FILE INTO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %D %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
    	
    	  for(counter1=0;tempptr!=NULL;counter1++) {
    	
    		tempptr = bankrecords;
    		printf("NUM\tADDRESS LAST\t     \tFIRST   MI    SSN        BALANCE    NEXT\n"); 
    		printf("-------------------------------------------------------------------------------\n\n"); 
    		track=1;
    		for(counter2=0;counter2<=counter1;counter2++,track++) {
          
    			printf("%d    %8X\t%-12s \t%-9s %1c   %9d   %-.2lf  ",track,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
    			if(tempptr->next==NULL) 
    				printf("NULL\n");
    			else
    				printf("%6X\n",tempptr->next);
    			tempptr=tempptr->next;
    		}
    		printf("\n\n");
    		}
    	}
          if(bankrecords==NULL)
          {
            bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr = bankrecords;
            tempptr->next=NULL;
          }
          else				// RECORDS ALREADY EXIST
          {
            tempptr = bankrecords;
            while((tempptr->next!=NULL) && (strcmp(tempptr->fname,temprecord.fname)<0))
              tempptr=tempptr->next;
    
            if((bankrecords==tempptr) && (strcmp(tempptr->fname,temprecord.fname)>0))	// RECORD BELONGS AT START OF LIST
            {
              tempptr2=tempptr;
              bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr = bankrecords;
            }
            else				// RECORD IS SOMEWHERE IN MIDDLE OR END
            {
              tempptr2=tempptr->next;
              tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
              tempptr=tempptr->next;
            }
            tempptr->next=tempptr2;
          }
    
     // i tried to move this inside the while loop but it did no good    
          //COPY ALL DATA FROM TEMP RECORD TO NEW ONE
          strcpy(tempptr->fname,temprecord.fname);
          strcpy(tempptr->lname,temprecord.lname);
          tempptr->initial = temprecord.initial;
          tempptr->socialnum = temprecord.socialnum;
          tempptr->balance = temprecord.balance; 
        }
        fclose(infile);
    
    
        }
      
    
    	return 0;
    }

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is easy. Break this into seperate functions. You are cluttering this main function up horribly. The easiest way to do this:

    Create a function that reads 1 entry and puts it in the list:
    int readRecord( FILE *fp, struct MyList *list )
    {
    ...if file
    ......newNode = allocate a new node
    ......read into newnode
    ......append new node to list
    ......return success
    ...return failure
    }

    Create a function that displays the list:
    void displayList( struct MyList *list )
    {
    ...print header...
    ...while !end of list
    ......display this node
    ......node = next node
    }

    Use this main function:
    int main ( void )
    {
    ...open file
    ...while readRecord( file ) != failure
    ......displayList
    ...close file
    ...return success
    }

    See how easy it is when you break it down into logical steps?

    Quzah.
    Hope is the first step on the road to disappointment.

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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