【Add_two_numbers】

  1. 链表表示的两个数,低位在前高位在后,方便进位的时候向高位加一
  2. 链表初始化
#include<stdio.h>
#include<stdlib.h>

typedef int Elemtype;
typedef struct Node
{
    Elemtype data;
    struct Node *next;
}Node, *LinkedList;

//单链表的初始化
LinkedList LinkedListInit()
{
    Node *L;
    L = (Node*)malloc(sizeof(Node));
    if (L == NULL)
    {
        printf("申请内存空间失败\n");
    }
    L->next = NULL;
    return L;
}

//单链表的创建一:头插法建立单链表
LinkedList LinkedListCreatH()
{
    Node *L;
    L = (Node *)malloc(sizeof(Node));
    L->next = NULL;

    Elemtype x;
    while (scanf("%d", &x) != EOF)
    {
        Node *p;
        p = (Node *)malloc(sizeof(Node));
        p->data = x;
        p->next = L->next;
        L->next = p;
    }
    return L;
}

//单链表的创建二:尾插法建立单链表
LinkedList LinkedListCreatT()
{
    Node *L;
    L = (Node *)malloc(sizeof(Node));
    L->next = NULL;

    Node *r;
    r = L;
    Elemtype x;
    while (scanf("%d", &x) != EOF)
    {
        Node *p;
        p = (Node *)malloc(sizeof(Node));
        p->data = x;
        //p->next = NULL;
        r->next = p;
        r = p;
    }
    r->next = NULL;
    return L;
}

//单链表的插入,在链表的第i个位置插入x的元素
//要在第i个位置插入,就得先找到第(i-1)个位置,插在它后面
LinkedList LinkedListInsert(LinkedList L, int i, Elemtype x)
{
    Node *pre;
    pre = L;
    int tempi = 0;
    for (tempi = 1; tempi < i; tempi++)
        pre = pre->next;
    Node *p;
    p = (Node *)malloc(sizeof(Node));
    p->data = x;
    p->next = pre->next;
    pre->next = p;
    return L;
}

//单链表的删除,在链表中删除第一个值为x的元素
LinkedList LinkedListDelete(LinkedList L, Elemtype x)
{
    Node *pre, *p;
    p = L->next;
    while (p->data != x)
    {
        pre = p;
        p = p->next;
    }
    pre->next = p->next;
    free(p);
    return L;
}

//单链表的反转
LinkedList LinkedListReverse(LinkedList L)
{

    Node *rhead = NULL;
    Node *prev = NULL;
    Node *p = L->next;//如果原链表的头是一个结点,结点的内容为任意值,p要指向头的下一个结点才是链表的第一个值
    //Node *p = L;//如果原链表的头是一个指针,p直接等于Lj就可以了,L指的就是链表的第一个值
    Node *pnext = NULL;
    while (p != NULL)
    {
        pnext = p->next;
        if (pnext == NULL)
            rhead = p;
        p->next = prev;
        prev = p;
        p = pnext;
    }
    free(L);
    return rhead;    
}

int main()
{
    LinkedList list, start;

    //单链表的创建一:头插法建立单链表
    printf("请输入单链表的数据:");
    list = LinkedListCreatH();
    for (start = list->next; start != NULL; start = start->next)
        printf("%d", start->data);
    printf("\n");

    //单链表的创建二:尾插法建立单链表
    printf("请输入单链表的数据:");
    list = LinkedListCreatT();
    for (start = list->next; start != NULL; start = start->next)
        printf("%d", start->data);
    printf("\n");

    //单链表的插入,在链表的第i个位置插入x的元素
    int i,x;
    printf("请输入插入数据的位置:");
    scanf("%d", &i);
    printf("请输入插入数据的值:");
    scanf("%d", &x);
    LinkedListInsert(list, i, x);
    for (start = list->next; start != NULL; start = start->next)
        printf("%d", start->data);
    printf("\n");

    //单链表的删除,在链表中删除第一个值为x的元素
    printf("请输入要删除的元素的值:");
    scanf("%d", &x);
    LinkedListDelete(list, x);
    for (start = list->next; start != NULL; start = start->next)
        printf("%d", start->data);
    printf("\n");

    //单链表的反转
    Node *rhead;
    rhead=LinkedListReverse(list);
    for (start = rhead; start != NULL; start = start->next)
        printf("%d", start->data);
    printf("\n");

    system("pause");
    return 0;
}
//注意:结束输入的时候连续输入三个ctrl+z

The pseudocode is as following:

Initialize current node to dummy head of the returning list.
Initialize carry to 00.
Initialize pp and qq to head of l1l1 and l2l2 respectively.
Loop through lists l1l1 and l2l2 until you reach both ends.
Set xx to node pp’s value. If pp has reached the end of l1l1, set to 00.
Set yy to node qq’s value. If qq has reached the end of l2l2, set to 00.
Set sum = x + y + carrysum=x+y+carry.
Update carry = sum / 10carry=sum/10.
Create a new node with the digit value of (sum \bmod 10)(summod10) and set it to current node’s next, then advance current node to next.
Advance both pp and qq.
Check if carry = 1carry=1, if so append a new node with digit 11 to the returning list.
Return dummy head’s next node.
Note that we use a dummy head to simplify the code. Without a dummy head, you would have to write extra conditional statements to initialize the head’s value.

note:
1)进位
2)链表长度的判断(null)
3)最后一次判断进位与否决定是否创建新节点

ref:https://www.cnblogs.com/engraver-lxw/p/7620132.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_shsf

来包瓜子嘛,谢谢客官~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值