从链表中返回一些东西

时间:2014-01-07 03:32:49

标签: c++ struct linked-list

有谁可以告诉我为什么我的主程序打印出9460301而不是350? 我只是想将一个结构作为单个项插入到链表中。该结构具有属性x和y。然后我希望在链表中打印出结构的x属性。我写了一个庞大的程序,我试着在这篇文章中删除它,只是为了查看我出现的这个新问题。

我的chunk结构和Linkedlist类如下:

struct chunk{
    int x;
    int y;
};

template <class T>
class linkedList
{
public:
    class node
    {
    public:
        ///node class attributes
        T mPayload;
        node* mNext;
        ///constructor
        node(T toucan):mPayload(toucan),mNext(NULL)
        {}
        ///destructor
        ~node()
        {
            ///cascading delete
            if(mNext)
                delete mNext;
        }
        ///node class methods
    };

    ///linkedList class attributes
    node* mStart;
    ///constructor
    linkedList():mStart(NULL)
    {}
    ///destructor
    ~linkedList()
    {
        ///initializes the cascading delete.
        if(mStart)
            delete mStart;
    }
    ///linkedList class methods
    T mReturnT(int indx)
    {
        if(!mStart)
        {
            T emptyList;
            return emptyList;
        }
        else
        {
            node* cur;
            for(int i = 0; i<indx+1; i++)
            {
                if(cur->mNext == NULL)
                {
                    cout << "Indx out of range. Deleting last item." << endl;
                    break;
                }
                cur = cur->mNext;
            }
            return cur->mPayload;
        }
    }

    void mInsertHelper(node* blah, T data)
    {
        if(blah->mNext != NULL)
            mInsertHelper(blah->mNext, data);
        else
        {
            blah->mNext = new node(data);
            blah->mNext->mNext = NULL;
        }
    }

    void mInsert(T data)
    {
        if(mStart == NULL)
        {
            mStart = new node(data);
            //mStart->mPayload = data;
        }
        else
            mInsertHelper(mStart, data);
    }

    T mPop()
    {
        ///Removes the last item in the list,
        ///and returns it.
        if(!mStart)
            return NULL;
        else
        {
            node* cur = mStart;
            while(cur->mNext)
            {
                cur = cur->mNext;
            }
            T var = cur->mPayload;
            delete cur;
            return var;
        }
    }

    int mSize()
    {
        if(!mStart)
            return 0;
        else
        {
            node* cur = mStart;
            int counter = 1;
            while(cur->mNext)
            {
                cur = cur->mNext;
                counter++;
            }
            delete cur;
            return counter;
        }
    }

};

我的main.cpp:

int main()
{
    chunk head;
    head.x = 350;
    head.y = 600;
    linkedList<chunk> p1Snake;
    p1Snake.mInsert(head);

    cout<<p1Snake.mReturnT(0).x<<endl;
}

1 个答案:

答案 0 :(得分:2)

在迭代之前,你永远不会初始化cur

node* cur;                                  // <-- UNINITIALISED!
for(int i = 0; i<indx+1; i++)
{
    if(cur->mNext == NULL)
    {
        cout << "Indx out of range. Deleting last item." << endl;
        break;
    }
    cur = cur->mNext;
}
return cur->mPayload;

第一行应该是:

node* cur = mStart;

我认为你应该在那个循环中使用indx而不是indx+1 ......除非你使用的是 dummy-head 方案,否则你就不会这样做。

用于检测越界的循环内部的逻辑也有点错误。如何改造整个事情:

node* cur = mStart;
while( cur && indx > 0 ) {
    cur = cur->mNext;
    indx--;
}

if( !cur ) {
    cout << "Indx out of range." << endl;
    return T();
}

return cur->mPayload;