Linked List problem

This is a discussion on Linked List problem within the C Programming forums, part of the General Programming Boards category; I apologize for the non explanation. What my program is supposed to do is first read input from a file ...

  1. #61
    Unregistered
    Guest
    I apologize for the non explanation. What my program is supposed to do is first read input from a file like such:

    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

    Store it in a linked list and then read data from another file similiar to this one:

    Khan Eddy U 947604592 -80.50
    Chatila Sami K 029375938 +2041.72
    Ma Glen T 264957640 -409.85
    Heineman Doug A 264592791 -503.96
    Jiang Nan A 648937212 +29478.94
    Tran Michael B 204946731 -705.08
    Davis Brian W 294765949 -80.00
    Hsieh Jill I 749394000 +8403.91
    Sharma Rashmi O 994739403 +294.08
    Chatila Sami K 029375938 -1029.84
    Chatila Sami K 029375938 -4083.19
    Nguyen Lam A 847308840 +4029.38
    Alagappan Solayappan Q 567493784 -20493.75
    Easton Dwight R 193465943 -10294.88
    Alagappan Solayappan Q 567493784 -10.00

    IT should read one record off teh second file one at a time and look to see if it already exists on the list if so it should do a series of checks like to see if its a withdrawal or a deposit and such and such. For the most part i have every thing working fine except the part where if the person exist on the list and the amount they are trying to withdraw is larger than their current balance i delete them. The only problem i have is when i read the last name Alagappan Solayappan, he is at the top of the list (the head) i need to delete the head and i can't figure out how to do it.

    The code below is the part where im trying to delete the head.

    Code:
    if((temprecord->balance < tempptr->balance))
    	{
    		fprintf(infile3,"%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
    		if(tempptr == *bankrecords) {
    			
    		}
    		else  {
    			prev->next=tempptr->next;
    			free (tempptr);
    			printlist(bankrecords);
    		}
    		return 0;
    	}

  2. #62
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    All I can really say about linked lists, is to draw a picture of each record in the list and link them with lines, and go through your program changing what the tempptr or whatever is pointing at and then if you say wanna delete a record then work out what you'll be searching for and where all the pointers will be at once you find it and try to reconnect the list and delete the record without moving a pointer off of any of the records.

  3. #63
    Unregistered
    Guest
    i drew boxes and i cannot get this last part to work for anything. The code i came up with to delete the head was this:

    Code:
    if(tempptr == *bankrecords) {
    			
    			tempptr = tempptr->next;
    			free(tempptr);
    			printlist(bankrecords);
    		}
    it did not work i got an error when i got the record that was the head and tried to free up the space.

  4. #64
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    ok ya on the right track, try:

    Code:
    if(tempptr == *bankrecords)
    {
    	*bankrecords = (*bankrecords)->next;
    	free(tempptr);
    	printlist(bankrecords);
    }
    the reason is that bankrecords should always hold the first value in the list, otherwise your freeing the piece of memory that bankrecords is pointing too without telling bankrecords what to point to, so when you print the list again it will print nothing and the value of ->next will be screwed, etc. but in your example you were deleting the second record, not the first, the above will remove the first.
    Note: if you want to use tempptr again straight after this code, you'll need to do:
    Code:
    tempptr = *bankrecords;
    hope this helps, basically if you want to change the first value of your list always use *bankrecords, otherwise you can use tempptr.

  5. #65
    Unregistered
    Guest
    Thank you so much, you have been of tremendous help. I know i have bothered you to death but thanks for helping

  6. #66
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    no problems, linked lists are always tricky because you always need to account for deleting records (nodes) in the middle and at the start and making sure that everything is pointing to something, etc.

Page 5 of 5 FirstFirst 12345
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