Thread: linked list help

  1. #1
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455

    linked list help

    Code:
    struct BLOCK {
    	vector <string> say;
    	vector <string> resp;
    	int index;
    	BLOCK *next;
    };
    
    BLOCK *first;
    	BLOCK *curr;
    
    int cAIBrain::GetWords(char *strWork, char type){
    	
    	string temp;
    	int numwords=0;
    	BLOCK *insert = new BLOCK;
    
    	//clear the >, so then the words are left
    	for(int i=0;i<=strlen(strWork);i++){
    		if(*strWork=='>')
    			break;
    		
    		*strWork++;
    	}
    
    	*strWork++;	//clear the >
    	while(*strWork!='/'){
    		//next time change this with the delimiter chosen
    		//by the user, for now it is a |
    		if(*strWork=='|' || *strWork=='<'){
    			*strWork++;
    
    			if(type=='s')	 {	//if its a say block, add it to the say
    				insert->say.push_back(temp);
    			} else if(type=='r'){	//if its a resp block, add it to resp
    				insert->resp.push_back(temp);
    			}
    
    			numwords+=1;
    			temp="";
    		} else {
    			temp += *strWork;
    		}
    
    		*strWork++; //move to next character
    	}
    
    	AddNode(insert);
        
    	return numwords;
    }
    
    
    void cAIBrain::AddNode(BLOCK *node) {
    	if(first==NULL){
    		first=node;
    	} else {
    		curr=first;
    
    		while(curr!=NULL){ 
    			curr=curr->next; //ERROR HERE!
    		}
    		curr=node;
    		curr->next = NULL;
    	}
    }
    I get an error in the "AddNode" function, that's the entire code. I think maybe my linked list implementation is off. I know its not optimized etc, I'm just trying to get it to work

    i set first to NULL in the class initialization, but ahh i cant figure it out


    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It sounds as if you forgot to set node's next pointer to NULL, the memory location is indeterminate, so you get a segmentatin violation when you dereference the memory that your program doesn't own.

    -Prelude
    My best code is written with the delete key.

  3. #3
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    so before i add it, set the next to null?

    edit: did that, and did this:

    if(curr->next=NULL){
    curr->next=node;
    } else {

    while(curr->next!=NULL){
    curr=curr->next;
    }
    curr=node;
    curr->next = NULL;
    }

    i dont know what effect that will have yet on multiple blocks, but i'll see
    Last edited by the Wookie; 10-11-2002 at 04:08 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if(curr->next=NULL){
    Only one equals sign...? I think you're meant to have 2
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    ah, i am a fool.

    now i get the same error on the while loop

    "Unhandled exception at 0x0041cd33 in aimoduletest.exe: 0xC0000005: Access violation reading location 0xcdcdcdf1."

    i dohave next set as null, i dont see what the problem could be..


    another edit:

    i fixed that, now when i try to get <vector>.size(), i get a memory violation. is it because my list node isnt initialized properly?

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. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  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, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM