Linked List not advancing

This is a discussion on Linked List not advancing within the C Programming forums, part of the General Programming Boards category; I've beens sitting here trying to figure out how in the world I can't seem to understand this linked list ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Linked List not advancing

    I've beens sitting here trying to figure out how in the world I can't seem to understand this linked list I'm trying to build.

    What is happening is that my Chapter_List (linked list) isn't advancing and thus when I go to load the Binary Search Tree I'm adding to the very same BST. My issue lies in not setting up the Linked List for Chapter_List correct, I've tried using notes posted on my professor's site, but they're not working for me..

    Here is my function

    Code:
    void processChapters(struct filenames files, struct Chapter_List **chapList,struct Chapter** chap)
    {
    	struct Chapter_List *pHead, *pPre;
    	
    	pPre = *chapList;
    	
    	pHead = *chapList;
    	pHead = NULL;
    	
    	int i = 0; // index
    	int j = 0;
    	int lines = 0; // stores the lines of a chapter
    	char word[MAX]; // stores the word we're dealing with now
    	int words; // stores number of words in a line
    		
    	FILE *ifp; // the file pointer... of tribulation
    	
    	//
    	// Operation loop. Runs 3 times due to number of files defined to be in a master file
    	//
    	for(i=0;i<FILES;i++)
    	{
    		ifp = fopen(files.file1, "r");
    		fscanf(ifp,"%d",&lines); // reads in the lines in this chapter
    		
    		struct Chapter_List* thisChap = (struct Chapter_List*)malloc(sizeof(struct Chapter_List));
    		thisChap->next = NULL;
    		
    		
    		//
    		// Processing Loop: Once per line
    		//
    		for(j=0;j<3;j++){ // Process lines
    		
    			//
    			// Setup Chapter: Process chapter title
    			//
    			
    			
    			fscanf(ifp,"%s",thisChap->title); // reads the title of this chapter into the chapter list
    			fscanf(ifp,"%d",&words); // Scans in the total words
    			
    			// Processes: Rest of line
    			for(i=0;i<words;i++)
    			{
    				fscanf(ifp,"%s",word); // get word
    				thisChap->BST = insert(word, thisChap->BST,thisChap); // insert word into BST
    			}
    		
    		
    			//
    			// Display Results
    			//
    	printf("%s: %d distinct words and tree height is %d\n",thisChap->title,thisChap->words,height(thisChap->BST));
    			
    			// display words and their occurances, pre-order traveral		
    			preorder(thisChap->BST);
    			printf("\n\n");
    			
    			// 
    			// PREPARE FOR NEXT RUN
    			//
    			// Advance to next chapter in the file depending on NULL status	
    			
    			// Insert as first
    			if( pHead == NULL){
    				pHead = thisChap;
    			}
    			
    			// Insert after first
    			else
    			{
    				pHead->next = thisChap;
    				thisChap->next = NULL;
    			}
    			
    			
    		}
    
    	} // END of Operation Loop		
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,453
    pHead = *chapList;
    pHead = NULL;
    second line makes the first useless

    On the first iteration you set
    pHead = thisChap;
    On each other;

    pHead->next = thisChap;
    As a result pHead point to the first created node
    pHead-> next to the last, all other nodes are effectively lost
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    How about this, I had made this change later

    Code:
    		// Insert as first
    		if (pHead == NULL)
    			pHead = thisChap;
    		
    		// Insert after first
    		else{
    			pCur = pHead;
    			
    			// get to insert point (to node that has no next)
    			while(pCur->next != NULL)
    				pCur = pCur->next;
    			
    			pCur->next = thisChap;
    			
    		
    		}
    I'm still having the issue, not sure what to do, I've been working on this for a long time, I know I'm just getting hung up on something simple.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,453
    this should work... (but really why not store the pointer to the last insearted node till the next iteration instead of the going through whole the loop every time?)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Well cause I wanted to make sure I didn't have issues, unfortunently that isn't working, the darn assignment is due today too, I wish I didn't have these small errors all the time, it's annoying. I think I'm stuck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  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