Linked List problem

This is a discussion on Linked List problem within the C Programming forums, part of the General Programming Boards category; the easiest way if you dont want to create function which may be an idea once its working is to ...

  1. #31
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    the easiest way if you dont want to create function which may be an idea once its working is to grab the code I posted before and change it so at the bottom of the for statement before:
    Code:
    }
    fclose(infile);
    put:

    Code:
    // PRINT ALL THE LIST
    
          tempptr = bankrecords;
          printf("NUM\tADDRESS\tLAST\tFIRST\tMI    SSN        BALANCE    NEXT\n"); 
          printf("----------------------------------------------------------------\n\n"); 
          while(tempptr!=NULL)
          {
            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");
        }
        fclose(infile);
    and then delete the printouts later in the code and should all work how ya want it too.

  2. #32
    Unregistered
    Guest
    Ok you have lost me i understand putting it into functions but im confused as to what declarations should go in which function.

  3. #33
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How hard is this to understand?

    1) Make a function to read from a file pointer, which puts that informtion on the end of the list.

    You already know how to do this, so put it in a function so that this is ALL that the function does.

    a) read the line
    b) if EOF, return failure
    c) else create a node and put the info in it

    2) Make a function to print the list.
    You already know how to do this also.
    Pass it the list, cycle through it and display it all.

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

  4. #34
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    Since this post has been goin on for ages and you have actually been trying different things etc. I've fixed it up, but I wouldnt normally, here it is in function, make sure you read it and understand it before you hand it in for your subject.
    P.S. you'll have to change the print statements to whatever ones your using.

    Code:
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      long socialnum;
      double balance;
      struct customer *next;
    }CUSTOMER; 
    
    void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
    
      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;
    }
    
    void printlist(CUSTOMER **bankrecords)
    {
      CUSTOMER *tempptr;
      int counter=1;
    
      tempptr = *bankrecords;
      printf("NUM\tADDRESS\tLAST\tFIRST\tMI    SSN        BALANCE    NEXT\n"); 
      printf("----------------------------------------------------------------\n\n"); 
      while(tempptr!=NULL)
      {
        printf("%d%5X\t%-12s\t%-9s\t%c   %9ld   %.2lf  ",counter,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;
        counter++;
      }
      printf("\n\n");
    }
    
    int main() 
    { 
      FILE *infile; 
      CUSTOMER *bankrecords;
      CUSTOMER temprecord;
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
        // READ IN FILE TO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
          addtolist(&bankrecords,&temprecord);
          printlist(&bankrecords);
        }
        fclose(infile);
      }
      return 1;
    }

  5. #35
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    oops that last line
    Code:
    return 1;
    should be
    Code:
    return 0;

  6. #36
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    RE: "Oops"

    You do know, Bull, that you can click "edit" and go back and modify your posts, right? Just thought I'd let you know. I see tons of people that post "oops" follow-up posts when they could have just edited their original.

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

  7. #37
    Unregistered
    Guest
    ok i have read the data in from one file and there is a second file that has some more info and i am going to read one piece in at a time and then do some things with it according to exactly what it is. So my question is this: How am i reading in the second file? am i starting another linked list or is it going in the same list? Also say i want to compare the SSN from teh first list with one from the second list to see if the customer is already on that list how could i do that? My problem is in the main function.

    Code:
    
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      char socialnum[10];
      double balance;
      double amount;
      struct customer *next;
    }CUSTOMER; 
    
    void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
    
      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;
      strcpy(tempptr->socialnum,temprecord->socialnum);
      tempptr->balance = temprecord->balance;
    }
    
    void addtolist2(CUSTOMER **transactions,CUSTOMER *temp)
    {
      CUSTOMER *tempptr3;
      CUSTOMER *tempptr4;
    
      
    
      if(*transactions==NULL)
      {
        *transactions = (CUSTOMER *) malloc(sizeof(CUSTOMER));
        tempptr3 = *transactions;
        tempptr3->next=NULL;
      }
      else				
      {
        tempptr3 = *transactions;
        while((tempptr3->next!=NULL) && (strcmp(tempptr3->fname,temp->fname)<0))
          tempptr3=tempptr3->next;
    
        if((*transactions==tempptr3) && (strcmp(tempptr3->fname,temp->fname)>0))	
        {
          tempptr4=tempptr3;
          *transactions = (CUSTOMER *) malloc(sizeof(CUSTOMER));
          tempptr3 = *transactions;
        }
        else				
        {
          tempptr4=tempptr3->next;
          tempptr3->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
          tempptr3=tempptr3->next;
        }
        tempptr3->next=tempptr4;
      }
    
      
      strcpy(tempptr3->fname,temp->fname);
      strcpy(tempptr3->lname,temp->lname);
      tempptr3->initial = temp->initial;
      strcpy(tempptr3->socialnum,temp->socialnum);
      tempptr3->balance = temp->balance;
    }
    
    
    void printlist(CUSTOMER **bankrecords)
    {
    	CUSTOMER *tempptr;
    	int counter=1;
    	
    
      tempptr = *bankrecords;
      printf("NUM  ADDRESS  LAST       FIRST   MI    SSN        BALANCE    NEXT\n"); 
      printf("----------------------------------------------------------------\n\n"); 
      while(tempptr!=NULL)
      {
        printf("%d    %5X  %-12s%-9s%c   %s    %.2lf  ",counter,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;
        counter++;
      }
      printf("\n\n");
      
    }
    
    int main() 
    { 
      FILE *infile,*infile2; 
      CUSTOMER *bankrecords;
      CUSTOMER temprecord;
      CUSTOMER *transactions;
      CUSTOMER temp;
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
        // READ IN FILE TO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
          addtolist(&bankrecords,&temprecord);
          printlist(&bankrecords);
        }
        
      }
    
    	infile2=fopen("data_2.dat","r");
    
    	/////This is the part in Question!!!!///
    	if(infile2 == NULL)
    	{ 
    		printf("Data file could not be opened.\n"); 
    	}
    	else
    	{
    		while(!feof(infile2))
    		{
    			fscanf(infile,"%s %s %c %s %lf",temp.lname,temp.fname,&(temp.initial),temp.socialnum,&(temp.amount));
    			
    			
    		}
    		
    	}
    	
      return 0;
    }

  8. #38
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    ok the following code wont allow 2 occurences of the same SSN in the same list:
    Code:
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      long socialnum;
      double balance;
      struct customer *next;
    }CUSTOMER; 
    
    // RETURN 1 IF INSERTION IS SUCCESSFUL
    int addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
    
      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;
          tempptr->next=tempptr2;
        }
        else				// RECORD IS SOMEWHERE IN MIDDLE OR END
        {
          if(tempptr->socialnum != temprecord->socialnum)
          {
            tempptr2=tempptr->next;
            tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr=tempptr->next;
            tempptr->next=tempptr2;
          }
          else return 0;
        }
      }
    
      //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;
      return 1;
    }
    
    void printlist(CUSTOMER **bankrecords)
    {
      CUSTOMER *tempptr;
      int counter=1;
    
      tempptr = *bankrecords;
      printf("NUM\tADDRESS\tLAST\tFIRST\tMI    SSN        BALANCE    NEXT\n"); 
      printf("----------------------------------------------------------------\n\n"); 
      while(tempptr!=NULL)
      {
        printf("%d%5X\t%-12s\t%-9s\t%c   %9ld   %.2lf  ",counter,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;
        counter++;
      }
      printf("\n\n");
    }
    
    int main() 
    { 
      FILE *infile; 
      CUSTOMER *bankrecords;
      CUSTOMER temprecord;
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
        // READ IN FILE TO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %D %lf",temprecord.fname,temprecord.lname,&(temprecord.initial),&(temprecord.socialnum),&(temprecord.balance));
          if(addtolist(&bankrecords,&temprecord))
            printlist(&bankrecords);
        }
        fclose(infile);
      }
      return 0;
    }
    as long as the second file is being read in with the same information then theres no reason you cant use the same list. just read each line in the same (saving it into temprecord) and then do whatever checks and stuff ya need to and save it if you want to, alternatevly you could create a new list just as easy by doing this:

    Code:
      CUSTOMER *secondlist=NULL;
    and then using the functions exactly the same way.

    Hope this helps

  9. #39
    Unregistered
    Guest
    Ok once i read in the data from the second file the first thing im trying to do is compare the social num i just read in to the ones already in the list to see if there is a match, how can i do that?

    Code:
    
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h> 
    
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial;
      char socialnum[10];
      double balance;
      double amount;
      struct customer *next;
    }CUSTOMER; 
    
    void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
    
      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->lname,temprecord->lname)<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;
      strcpy(tempptr->socialnum,temprecord->socialnum);
      tempptr->balance = temprecord->balance;
    }
    
      
    
    
    
    void printlist(CUSTOMER **bankrecords)
    {
    	CUSTOMER *tempptr;
    	int counter=1;
    	
    
      tempptr = *bankrecords;
      printf("NUM  ADDRESS  LAST       FIRST   MI    SSN        BALANCE    NEXT\n"); 
      printf("----------------------------------------------------------------\n\n"); 
      while(tempptr!=NULL)
      {
        printf("%d    %5X  %-12s%-9s%c   %s    %.2lf  ",counter,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;
        counter++;
      }
      printf("\n\n");
      
    }
    
    int main() 
    { 
      FILE *infile,*infile2; 
      CUSTOMER *bankrecords;
      CUSTOMER temprecord;
     
    
    
      bankrecords=NULL;
    
      infile=fopen("data.dat","r"); 
      if(infile == NULL)
      { 
        printf("Data file could not be opened.\n"); 
      }
      else
      {
        // READ IN FILE TO THE LIST
        while(!feof(infile))
        {
          fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
    	  if(strcmp(temprecord.socialnum
          addtolist(&bankrecords,&temprecord);
          printlist(&bankrecords);
        }
        
      }
    	
      
    	infile=fopen("data_2.dat","r");
    	if(infile2 == NULL)
    	{ 
    		printf("Data file could not be opened.\n"); 
    	}
    	else
    	{
    		 while(!feof(infile))
    		{
    			fscanf(infile,"%s %s %c %s %lf",temprecord.lname,temprecord.fname,&(temprecord.initial),temprecord.socialnum,&(temprecord.amount));
    			addtolist(&bankrecords,&temprecord);
    			printlist(&bankrecords);
    			
    			
    		} 
    		
    	} 
    	
      return 0;
    }

  10. #40
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    something like this should work:

    Code:
    // Returns true (1) if the record exists
    int findinlist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
      CUSTOMER *tempptr;
    
      tempptr = *bankrecords;
      if(tempptr==NULL)
        return 0;
      else
      {
        while((tempptr!=NULL) && (tempptr->socialnum!=temprecord->socialnum))
          tempptr=tempptr->next;
        if(tempptr==NULL)
          return 0;
        else
          return 1;
      }
    }
    should work, havent tested it, I see your using a string for socialnum, so in that case you whould have the while statement reading something like:
    Code:
        while((tempptr!=NULL) && (strcomp(tempptr->socialnum,temprecord->socialnum)<>0))
    but if you are only going to be inserting it into the list if its not there then you can just use the add function I fixed as it returns 0
    if the record is already in the list.

  11. #41
    Unregistered
    Guest

    Sort

    ok my problem is that when i expanded my list of names from the first file. As it reads each person and their stats to teh linked list it no longer sorts them by their last name?!! I have no idea why it did that.

    Here is the exact data file im using

    Springston Ray L 374926490 40392.37
    Heineman Doug A 264592791 19473.08
    Alagappan Solayappan Q 567493784 20493.76
    Easton Dwight R 193465943 30284.33
    Chatila Sami K 029375938 1943.88
    Johnson Devin E 736549283 197493.93
    Fazal Talha S 643847651 2947.08
    Punjal Jagan D 463248598 29473.71
    Pruitt Terry O 846239402 19374.50
    Ma Glen T 264957640 134.29
    Lewis Troy R 002947594 19374.12
    Khan Eddy U 947604592 9476.66
    Tran Michael B 204946731 652.09
    Hopkins Weston S 463947204 2940.45

    Code:
    void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
    	CUSTOMER *tempptr;
    	CUSTOMER *tempptr2;
    
    
    
    	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->lname,temprecord->lname)<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;
    	strcpy(tempptr->socialnum,temprecord->socialnum);
    	tempptr->balance = temprecord->balance;
    }
    what could be wrong?

  12. #42
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    ok the problem is that it originally sorted them by their firstname, and in the addtolist function theres still some references to fname, all you have to do is search through the addtolist function and change EVERYTHING that says fname to lname. and that should fix your problem.

  13. #43
    Unregistered
    Guest
    ok i went back and changed them all to last name as you can see below:

    Code:
    void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
    	CUSTOMER *tempptr;
    	CUSTOMER *tempptr2;
    
    
    
    	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->lname,temprecord->lname)<0))
    			tempptr=tempptr->next;
    
        if((*bankrecords==tempptr) && (strcmp(tempptr->lname,temprecord->lname)>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;
    	strcpy(tempptr->socialnum,temprecord->socialnum);
    	tempptr->balance = temprecord->balance;
    }
    ....but my output is still this:

    Code:
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    430080  Springston    Ray         L   374926490    40392.37    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431E50  Heineman      Doug        A   264592791    19473.08    430080
     2    430080  Springston    Ray         L   374926490    40392.37    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    430080
     3    430080  Springston    Ray         L   374926490    40392.37    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431D50
     3    431D50  Easton        Dwight      R   193465943    30284.33    430080
     4    430080  Springston    Ray         L   374926490    40392.37    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431CD0
     3    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     4    431D50  Easton        Dwight      R   193465943    30284.33    430080
     5    430080  Springston    Ray         L   374926490    40392.37    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431CD0
     3    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     4    431D50  Easton        Dwight      R   193465943    30284.33    430080
     5    430080  Springston    Ray         L   374926490    40392.37    431C50
     6    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431C50
     7    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431B50
     7    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
     8    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431AD0
     7    431AD0  Pruitt        Terry       O   846239402    19374.50    431B50
     8    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
     9    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431A50
     7    431A50  Ma            Glen        T   264957640    134.29    431AD0
     8    431AD0  Pruitt        Terry       O   846239402    19374.50    431B50
     9    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
    10    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    4319D0
     7    4319D0  Lewis         Troy        R   002947594    19374.12    431A50
     8    431A50  Ma            Glen        T   264957640    134.29    431AD0
     9    431AD0  Pruitt        Terry       O   846239402    19374.50    431B50
    10    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
    11    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431950
     7    431950  Khan          Eddy        U   947604592    9476.66    4319D0
     8    4319D0  Lewis         Troy        R   002947594    19374.12    431A50
     9    431A50  Ma            Glen        T   264957640    134.29    431AD0
    10    431AD0  Pruitt        Terry       O   846239402    19374.50    431B50
    11    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
    12    431C50  Johnson       Devin       E   736549283    197493.93    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431950
     7    431950  Khan          Eddy        U   947604592    9476.66    4319D0
     8    4319D0  Lewis         Troy        R   002947594    19374.12    431A50
     9    431A50  Ma            Glen        T   264957640    134.29    431AD0
    10    431AD0  Pruitt        Terry       O   846239402    19374.50    431B50
    11    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
    12    431C50  Johnson       Devin       E   736549283    197493.93    4318D0
    13    4318D0  Tran          Michael     B   204946731    652.09    NULL
    
    
    NUM   ADDRESS  LAST        FIRST       MI    SSN        BALANCE    NEXT
    -------------------------------------------------------------------------
    
     1    431DD0  Alagappan     Solayappan  Q   567493784    20493.76    431E50
     2    431E50  Heineman      Doug        A   264592791    19473.08    431BD0
     3    431BD0  Fazal         Talha       S   643847651    2947.08    431CD0
     4    431CD0  Chatila       Sami        K   029375938    1943.88    431D50
     5    431D50  Easton        Dwight      R   193465943    30284.33    430080
     6    430080  Springston    Ray         L   374926490    40392.37    431850
     7    431850  Hopkins       Weston      S   463947204    2940.45    431950
     8    431950  Khan          Eddy        U   947604592    9476.66    4319D0
     9    4319D0  Lewis         Troy        R   002947594    19374.12    431A50
    10    431A50  Ma            Glen        T   264957640    134.29    431AD0
    11    431AD0  Pruitt        Terry       O   846239402    19374.50    431B50
    12    431B50  Punjal        Jagan       D   463248598    29473.71    431C50
    13    431C50  Johnson       Devin       E   736549283    197493.93    4318D0
    14    4318D0  Tran          Michael     B   204946731    652.09    NULL
    
    
    Press any key to continue
    I have no clue why its doing this

  14. #44
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    fixed, acidently screwed up the search function to work out where it goes, before it was checking the current lists lname with the value to insert, if it was bigger then it put it after it instead of before it. So instead I made it check the next->lname value instead:

    Code:
    // RETURN 1 IF INSERTION IS SUCCESSFUL
    int addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
      CUSTOMER *tempptr;
      CUSTOMER *tempptr2;
    
      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->next->lname,temprecord->lname)<0))
          tempptr=tempptr->next;
    
        if((*bankrecords==tempptr) && (strcmp(tempptr->next->lname,temprecord->lname)>0))	// RECORD BELONGS AT START OF LIST
        {
          tempptr2=tempptr;
          *bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
          tempptr = *bankrecords;
          tempptr->next=tempptr2;
        }
        else				// RECORD IS SOMEWHERE IN MIDDLE OR END
        {
          if(tempptr->socialnum != temprecord->socialnum)
          {
            tempptr2=tempptr->next;
            tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
            tempptr=tempptr->next;
            tempptr->next=tempptr2;
          }
          else return 0;
        }
      }
    
      //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;
      return 1;
    }

  15. #45
    Unregistered
    Guest
    ok im trying to set up a way for me to be able to make adjustments to my list, i have started but its beginning to get confusing to me. im tyring to set up a switch statement or something where like

    If the Social Security number does NOT already exist in the linked list, AND
    the transaction request is a DEPOSIT, then
    1. print out a statement letting them know they are being added

    If the Social Security number does NOT already exist in the linked list, AND
    the transaction request is a WITHDRAWAL, then

    1. print out statement saying they do not exist in the list

    If the Social Security number DOES already exist in the linked list, AND the
    transaction request is a DEPOSIT, then
    1. print out statement saying that the amount is being added

    If the Social Security number DOES already exist in the linked list, AND the
    transaction request is a WITHDRAWAL, and the amount to be withdrawn does NOT
    exceed the balance, then
    1. statement saying they are making a withdrawal

    If the Social Security number DOES already exist in the linked list, AND the
    transaction request is a WITHDRAWAL, AND the amount to be withdrawn exceeds
    the balance, then
    1.message saying their acount will be close and then delete the account.

    How can i go about this.

    Code:
    int findinlist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
    {
    	CUSTOMER *tempptr;
    	int test;
    	tempptr = *bankrecords;
    
    	
    	
    	if(tempptr==NULL)
    		return 0;
    	else
    	{
    	while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
    			tempptr=tempptr->next;
    		}
    
    	if((temprecord->balance) > 0 )
    		test=1;
    	if((temprecord->balance < 0 ) && (temprecord->balance < tempptr->balance))
    		test=2;
    		
    	
        if(tempptr==NULL) {
    		return 0;
    	}
    	if(test==0)
    		printf("Error");
    	else if(test==1) {
    		printf("%s %c %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,temprecord->balance);
    		tempptr->balance = tempptr->balance + temprecord->balance;
    		printlist(bankrecords);
    		return 1;
    	}
    	else if (test==2) {
    		printf("%s %c %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
    		tempptr->balance = tempptr->balance - temprecord->balance;
    		printlist(bankrecords);
    		return 1;
    	}
    			
    			
      }
    }

Page 3 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, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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