添加两个链表 [英] Adding two linked list

查看:74
本文介绍了添加两个链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在尝试的代码,它添加了两个链表。给定例如3-> 4-> 5和1-> 6-> 3的列表,这导致新的列表4-> 0-> 9。

最重要的数字是列表中最左边的数字。但结果并未预料到。 printlist功能打印结果链表,它只给出一个值0。请帮帮我吗?



 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->数据)+(node2->数据)+(进位));
node1 = node1-> next;
node2 = node2-> next;
result = result-> next;
if((result-> data)> 9){
carry =(result-> data)%10;

}

}
返回结果;
}

printlist(结果);

}< / pre>

解决方案

当您尝试返回时,您的功能毫无意义来自具有void返回类型的函数的值;



这样的东西可能适合你:



< pre lang =cpp> #include < < span class =code-leadattribute> stdlib.h >
#include < stdio.h < span class =code-keyword>>


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

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

return 节点;
}

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->数据);

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( 结果:);
print_list(结果);
printf( \\\\ 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( \\\\ n);
printf( List 2:);
print_list(l2);
printf( \\\\ n);


add_list(l1,l2);


return 0 ;
}





希望这会有所帮助,

Fredrik


< blockquote>首先想到的一个问题是你只在结果中分配一个元素,所以你不能返回多个元素。

其次,你有以后退出函数处理了两个列表中的第一个元素!



给你的一些建议:

1)整理你的缩进 - 因为它是,它反映了一个没有发生的控制流。

2)使用调试器,并逐步执行代码 - 你几乎可以立即发现返回在循环中!


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>

解决方案

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:

#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


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!


这篇关于添加两个链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆