Linked List(+reverse) trouble

This is a discussion on Linked List(+reverse) trouble within the C Programming forums, part of the General Programming Boards category; i am having a problem figuring out how to reverse my linked list. I have the data being read from ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    43

    Linked List(+reverse) trouble

    i am having a problem figuring out how to reverse my linked list. I have the data being read from an infile p2data.txt which displays the first name, last name, rank, and score of 20 people. My instructions are to use the link i have to make another reversed link. Any ideas? here is my code..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stud_info
    {
    	char fname[15] ;
    	char lname[15] ;
    	int rank ;
    	float score ;
    	struct stud_info* prev,*next ;
    } ;
    
    
    
    int main()
    {
        FILE* infile;
    	struct stud_info* pnew_rec ;
    	struct stud_info* top ;
    	struct stud_info* move_pntr ;
    
    infile = fopen("p2data.txt", "r") ;
    
    
    		pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
    
    		fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) ;
    		pnew_rec->next = NULL ;
    
    		top = pnew_rec ;
    
    		while( !feof(infile) )
    		{
    			pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ; //get block of mem
    			fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) ;
    			
    			pnew_rec->next = top ;
    
    			top = pnew_rec ; //save block of mem
    
    		}
    
    		
    		move_pntr = top ;
    		while( move_pntr != NULL )
    		{
    
    			printf( "%15s %15s %4d %5.2f \n", move_pntr->fname , move_pntr->lname, move_pntr->rank, move_pntr->score ) ;
    			move_pntr = move_pntr->next ;
    		}
    
    		
    
    
    			fclose(infile);
    					
    
    return (0) ; //end of func main
    }

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    I bet the purpose of your assignment is to teach you a useful feature of another data structure. Think about other data structures you have (recently) learned about.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Can I answer with hostility, sarcasm, and code that will generate seg faults?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I've already completed your assignment and I've used the following:

    Code:
    struct stud_info* top = NULL, *bottom = NULL;
    
    void StoreRecord( struct stud_info  *i,  /* new element */
                    struct stud_info  **start, /* first element */
                    struct stud_info  **last /* last element */
                    )
    Thus, you should create a separate function for storing the records in the linklist. In other words, get it out of main to keep things neat. You'll notice that I keep track of the last item (bottom struct) in the linked list. This is used to display the list from last to first using the bottom struct. The start item (top struct) is used to display the the list from first to last.

    As you read a record in from your text file, call StoreRecord to put that record in the linked list.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    do you think something like a swap function would work in this case?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by killmequick View Post
    do you think something like a swap function would work in this case?
    Well, I'm not aware of any "swapping" method to traverse the list from bottom to top. If you notice in my StoreRecord function that I'm always updating the top (first record) and bottom (last record) pointer. Normally, you would not have to do this. You would update the top pointer to obviously point to the first record. And obviously, the bottom pointer would be updated once to point to the last record. But I've added a twist to your code by sorting the list based on Lname. I've done this by "swapping" the Link List pointers. Thus, top and bottom pointer are updated with each record read. Now to traverse the list from bottom to top would be done as follows:

    Code:
    for(move_pntr = bottom; move_pntr != NULL; move_pntr = move_pntr->prev)
            printf( "%15s %15s %4d %5.2f \n", move_pntr->fname , move_pntr->lname, move_pntr->rank, move_pntr->score ) ;
    So, any type of "swapping", IMHO would be only done to sort the list. And to reiterate, I just can't see how "swapping" can be easily implemented to traverse a linked list

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    with your code and some other work i manage to get the list to print the first ten numbers in order..1,2,3,4,5,6,7,8,9,10 but after this the list was made in such a way that if i just print the bottom it will be 20,19,18,17,16,15,14,13,12,11. I can probably figure that out on my own but my real problem is in my third while loop that prints the bottom portion of the numbers it keeps giving me a debugging error at line 86. Suggestions?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stud_info
    {
    	char fname[20] ;
    	char lname[20] ;
    	int rank ;
    	float score ;
    	struct stud_info* prev,* next ;
    } ;
    
    
    
    int main()
    {
        FILE* infile;	
    	struct stud_info* pnew_rec ;
    	struct stud_info* link2 ;
    	struct stud_info* top ;
    	struct stud_info* bottom ;
    	struct stud_info* move_pntr ;
    	struct stud_info* move_pntr2 ;
    	
    
    infile = fopen("p2data.txt", "r") ;
    
    
    		pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
    		fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) ;
    		
    		pnew_rec->next = NULL ;
    		
    		
    		
    		top = pnew_rec ;
    		bottom = pnew_rec ;		
    		
    		
    
    		while( !feof(infile) )
    		{ 
    
    			
    			pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
    			fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) ;			
    			
    			pnew_rec->next = top ;
    			top = pnew_rec ;						
    			
    			
    			
    			link2 = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ; //get block of mem for link 2
    			fscanf( infile, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
    			
    			
    			link2->prev = bottom ;
    			bottom = link2 ;
    			
    		
    		
    		}			
    
    
    			move_pntr = top ;
    					
    
    
    			while( move_pntr != NULL )
    		{
    
    			printf( "%15s %15s %4d %5.2f \n", move_pntr->fname , move_pntr->lname, move_pntr->rank, move_pntr->score ) ;
    			move_pntr = move_pntr->next ;	
    			
    				
    		}
    			
    			
    			move_pntr2 = bottom ;
    			
    		
    			while(move_pntr2 != NULL)
    		{
    			printf( "%15s %15s %4d %5.2f \n", move_pntr2->fname , move_pntr2->lname, move_pntr2->rank, move_pntr2->score ) ;
    			move_pntr2 = move_pntr2->prev ;
    		}
    		
    		
    		
    		
    
    
    			fclose(infile);
    					
    
    return (0) ; //end of func main
    }

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I've commented out nonrelevant code statements from your post which would probably send you off on a tangent. Also, I've added comments indicating what coding is necessary to complete your assignment. A total of nine code statements are needed in the if/else statement to complete the task. Pay close attention to the comments in the if/else statement. And, yes, I've completed this assignment to verify my posts.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stud_info
    {
        char fname[20] ;
        char lname[20] ;
        int rank ;
        float score ;
        struct stud_info* prev,* next ;
    } ;
    
    int main(void)
    {
        FILE* infile = NULL;   
        struct stud_info* pnew_rec = NULL;
        struct stud_info* link2 = NULL;
        struct stud_info* top = NULL;
        struct stud_info* bottom  = NULL;
        struct stud_info* move_pntr = NULL;
        struct stud_info* move_pntr2 = NULL;
    
    
        infile = fopen("p2data.txt", "r") ;
        //    pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
        //  fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) ;
        // pnew_rec->next = NULL ;
        //  top = pnew_rec ;
        //  bottom = pnew_rec ;     
        while( !feof(infile) )
        { 
            pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
            if(fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) != 4)
                break;
            //  pnew_rec->next = top ;
            
            // top is NULL, so this is first record read
            if(top == NULL)
            {
                // You'll need four code statements here to accomplish the following:
                // We'll assign the first record read to the top pointer.
                // Since this is the first record read, the bottom pointer will also point to this record
                // The new rcord prev and next point should be pointing to NULL since there are no
                // previous and next records
            }
            else{
                // You'll need five code statements here to accomplish the following:
                // We'll assign the bottom pointer to the temporary pointer called link2
                // Next we'll assign this new record read to the next pointer of link2
                // Since there are no records following the new record, we'll assign NULL to the
                // next pointer of the new record.
                // The prev pointer of the new record should be be set to link2 which was the previous
                // record at the end of the list
                // Finally, the bottom pointer should now be set to the new record read     
            }
            //top = pnew_rec ;                        
            //        link2 = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ; //get block of mem for link 2
            //      fscanf( infile, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
            //    link2->prev = bottom ;
            //  bottom = link2 ;
        }           
        move_pntr = top ;
        while( move_pntr != NULL )
        {
            printf( "%15s %15s %4d %5.2f \n", move_pntr->fname , move_pntr->lname, move_pntr->rank, move_pntr->score ) ;
            move_pntr = move_pntr->next ;   
        }
    
        move_pntr2 = bottom ;
        while(move_pntr2 != NULL)
        {
            printf( "%15s %15s %4d %5.2f \n", move_pntr2->fname , move_pntr2->lname, move_pntr2->rank, move_pntr2->score ) ;
            move_pntr2 = move_pntr2->prev ;
        }
        fclose(infile);
        return (0) ; //end of func main
    }

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Thanks a lot Bob, everything is working correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 08:11 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