priority list won't work, i give up

This is a discussion on priority list won't work, i give up within the C Programming forums, part of the General Programming Boards category; Hi, I am creating a priority list which is a part of a larger program. When the function runs, it ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    priority list won't work, i give up

    Hi,

    I am creating a priority list which is a part of a larger program. When the function runs, it just keeps running forever without stopping. The program reads from the file and creates a priority linked list based on "priority" variable. If priority is the same, it does lexical comparison of the "mnemonic" string and prioritizes based on that. I tried inserting printf's but they fail at indicated comments.

    Here is the struct, along with the global struct pointer:

    Code:
    struct ot_entry {
         char    mnemonic[OP_LENGTH + 1];
         int     format;                           
         char    opcode;   
         int     priority;  
         struct  ot_entry  *next;
    };
    
    struct  ot_entry  *optbl;
    OP_LENGTH is 6

    Here is a function:

    Code:
    void build_optbl()
    
    {
    	struct ot_entry *new, *curr;
    	struct ot_entry *temp = NULL;
    	char mnemonic[OP_LENGTH + 1]; /* temp storage of the values from file*/
    	int format, priority, opcode;        
    
    	optbl = (struct ot_entry *)malloc(sizeof(struct ot_entry)); //first node in the priority list
    
    	fscanf(ot_fp, "%s", mnemonic);
    	fscanf(ot_fp, "%d", &format);
    	fscanf(ot_fp, "%x", &opcode);
    	fscanf(ot_fp, "%d", &priority);
    	strcpy(optbl->mnemonic, mnemonic);
    	optbl->format = format;
    	optbl->opcode = (char)opcode;
    	optbl->priority = priority;
    	optbl->next = NULL;
    	
    	while (!feof(ot_fp))
    	{
    		new = (struct ot_entry *)malloc(sizeof(struct ot_entry));
    	
    		fscanf(ot_fp, "%s", mnemonic);
    		fscanf(ot_fp, "%d", &format);
    		fscanf(ot_fp, "%x", &opcode);
    		fscanf(ot_fp, "%d", &priority);
    		strcpy(new->mnemonic, mnemonic);
    		new->format = format;
    		new->opcode = (char)opcode;
    		new->priority = priority;
    		new->next = NULL;
    		
    		curr = optbl;
    		temp = NULL;
    		
    		while(new->priority > curr->priority)
    			{
    			temp = curr;
    			curr = curr->next;
    			}
    		
    		if(new->priority == curr->priority)
    			{
    			
    			//prints here, only things with newline char
    
    			while((strcmp(new->mnemonic, curr->mnemonic) > 0) && (curr != NULL) && (new->priority == curr->priority));
    				{
                                    //does not print here
    				temp = curr;
    				curr = curr->next;
    				}
                            //does not print here either and further on
    			if (curr == NULL)
    				temp->next = new;
    				else if (new->priority != curr->priority)
    						  {
    						  temp->next = new;
    						  new->next = curr;
    						  } 
    						  else if (curr == optbl)
    					  				 {
    					  				 new->next = curr;
    					  				 optbl = new;
    					  				 }
    				  				 else
    					  				 {
    					  				 temp->next = new;
    					  				 new->next = curr;
    					  				 }			 		
    			
    			
    			}
    			else if (curr == NULL)                    
    					  {
    					  temp->next = new;
    					  }
    				  else if (curr == optbl)
    				  	  		 {
    					  		 new->next = curr;
    					  		 optbl = new;
    					  		 }
    					  	 else 
    					  		 {
    							 temp->next = new;
    							 new->next = curr;
    							 }
    							 
    
    	}
    		
    }
    "temp" should keep track of the previous node and "curr" is the current node. It inserts the "new" node in between these, unless it is at the very end. Then it just appends the node to the end.

    File pointer works and I've checked if the fscanf's record the values correctly.

    Here is the input file:

    Code:
    ADD    3  18  10
    ADDF   3  58  10
    ADDR   2  90  10
    AND    3  40  10
    BASE   0  FF  50
    BYTE   0  FF  50
    CLEAR  2  B4  12
    COMP   3  28  12
    COMPF  3  88  12
    COMPR  2  A0  12
    CSECT  0  FF  50
    DIV    3  24  13
    DIVF   3  64  13
    DIVR   2  9C  13
    END    0  FF  50
    EQU    0  FF  50
    EXTDEF 0  FF  50
    EXTREF 0  FF  50
    FIX    1  C4  15
    FLOAT  1  C0  15
    HIO    1  F4  17
    J      3  3C  19
    JEQ    3  30  19
    JGT    3  34  19
    JLT    3  38  19
    JSUB   3  48  19
    LDA    3  00  21
    LDB    3  68  21
    LDCH   3  50  21
    LDF    3  70  21
    LDL    3  08  21
    LDS    3  6C  21
    LDT    3  74  21
    LDX    3  04  21
    LPS    3  D0  21
    MUL    3  20  22
    MULF   3  60  22
    MULR   2  98  22
    NOBASE 0  FF  50
    NORM   1  C8  23
    OR     3  44  24
    RD     3  D8  27
    RESB   0  FF  50
    RESW   0  FF  50
    RMO    2  AC  27
    RSUB   3  4C  27
    SHIFTL 2  A4  28
    SHIFTR 2  A8  28
    SIO    1  F0  28
    SSK    3  EC  28
    STA    3  0C  28
    START  0  FF  50
    STB    3  78  28
    STCH   3  54  28
    STF    3  80  28
    STI    3  D4  28
    STL    3  14  28
    STS    3  7C  28
    STSW   3  E8  28
    STT    3  84  28
    STX    3  10  28
    SUB    3  1C  28
    SUBF   3  5C  28
    SUBR   2  94  28
    SVC    2  B0  28
    TD     3  E0  29
    TIO    1  F8  29
    TIX    3  2C  29
    TIXR   2  B8  29
    WD     3  DC  32
    WORD   0  FF  50

    I'm a little clueless here. Why doesn't it work?

    Thanks in advance for your help.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Just realized that:

    Code:
    while(new->priority > curr->priority)
    needs to be changed to:

    Code:
    while(new->priority < curr->priority)
    That is irrelevant to the issue I'm having, though.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm a little clueless here. Why doesn't it work?
    You tell me. Why doesn't it work? What is it doing that it shouldn't? What doesn't it do that it should? In short, ask smart questions.

    You can probably solve your own problem by simply printing values as you get them and transfer them. Try printing out each node. Make a function that prints the entire list. Things like this.

    [edit]
    You should break that up into different functions. Make a functon that simply reads the contents of one entry and puts it in a node. Then you can test that one function, make sure it works fine, then add it to your list. Then test that function.

    In short, a "function" should be just that. One task. For the most part anyway. It aids in debugging, because you can test them one at a time to make sure each little piece works.
    [/edit]


    Quzah.
    Last edited by quzah; 10-17-2006 at 08:24 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I did not look all that closely, however, you may want to use fgets and sscanf for your input gathering (as opposed to fscanf checking for feof). Reason: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    It doesn't even reach the next iteration of the loop with feof in it. The comments I placed indicated failure to print during the first run; it never goes past that. It is weird that it keeps running endlessly with no error messages. I tried inserting printf's everywhere. The loops that are irrelevant for the first case (i.e. second line of the input file), are skipped, of course.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( buf, BUFSIZ, fp ) != NULL )
        ...now you have one line...
        ...parse the buffer into a new node...
        ...put the node into the list...
    }
    Start simple. Just read a line from your file. Print it out. Try putting the info into a single structure instance. Just use a standard structure instance, don't fiddle with allocation. Once that works, do the same, but change it to use a node you allocate. Allocate, stick it in the node, print it, free it. Once that works, instead of freeing it, stick it on your list.


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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > It doesn't even reach the next iteration of the loop with feof in it.
    Read the FAQ on why feof() is bad at controlling a loop.

    >optbl = (struct ot_entry *)malloc(sizeof(struct ot_entry));
    Read the FAQ on why casting malloc in C is poor form.

    You really need to separate the "reading the data" from "building the list" code.

    The list code you should be able to wrap up with functions like this.
    Code:
    struct ot_entry *list, *node;
    list = initList();
    node = createNode();
    // fill in some detail
    list = appendList( list, node );
    > while((strcmp(new->mnemonic, curr->mnemonic) > 0) && (curr != NULL) && (new->priority == curr->priority));
    The ; at the end of this line is a real killer.
    Do nothing, FOREVER!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    I feel like a total n00b, especially about the last one, lol. Anyway, I'll try to separate reading and linked list operations. Yeah, when I fixed the last one, it gives me seg fault.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Post your latest code then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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