Goods类,用于存放每件货物重量,total_weight用来存储全部重量 ,通过三个函数实现,对链表的增删遍历
#includeusing 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; }



