Linked List Part 2

This is a discussion on Linked List Part 2 within the C Programming forums, part of the General Programming Boards category; Oh please, can't you see that replacing Code: for ( i = 0 ; i < sizeof(lines)/sizeof(*lines); i++ ) { ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Oh please, can't you see that replacing
    Code:
        for ( i = 0 ; i < sizeof(lines)/sizeof(*lines); i++ ) {
            decode(lines[i]);
        }
    With
    Code:
        while ( fgets( buff, BUFSIZ, file_pointer ) != NULL ) {
            decode( buff );
        }
    is pretty much all you need to do.

    It's a way of testing out ideas by having an array of example lines embedded in the code (to make it easy to test), rather than having to wade through masses of file data.
    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.

  2. #17
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Filling Linked List after Reading File

    Great.
    I get it now.
    Well OK now we have finished the file reading. So I have my values for cordinates and track nos. I check if type is X or Y and accordingly set them as x1 or x2 etc.
    Now I come to filling up my list with these values.
    I had some conditions for the same i.e. I compare my values of x1 x2 y1 and y2 and then get the element of the list which I am supposed to fill.
    So I will have to call this function for filling my list inside the file read function i.e. decode (just at the beginning so that the check is done everytime).

    This is my funtion for filling the list:

    Code:
    void fill_list (int x1, int x2, int y1, int y2, int trackno1, int trackno2)
    {
      if (x1==0 && x2==0 && y1==0 && y2==0)
    	{
    	  goto END;
    	}
          else
    	{ 
    	  if (x1==x2)
    	    {
    	      if (y1==y2)
    		{
    		  temp=Layout[17-y1][x1].Array[19+trackno2];
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		  
    		  temp=Layout[17-y1][x1].Array[19-trackno1];
    		  temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		}
    	      
    	      else
    		{
    		  if (y2==y1+1)
    		    {
    		      temp=Layout[17-y1][x1].Array[19-trackno1];
    		      temp->line_used=1;
    		      
    		      temp=Layout[17-y1][x1].Array[9-trackno2];
    		      temp=temp->nextptr;
    		      temp->line_used=1;
    		}
    		  else 
    		    {
    		      if (y2 > y1+1)
    			{
    			  temp=Layout[17-(y2-1)][x1].Array[19+trackno2];
    			  temp->line_used=1;
    			  
    			  temp=Layout[17-(y2-1)][x1].Array[9-trackno2];
    			  temp=temp->nextptr;
    			  temp=temp->nextptr;
    			  temp->line_used=1;
    			}
    		    }
    		}
    	    }
    	  else
    	    {
    	      if (y1==y2) 
    		{
    		  if (x1==x2+1)  
    		    {
    		      temp=Layout[17-y2][x2].Array[19+trackno2];
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		      temp->line_used=1;
    		      
    		      temp=Layout[17-y2][x2].Array[29+trackno1];
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		      temp->line_used=1;
    		    }
    		  else
    		    {
    		      if (x1 > x2+1)
    			{
    			  temp=Layout[17-y1][x1-1].Array[19-trackno1];
    			  temp=temp->nextptr;
    			  temp=temp->nextptr;
    			  temp=temp->nextptr;
    			  temp->line_used=1;
    			  
    			  temp=Layout[17-y1][x1-1].Array[29+trackno1];
    			  temp=temp->nextptr;
    			  temp->line_used=1;
    			}
    		    }
    		}
    	      else
    		{
    		  if (y2==y1+1)
    		    {
    		      temp=Layout[17-y1][x2].Array[9-trackno2];
    		      temp=temp->nextptr;
    		      temp=temp->nextptr;
    		  temp=temp->nextptr;
    		  temp->line_used=1;
    		  
    		  temp=Layout[17-y1][x2].Array[29+trackno1];
    		  temp->line_used=1;
    		    }
    		}
    	    }
         END:
    }
    Does this part require any change to it. It is compiling if I run only this part.

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Start by having a single file containing fill_list, and a main() containing some data and calls to fill_list (like I just showed you).

    Then add some code to just print a list.

    Add test cases and fixes to the code until you're happy with the way fill_list is working.

    When you're happy it's working, move the function to your main program.
    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.

  4. #19
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Fill_list function

    OK there is a thing I am not understanding. In my decode function after I get all the variables I check to see if CHAN is X or Y if X i put values of x in x1 y in y1 else if Y i put x in x2 and so on. Now I am supposed to call my function to fill the list everytime I have these values of x1 and x2. So I am passing x1,x2,y1,y2,trackno1,trackno2 as paramentrs into my fill_list function so I will need to initialize these variables to 0 somewhere, if I do not I get an error. The question is where to initialize them, I cannot at the start of the function as then everytime I will be passing 0s only.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 12:56 PM
  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. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

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