C语言,超长整数加减,双向循环链表

本文探讨如何使用C语言通过双向循环链表处理超长整数的加减操作,详细阐述了相关算法设计与实现过程。
摘要由CSDN通过智能技术生成

emm,太多了,放不下main了
在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct node//节点,双向循环链表
{
   
    int num;
    struct node *pre;
    struct node *next;
} node;
void Init(node *head, int n)//初始化
{
   
    //n是num的位数,其符号代表num的符号,位数
    head->next = head;
    head->pre = head;
    head->num = n;
}
void Insert(node *head, int e, int pos)
{
    //插在第pos个节点之后
    node *s = (node *)malloc(sizeof(node));//申请节点
    if (s == NULL)
    {
   
        printf("malloc fail in Insert");
        return;
    }
    s->num = e;
    node *p = head;
    for (int i = pos; i > 1; i--)//找位置
    {
   
        p = p->next;
    }
    s->next = p->next;//插入
    p->next = s;
    s->next->pre = s;
    s->pre = p;
}
int length(node *head)//返回链表长度
{
   
    node *p = head;
    int len = 1;
    while (p->next != head)//遍历
    {
   
        len++;
        p = p->next;
    }
    return len;
}
void read(node *head, int m)//从键盘读入数据
{
   
    int e;
    int n = abs(m);//去掉符号
    switch (n % 4)
    {
   
    case 1://若是1/5/9之类的
        scanf("%1d", &e);//首位只存一位数字
        Insert(head, e, length(head));
        for (int i = abs(n - 1) / 4; i > 0; i--)//假如是9,需要三个节点存储,(除首位外)每个节点存储一个四位数
        {
   
            scanf("%4d", &e);
            Insert(head, e, length(head));
        }
        break;
    case 2://若是2/6/10之类的
        scanf("%2d", &e);//首位存两位数字
        Insert(head, e, length
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值