Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表插入节点-如何引用参数_C++_List_Pointers_Hyperlink - Fatal编程技术网

C++ 链表插入节点-如何引用参数

C++ 链表插入节点-如何引用参数,c++,list,pointers,hyperlink,C++,List,Pointers,Hyperlink,所以我有一个链表,我传入一些参数,在这个例子中,它是一个框,参数是长度、宽度和高度。我想要的是能够打印出输入的值。目前,我的指针是长方体的体积,即长度、宽度和高度的乘积。当一个节点位于另一个节点的后面或前面时,我希望它显示例如在列表前面插入的10x2x5,而不是在列表前面插入的100 插入节点的代码: template <typename T>void LinkList<T>::insertNode(const T &length, const T &wi

所以我有一个链表,我传入一些参数,在这个例子中,它是一个框,参数是长度、宽度和高度。我想要的是能够打印出输入的值。目前,我的指针是长方体的体积,即长度、宽度和高度的乘积。当一个节点位于另一个节点的后面或前面时,我希望它显示例如在列表前面插入的
10x2x5
,而不是在列表前面插入的
100

插入节点的代码:

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";
    }
}
}
模板无效链接列表::插入节点(常量T和长度、常量T和宽度、常量T和高度){
LinkNode*newPtr=getNewNode(长*宽*高);
bool-inserted=false;
if(isEmpty()| |(newPtr->datadata)){
newPtr->nextPtr=firstPtr;
firstPtr=newPtr;
cout data nextPtr&!已插入){
如果(新建PTR->datanextPtr->data){
cout data data NEXTPTTR=当前PTR->NEXTPTTR;
currentPtr->nextPtr=newPtr;
插入=真;
}
否则{
currentPtr=currentPtr->nextPtr;
}
}
如果(!插入){
currentPtr->nextPtr=newPtr;

cout数据您将需要存储三个值(长度、宽度和高度)在您的
节点中
作为单独的变量。但是您没有显示您的
节点
类。对不起,我现在将添加我的节点类!因此,我将使用
t length、t width、t height
,而不是
t data
,然后在我的打印方法中,我将使用这些变量?我建议放置
length、width
height
在您的
T
结构内部。长度、宽度和高度是节点的成员,而不是单独的节点。这些更改是否会出现在我的节点类中?我对链接列表一无所知,并且发现这个概念有些混乱。当您说在我的
T
结构内部时,这是否意味着我应该将
长度、宽度和高度添加到类
LinkNode
在公共成员所在的位置下?或者这些成员是否属于结构
T
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