我今天早些時候在這裏發佈了一個問題,但它非常大,所以現在提供了更多的細節。在鏈表中返回最大值
我想從鏈表返回最大值,但它只返回我輸入的第一個值?
結構:
struct Stock{
string itemName;
int itemStock; //100 crisps etc...
double individualItemCost;
double totalSockValue;
Stock* next;
};
如何調用該函數:
Stock * g = Largest(head, sizeOf);
cout << "Name: " << g->itemName << " Stock: " << g->itemStock << endl;
實際的功能:
Stock* Largest(Stock*& list, int size)
{
Stock *current;
current = (list+0);// start_ptr;
int largestValue = 0;
if (current!=NULL)
{
while (current->next!=NULL)
{
if (current->itemStock < largestValue)
{
largestValue = current->itemStock;
current = current->next;
}
else
{
current = current->next;
}
}
}
return current;
}
它返回的第一個值我輸入作爲反對 最大的價值,任何建議都會有幫助。
這是我正在讀入只是讓你知道鏈表:
while (i < sizeOf)
{
cout << "Please enter the students name: " << endl;
cin >> head->itemName;
cout << "Please enter the item stock: " << endl;
cin >> head->itemStock;
cout << "Please enter the item price: " << endl;
cin >> head->individualItemCost;
push(head,head->itemName, head->itemStock, head->individualItemCost); //Push back everything in head
i++;
}
謝謝!
右鍵確定歡呼,所以你的意思是像設置一個類型的股票變量current-> itemstock,然後返回? – user2799788
差不多。只有'current'沒有' - > itemstock'。 – datenwolf
啊!我想我沒有工作!謝謝你的幫助,一直困在這個很長一段時間哈哈。只是最後一個問題,我現在有內存泄漏,因爲我正在啓動這個股票* temp; \t temp = new Stock;是否有沒有使用動態變量呢? – user2799788