所以我有一个链接列表,我传入一些参数,在这种情况下,它是一个框,参数是长度,宽度和高度。我想要的是能够打印输入的值。目前,我的指针是盒子的体积,长度,宽度和高度相乘。当节点落后于另一个节点时,我希望它显示为10x2x5 inserted at front of list
而不是100 inserted at front of list
。
插入节点的代码:
template <typename T>void LinkList<T>::insertNode(const T &length, const T &width, const T &height) {
LinkNode<T>*newPtr = getNewNode(length * width * height);
bool inserted = false;
if (isEmpty() || (newPtr->data < firstPtr->data)) {
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
cout << newPtr->data << " inserted at front of list\n";
}
else {
LinkNode<T>*currentPtr = firstPtr;
while (currentPtr->nextPtr && !inserted) {
if (newPtr->data < currentPtr->nextPtr->data) {
cout << newPtr->data << " inserted before " << currentPtr->nextPtr->data << endl;
newPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr = newPtr;
inserted = true;
}
else {
currentPtr = currentPtr->nextPtr;
}
}
if (!inserted) {
currentPtr->nextPtr = newPtr;
cout << newPtr->data << " inserted at end of list\n";
}
}
}
打印值代码:
template<typename T>void LinkList<T>::print()const {
if (isEmpty()) {
cout << "No boxes in list!\n";
}
else {
LinkNode<T>*currentPtr = firstPtr;
cout << "Your boxes are: ";
while (currentPtr != NULL) {
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
}
cout << "\n";
}
}
节点类:
//Template ListNode class definition.
#ifndef LINKNODE_H
#define LINKNODE_H
template <typename T> class LinkList;
template <typename T>class LinkNode{
friend class LinkNode <T>;
public:
LinkNode(const T &);
T getData()const;
T data;
LinkNode <T> *nextPtr;
};
template <typename T>LinkNode <T>::LinkNode(const T &info):data(info), nextPtr(NULL){
// Empty body
}
template <typename T>T LinkNode<T>::getData()const{
return data;
}
#endif