前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++类写链表

C++类写链表

作者头像
AngelNH
发布2020-04-16 15:44:42
6100
发布2020-04-16 15:44:42
举报
文章被收录于专栏:AngelNIAngelNI

贝祖定理,当且仅当 z 是 x, y 的最大公约数的倍数时,ax+by=z 有解

Class

C++中使用关键字 class 来定义类, 其基本形式如下:

代码语言:javascript
复制
class 类名
{
    public:
    //行为或属性 
    protected:
    //行为或属性
    private:
    //行为或属性
};
 
实现代码:
class Point
{
    public:
         void setPoint(int x, int y);
         void printPoint();
 
    private:
         int xPos;
         int yPos;
};
代码语言:javascript
复制
class linklist
{
    private:
        struct node
        {
            int data;
            node *next;
        }*p;
    public:
        linklist();
        void append(int num);
        void add_as_first(int num);
        void addafter(int c,int num);
        void del(int num);
        void display();
        int count();
        ~linklist();
};
linklist::linklist()
{
    p = NULL;
}
void linklist::append(int num)
{
    node *q,*t;
    if(p == NULL)
    {
        p = new node;
        p->data = num;
        p->next = NULL;
    }
    else
    {
        q = p;
        while(q->next!=NULL)
            q = q->next;
        t = new node;
        t->data = num;
        t->next = NULL;
        q->next = t;
    }
}
void linklist::add_as_first(int num)
{
    node * q;
    q = new node;
    q->data = num;
    q->next = p;
    p = q;
}
void linklist::addafter(int c,int num)
{
    node *q,*t;
    int i;
    for(int i =0;q = p;++i)
    {
        q = q->next;
        if(q ==NULL)
        {
            cout<<"\nThere are less than"<<c<<"element";
            return ;
        }
    }
    t = new node;
    t->data = num;
    t->next = q->next;
    q->next = t;
}
void linklist::del(int num)
{
    node *q,*r;
    q = p;
    if(q->data == num)
    {
        p = q->next;
        delete q;
        return ;
    } 
    r = q;
    while(q!=NULL)
    {
        if(q->data == num)
        {
            r->next = q->next;
            delete q;
            return ;
        }
        r = q;
        q = q->next;
    }
    cout<<"\nElement "<<num<<" not found";

}
void linklist::display()
{
    node *q;
    cout<<endl;
    for(q = p;q!=NULL;q=q->next)
    {
        cout<<endl<<q->data;
    }
}
int linklist::count()
{
    node * q;
    int c = 0;
    for(q = p;q!=NULL;q = q->next)
    {
        c++;
    }
    return c;
}
linklist::~linklist()
{
    node *q;
    if(p == NULL)
        return ;
    while(p!=NULL)
    {
        q = p->next;
        delete p;
        p = q;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-03-16|,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Class
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档