Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is code i have been trying which adds two linked list. Given a list for example like 3->4->5 and 1->6->3 ,this results into a new list 4->0->9 .
The most significant digit is the leftmost in the list. But the result is not expected . The printlist funtion prints the result linked list and it gives only one value 0 .Please help me out?

void add_list(struct list *node1,struct list *node2)
{
  if(node1==NULL && node2==NULL)

    {
      printf("empty list\n");
    }
  if(node1==NULL){
    return node2;
  }
  if(node2==NULL){
    return node1;
  }
  int i, carry=0;
  
  struct list *ptr1=node1,*ptr2=node2;
  
  struct list *result;
  
  result=malloc(sizeof(struct list));

  while(node1->next!=NULL && node2->next==NULL)

{
      result->data=((node1->data)+(node2->data)+(carry));
      node1=node1->next;
      node2=node2->next;
      result=result->next;
      if((result->data)>9){
         carry=(result->data)%10;
         
          }
  
    }
  return result;
}
 
   printlist(result);

 }</pre>
Posted
Updated 6-Sep-13 4:26am
v5
Comments
Richard MacCutchan 6-Sep-13 9:25am    
Most interesting, and do you have a question?
Member 10200096 6-Sep-13 9:51am    
yes ! i updated it . I forgot to mention the problem

Your function makes little sense as you're trying to return values from a function with void return type;

Something like this might work for you:

C++
#include <stdlib.h>
#include <stdio.h>


typedef struct list
{
    int data;
    list* next;
} list;

list* add_node(list* root, int data) {
    list* node = (list*)malloc(sizeof(node));
    node->data = data;
    node->next = NULL;
    if (root != NULL) {
        list* ptr = root;
        while(ptr->next != NULL) {
            ptr = ptr->next;
        }
        ptr->next = node;
    }

    return node;
}

void delete_list(list* root)
{
    list* ptr = root;
    while(ptr != NULL) {
        list* node = ptr;
        ptr = ptr->next;
        free(node);
    }
}

void print_list(list* root) {
    list* ptr = root;
    while(ptr != NULL) {
        printf("%d->", ptr->data);

        ptr = ptr->next;
    }
}

void add_list(list*l1, list* l2)
{
  list *l1Ptr = l1;
  list *l2Ptr = l2;


  int carry = 0;
  list *result = NULL;
  while(l1Ptr != NULL && l2Ptr != NULL) {
      int sum = l1Ptr->data + l2Ptr->data + carry;
      if (sum > 9) {
          sum %= 10;
          carry = 1;
      }

      if (result == NULL)
          result = add_node(result, sum);
      else
          add_node(result, sum);

      l1Ptr = l1Ptr->next;
      l2Ptr = l2Ptr->next;
  }
  printf("Result: ");
  print_list(result);
  printf("\r\n");
}

int main(int argc, char* argv[])
{
    list* l1 = add_node(NULL, 3);
    add_node(l1, 4);
    add_node(l1, 5);

    list* l2 = add_node(NULL, 1);
    add_node(l2, 6);
    add_node(l2, 3);

    printf("List 1: ");
    print_list(l1);
    printf("\r\n");
    printf("List 2: ");
    print_list(l2);
    printf("\r\n");


    add_list(l1, l2);


    return 0;
}


Hope this helps,
Fredrik
 
Share this answer
 
Well one problem that springs to mind is that you only ever allocate a single element in result, so you can't return more than a single element.
Second, you exit the function after you have processed the first element in both the lists!

A couple of suggestions for you:
1) sort out your indentation - as it is, it reflects a flow of control which does not happen.
2) Use a debugger, and step through your code - you would have spotted the return was in the loop almost immediately!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900