用C有效地增加了两个链表 [英] adding two linked lists efficiently in C

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

问题描述

我有两个链表从most-到最低显著重新presenting十进制数的数字秩序。对于如 4- GT; 7> 9> 6 5→7
答案应该是 4- GT; 8> 5→3 没有扭转名单,因为倒车名单会导致效率的降低

我想使用stack.I将遍历两种列表和数据元素推入每个链接list.Then两个单独stacks.One我弹出两个堆叠在一起,并同时添加的元素,并且如果解决问题的结果是一个两位数字没有我10模,并存储在临时variable.The余的进位存储在节点和进位加到下一总和等。
如果两个堆栈是s1和s2,结果链表水库

 温度= 0;
RES =(节点*)(的malloc(sizeof运算(节点*));而(S1->顶= - 1 || S2->!顶= - 1!)
{
    温度= 0;
    总和= POP(S1)+ POP(S2);
    N1 =(节点*)(malloc的(的sizeof(节点*));
    温度= SUM / 10;
    总和= SUM 10%;
    总和=总和+温度;
    N 1>数据= SUM;
    N 1 - >接下来=资源;
    RES = N1;
    免费N1;
    //温度= 0;
}如果((S1->顶== - 1)及及(S2->顶== - 1))
{
    返回水库;
}
否则如果(S1->顶== - 1)
{
    而(S2->顶= - 1!)
    {
        温度= 0;
        总和= POP(S2);
        总和=总和+温度;
        温度= SUM / 10;
        总和= SUM 10%;
        N1 =(节点*)(malloc的(的sizeof(节点*));
        N 1>数据= SUM;
        N 1 - >接下来=资源;
        RES = N1;
        免费N1;
    }
}
其他
{
    而(S2->顶= - 1!)
    {
        温度= 0;
        总和= POP(S2);
        总和=总和+温度;
        温度= SUM / 10;
        总和= SUM 10%;
        N1 =(节点*)(malloc的(的sizeof(节点*));
        N 1>数据= SUM;
        N 1 - >接下来=资源;
        RES = N1;
        免费N1;
    }
}返回水库;

我也碰到过这个问题很多次的面试问题,但是这是我能想到的最好的解决方案。
如果任何人都可以进来在C一些更有效的,我会很高兴。


解决方案

二遍,没有堆栈:


  • 获取两个列表的长度。


  • 创建一个节点的解决方案列表。该节点的价值初始化为零。这将举行进位。设置列表指针(称之为进指针)到该节点的位置。设置列表指针(称之为结束指针)到该节点的位置。


  • 与较长名单开始,每名超额节点,链接一个新的节点到最后的指针并为其分配多余节点的值。设定结束指针到这个新的节点。如果
    值小于9,进指针设置为新的节点。


  • 现在我们只剩下具有相同数量的节点列表都在指针每。


  • 虽然名单不为空...


    • 链接一个新的节点到最后指针,推进结束指针到该节点。


    • 获取每个列表中的值,推进各表指针到下一个节点。


    • 添加两个值加在一起。


      1. 如果值大于9,将该值设置为值模10 ,增加在进指针的节点保存的值,进指针移动到下一个节点。如果随身携带指针的值是九,设置为零并进入下一个节点。


      2. 如果值是九。设置。做任何其他事情。


      3. 如果值小于九岁。设置。设置携带指向当前节点。




  • 当你与这两个列表完成,检查解决方案指针的节点值为零。如果它是,该解决方案指针设置到下一个节点,删除不需要的额外的数字


I have two linked lists representing the digits of decimal numbers in order from most- to least-significant. for eg 4->7->9->6 and 5->7 The answer should be 4->8->5->3 without reversing the lists because reversing the lists would result in decrease of efficiency.

I am thinking of solving the problem using stack.I will traverse both the lists and push the data elements into two separate stacks.One for each linked list.Then I pop both the stacks together and add both the elements and if the result is a two digit no I 10 modulo it and store the carry in a temp variable.The remainder is stored in the node and the carry is added to the next sum and so on. if the two stacks are s1 and s2 and the result linked list is res.

temp = 0;
res = (node*)(malloc(sizeof(node*));

while(s1->top!=-1 || s2->top!=-1)
{  
    temp = 0;
    sum = pop(s1) + pop(s2);
    n1 = (node*)(malloc(sizeof(node*));
    temp = sum/10;
    sum = sum%10;
    sum = sum+temp;
    n1->data = sum;
    n1->next = res;
    res = n1;
    free n1;
    //temp=0;
}

if((s1->top==-1)&&(s2->top==-1))
{
    return res;
}
else if(s1->top==-1)
{
    while(s2->top!=-1)
    {
        temp = 0;
        sum = pop(s2);
        sum = sum + temp;
        temp = sum/10;
        sum = sum%10;
        n1 = (node*)(malloc(sizeof(node*));
        n1->data = sum;
        n1->next = res;
        res = n1;
        free n1;
    }
}
else
{
    while(s2->top!=-1)
    {
        temp = 0;
        sum = pop(s2);
        sum = sum+temp;
        temp = sum/10;
        sum = sum%10;
        n1=(node*)(malloc(sizeof(node*));
        n1->data = sum;
        n1->next = res;
        res = n1;
        free n1;
    }
}

return res; 

I have come across this problem many times in interview questions but this is the best solution that I could think of. If anyone can come with something more efficient in c i will be very glad.

解决方案

Two passes, no stack:

  • Get the length of the two lists.

  • Create a solution list with one node. Initialize the value of this node to zero. This will hold the carry digit. Set a list pointer (call it the carry pointer) to the location of this node. Set a list pointer (call it the end pointer) to the location of this node.

  • Starting with the longer list, for each excess node, link a new node to the end pointer and assign it the value of the excess node. Set the end pointer to this new node. If the value is less than 9, set the carry pointer to the new node.

  • Now we're left with both list pointers having the same number of nodes in each.

  • While the lists are not empty...

    • Link a new node to the end pointer and advance the end pointer to this node.

    • Get the values from each list and advance each list pointer to the next node.

    • Add the two values together.

      1. If value is greater than nine, set the value to value mod 10, increment the value held in the carry pointer's node, move the carry pointer to the next node. If carry pointer's value is nine, set to zero and go to next node.

      2. If value is nine. Set it. Do nothing else.

      3. If value is less than nine. Set it. Set carry pointer to current node.

  • When you're done with both lists, check if the solution pointer's node value is zero. If it is, set the solution pointer to the next node, deleting the unneeded extra digit.

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

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