栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 链表和类配合使用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 链表和类配合使用

文章目录

Goods类,用于存放每件货物重量,total_weight用来存储全部重量 ,通过三个函数实现,对链表的增删遍历

#include 

using namespace std;

class Goods
{
public:
    Goods()
    {
        m_weight = 0;
        m_next = NULL;
    }
    Goods(int weight)
    {
        this->m_weight = weight;
        m_next = NULL;
        total_weight += weight;
        cout<<"当前添加重量为:"<m_weight<<"总重量为:"<m_weight;
    }
    ~Goods()
    {
        cout<<"已删除"<m_weight;
    }
    Goods* m_next;
private:
    int m_weight;
    static int total_weight;
};
int  Goods::total_weight = 0;

void buy(Goods*& head,int weight)
{
    Goods* temp = new Goods(weight);
    if(head == NULL)
    {
        head = temp;
    }
    else
    {
        temp->m_next = head;
        head = temp;
    }
}
void sale(Goods*& head)
{
    if(head == NULL)
    {
        cout<<"没有可以删除的节点"<m_next;
        delete head;
        head = temp;
    }
}

void Goods_display(Goods* head)
{
    if(head == NULL)
        cout<<"没有货物"<get_mweight()<m_next;
    }
}

int main(void)
{
    int choise = 0;
    Goods* head = NULL;
    int weight = 0;
    while(1)
    {
        cout<<"1、进货"<>choise;
        switch (choise)
        {
        case 1:
            cout<<"请输入货物重量:"<>weight;
            buy(head,weight);
            break;
        case 2:
            sale(head);
            break;
        case 3:
            Goods_display(head);
            break;
        case 0:
            exit(0);
            break;
        default:
            break;
        }
    }
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396420.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号