问题描述
using namespace std;
struct Package_Node
{
int bar_code;
float package_weight;
struct Package_Node *next_packaged;
};
struct Key_Node
{
int key;
struct Package_Node *next_package;
};
Key_Node *keyMain=0;
int weightTemp=0,barcodeTemp=0,keyTemp=0,N=0,X=0,max_value=0,optionChosen=0,optionChosenTwo=0;
float tempWeight;
int main()
{
srand(time(NULL));
do
{
cout << "1 - Enter Number of keys and the Max Value\n2 - Enter Number of Packages (N)\n";
cout << "3 - Create Array of Keys\n4 - Organize Packages\n5 - Table Stats\n6 - Clean Table\n7 - Exit\n\n";
cin >> optionChosen;
switch(optionChosen)
{
case 1://entering keys and max values
{
cout << "\nEnter Number of Keys: ";
cin >>X;
cout <<"\nEnter Max Value of the Barcode: ";
cin >>max_value;
break;
}
case 2://enter number of packages.
{
cout << "\nEnter Number of Packages: ";
cin >> N;
break;
}
case 3://create array of keys.
{
//Key_Node *keyMain = new Key_Node[X];
keyMain= new Key_Node[X];
for(int i=0;i<X;i++)
{
keyMain[i].key=i;
cout << keyMain[i].key<<endl;
}
break;
}
case 4:
{
for(int i=0;i<N;i++)
{
barcodeTemp = rand() % max_value + 1;
keyTemp = barcodeTemp % X;
tempWeight = rand() % 500 + 1;
keyMain[keyTemp] = new Package_Node;
Package_Node->bar_code=barcodeTemp;
Package_Node->package_weight=tempWeight;
Package_Node->next_packaged=NULL;
}
for(int i=0;i<N;i++)
{
cout << totalPackages[i].bar_code <<endl;
cout << totalPackages[i].package_weight << endl;
}
cout << keyTemp;
break;
}
case 5:
{
break;
}
case 6:
{
break;
}
default:
{
break;
}
}
}while(optionChosen!=7);
}
我在这里感到困惑。 keyMain
是一个静态数组。 keyMain [1]
, keyMain [2]
等将指向使用 Package_Node
创建的链接列表。将添加 new package_node
的 keyMain
的索引由 keyTemp
。但是它似乎是一个很深的哭。看起来像我写的代码不工作。我在这里做错了什么?有任何想法吗?
I am sort of confused here. keyMain
is a static array.keyMain[1]
, keyMain[2]
etc. will point to a linked list that is made using Package_Node
. The index of keyMain
where the new package_node
will be added is determined by keyTemp
. But it seems like thats a far cry. Seems like the code I have written is not working. What am I doing wrong here? Any ideas? Please do not use vectors as this is for a class project and we wont talk about vectors at all.
推荐答案
此部分不会使用向量作为类项目,正确
This section does not look right
keyMain[keyTemp] = new Package_Node;
Package_Node->bar_code=barcodeTemp;
Package_Node->package_weight=tempWeight;
Package_Node->next_packaged=NULL;
应该是
keyMain[keyTemp].next_package = new Package_Node;
keyMain[keyTemp].next_package->bar_code=barcodeTemp;
keyMain[keyTemp].next_package->package_weight=tempWeight;
keyMain[keyTemp].next_package->next_packaged=NULL;
也是您计算keyTemp的方式
Also the way you are calculating keyTemp
barcodeTemp = rand() % max_value + 1;
keyTemp = barcodeTemp % X;
似乎不是取决于您输入的max_value,它可能大于X,数组的大小。
does not seem right as depending on what you enter in for the max_value it may be greater than X, the size of you array.
这篇关于在c ++中如何在静态数组的末尾添加一个链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!