关于链表类

来源:百度知道 编辑:UC知道 时间:2024/07/03 02:24:49
这是我写的作业
用循环双向链表实现。
但是 卖完了东西(应该变成了空结点) 在买或卖就会失败了
为什么呢?

#include<iostream>
using namespace std;

class Goods
{ public:
Goods(int w)
{ weight = w ;
total_weight+=w;
}
~Goods()
{ total_weight-=weight; }
int Weight() { return weight; }
static int TotalWeight()
{ return total_weight; }
Goods *next;
Goods *prior;
private:
int weight;
static total_weight;
};

int Goods::total_weight = 0;

void purchase(Goods *&p,int w); //购进货物在表尾插入结点
void sale(Goods *&p); //售出货物在表头删除结点

int main()
{ Goods *p=NULL;
int w;
int choice;
do
{ cout<<"Please choose:\n";
cout<<"Key in 1 is purchase,\nKey in 2 is sale,\nKey in 0 is over.\n";
cin>>choice;<

看得出你功力不错,但我感觉这程序不像是你写的。你语法很熟练,但怎么这么小的错误也看不出?
======================================================================
#include<iostream>
using namespace std;
class Goods
{
private:
int weight;
static int total_weight;
public:
Goods(int w)
{
weight = w ;
total_weight+=w;
}
~Goods()
{
total_weight-=weight;
}
int Weight()
{
return weight;
}
static int TotalWeight()
{
return total_weight;
}
Goods *next;
Goods *prior;
};
int Goods::total_weight = 0;

void purchase(Goods *&p,int w); //购进货物在表尾插入结点
void sale(Goods *&p); //售出货物在表头删除结点

int main()
{
Goods *p=NULL;
int w;
int choice;
do
{
cout<<"Please choose:\n";
cout<<"Key in 1 is purchase,\nKey in 2 is sale,\nKey in 0 is over.\n&