| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1289 人关注过本帖
标题:双向链表
只看楼主 加入收藏
菜菜的007
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2017-5-14
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
双向链表
老师布置作业:求任意大整数的加减法。
要求用双向链表,但是一脸蒙逼,一点思绪都没有,求助各位大神帮帮忙!
搜索更多相关主题的帖子: 加减法 
2017-06-05 17:41
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:20 
//很久没弄过双向链表了,用了那么长的代码才实现,真有点不好看
#include<iostream>
using std::endl;using std::cout;
using std::cin;
class Node
{
private:
    short n;
    Node*pre;
    Node*next;
public:
    short GetData(){return n;}
    void SetPre(Node*n)
    {
        pre=n;
    }
    void SetNext(Node*n)
    {
        next=n;
    }
    Node*GetPre(){return pre;}
    Node*GetNext(){return next;}
    Node(short _n=0):n(_n)
    {
        pre=next=NULL;
    }
    Node(short _n,Node*pr,Node*nt):n(-n)
    {
        pre=pr;next=nt;
        if(pr) pr->SetNext(this);
        if(nt) nt->SetPre(this);
    }
    ~Node(){delete pre;pre=0;delete next;next=0;}
    void InsertAfter(Node*n)
    {
        if(!n) return;
        pre=n;next=n->GetNext();
        n->SetNext(this);next->SetPre(this);
    }
    void InsertBefore(Node*n)
    {
        if(!n) return;
        next=n;pre=n->GetPre();
        n->SetPre(this);pre->SetNext(this);
    }
};
class Chain
{
private:
    Node*head;//哨兵
    Node*tail;//哨兵
    int size;
public:
    Chain()
    {
        head=new Node();
        tail=new Node();
        head->SetNext(tail);
        tail->SetPre(head);
        size=0;
    }
    Chain(char*str)
    {
        head=new Node();
        tail=new Node();
        head->SetNext(tail);
        tail->SetPre(head);
        size=0;
        MassPush(str);
    }
    void MassPush(char*str)
    {
        for(short i=0;i<strlen(str);i++)
        {
            Push(str[i]-'0');
        }
    }
    Node*First()
    {
        if(!size) return NULL;
        else
            return head->GetNext();
    }
    Node*Last()
    {
        if(!size) return NULL;
        else
            return tail->GetPre();
    }
    void Push(short n)
    {
        Node*nd=new Node(n);
        nd->InsertBefore(tail);size++;
    }
    void InsertAsFirst(short n)
    {
        Node*nd=new Node(n);
        nd->InsertAfter(head);size++;
    }
    void Display()
    {
        Node*i;
        for(i=First();i!=tail;i=i->GetNext())
        {
            cout<<i->GetData();
        }
    }
    int Size(){return size;}
    friend Chain operator-(Chain a,Chain b);
};
bool operator>=(Chain a,Chain b)
{
    return a.Size()>b.Size()||a.Size()==b.Size()&&a.First()->GetData()>=b.First()->GetData();
}
Chain operator+(Chain a,Chain b)
{
    Chain result;
    Node*a_digit,*b_digit;
    short a_num,b_num;
    short digit;//答案的数位
    short tag=0;//tag是进位标记
    for(a_digit=a.Last(),b_digit=b.Last();;)
    {
        a_num=a_digit?a_digit->GetData():0;
        b_num=b_digit?b_digit->GetData():0;
        digit=a_num+b_num+tag;
        tag=digit>9;digit%=10;
        result.InsertAsFirst(digit);
        a_digit=(a_digit==a.First()||a_digit==NULL)?NULL:a_digit->GetPre();
        b_digit=(b_digit==b.First()||b_digit==NULL)?NULL:b_digit->GetPre();
        if(!(a_digit||b_digit)) break;
    }
    return result;
}
Chain operator-(Chain a,Chain b)
{
    if(!(a>=b))
    {
        cout<<"本程序暂不支持负数运算。:P\n";exit(0);
    }
    Chain result;
    Node*a_digit,*b_digit;
    short a_num,b_num,digit;
    short tag=0;//退位标记
    for(a_digit=a.Last(),b_digit=b.Last();a_digit!=a.head;)
    {
        a_num=a_digit->GetData();
        b_num=b_digit?b_digit->GetData():0;
        digit=(10+a_num-tag-b_num)%10;
        result.InsertAsFirst(digit);
        tag=a_num-tag<b_num;
        a_digit=a_digit->GetPre();
        b_digit=(b_digit==b.First()||b_digit==NULL)?NULL:b_digit->GetPre();
    }
    return result;
}
int main()
{
    Chain c("87843269087");
    c.Display();cout<<endl;
    Chain d("4444");
    d.Display();cout<<endl;
    Chain e;
    e=c+d;
    e.Display();cout<<endl;
    e=c-d;
    e.Display();cout<<endl;
    return 0;
}
2017-06-07 21:43
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
Node(short _n,Node*pr,Node*nt):n(-n)        //这个构造函数弄错了,后面那个括号里应该是_n
    {
        pre=pr;next=nt;
        if(pr) pr->SetNext(this);
        if(nt) nt->SetPre(this);
    }
2017-06-07 21:45
快速回复:双向链表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016547 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved