Thread: Help with Linked Lists

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Help with Linked Lists

    I'm trying to use a for loop to create new structures of a linked list, but I keep overwriting the last struct and I don't know how to fix it.

    Code:
    struct node *make_linked_list(char *pointArray[], int N, int polyNumber) { 
    	struct node *first, *current, *newNode, *o;
    	char *term, *poly, sign = ' ';
    	int i, num;
    
    	poly = pointArray[0];
    	//beginning of the linked list
      	current = calloc(1,sizeof(struct node));
    	first = current;
    	/* Read the first term of the polynomial. From there, a new node will be created
    	 * for each term. Do this for however many polynomials are in the file.
    	 */
    	term = strtok(poly, " ");
            for(i=0; term != NULL; i++) {
    		term = strtok(NULL, " "); 
     .
     .
     .
    		newNode = calloc(1,sizeof(struct node));
    		current->next = newNode;
    		current = current->next;
    return first
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It does not help when you leave out the bit that probably has the bug in it. You're missing a bracket too.
    If you want real help you have to post the real code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    I figured out how to create a linked list. Now I'm having a problem with a while loop that adds 2 polynomials together. Everything seems to work correctly up until exiting where the loop checks the condition after the last term of the polynomial and then blows up.
    Code:
    struct node *add(struct node *head1, struct node *head2) { //Use only for ordered lists
    	struct node *poly_a, *poly_b, *sum, *tsum;
    
    	poly_a = head1;
    	poly_b = head2;
    	sum = calloc(1,sizeof(struct node));
    	sum->next = calloc(1,sizeof(struct node));
    	tsum = sum;
    	while(poly_a->next != NULL || poly_b->next != NULL) {
    		/* If a term is present in the first polynomial and not the second,
    		 * copy coeff and exp into the sum polynomial structure.
    		 */
    		if(poly_a->exp > poly_b->exp) {
    			tsum->exp = poly_a->exp;
    			tsum->coeff = poly_a->coeff;
    			poly_a = poly_a->next;
    			tsum->next = calloc(1,sizeof(struct node));
    			tsum = tsum->next;
    			continue;
    		}
    		/* If a term is present in the second polynomial and not the first,
    		 * copy coeff and exp into the sum polynomial structure.
    		 */
    		else if(poly_b->exp > poly_a->exp) {
    			tsum->exp = poly_b->exp;
    			tsum->coeff = poly_b->coeff;
    			poly_b = poly_b->next;
    			tsum->next = calloc(1,sizeof(struct node));
    			tsum = tsum->next;
    			continue;
    		}
    		/* If a term is present in both polynomials, add coefficients and copy exp into the sum 
    		 * polynomial structure.
    		 */
    		else if(poly_b->exp == poly_a->exp) {
    			tsum->exp = poly_b->exp;
    			tsum->coeff = poly_b->coeff + poly_a->coeff;
    			tsum->next = calloc(1,sizeof(struct node));
    			tsum = tsum->next;
    			poly_b = poly_b->next;
    			poly_a = poly_a->next;
    		}
    
    	}
    	return sum;
    }
    Last edited by arrgh; 04-17-2008 at 03:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM