测试一个简单的链表,但它坚持向后迭代



我做了一个简单的链表,看看我是否完全理解它们,我目前正试图验证所有的数据点都在适当的地方。他们不是,但我不知道为什么。我认为问题出在两个方面之一。这要么是列表的构造错误,要么是我试图输出数据的方式有问题。

//////////////
//linkedlist.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
namespace LinkedListMagno
{
    class LinkList
    {
    public:
        LinkList(int theData, LinkList* thePoint) : data(theData), point(thePoint){};
        int getData() {return data;}
        void setData(int theData) {data = theData;}
        LinkList* getLink() {return point;}
        void setLink(LinkList* thePoint) {point = thePoint;}
    private:
        int data;
        LinkList* point;
    };
}//LinkedListMagno
#endif

    ////////////
    //source.cpp
    #include<iostream>
    #include "linkedlist.h"
    using LinkedListMagno::LinkList;
    using std::cin;
    using std::cout;
    using std::endl;
    LinkList* getListPtr(int data[], int lenOfData);
    //precondition: passed an array of integers and the length of the same array
    //postcondition: returns a pointer to a linked list containing the integers from data[] in the given order

    int main()
    {
        LinkList *head, *point;
        int data[] = {0,1,2,3,4,5,6,7,8,9};//these numbers will go into the list in ascending order
        int len = 10;
        head = getListPtr(data, len);
        point = head;
        for(int i=0; i<len; i++)//Pretty sure the problem is here. How on earth is it iterating backwards through the list?
        {
            cout << point->getData();
            point = point->getLink();
        }
        system("PAUSE");
        return 0;
    }
    LinkList* getListPtr(int data[], int lenOfData)
    {
        LinkList *head, *newPoint; //two pointers. One for the head, the other to add nodes
        for(int i=0; i<lenOfData; i++)
        {
            if(i == 0)
            {
                head = new LinkList(data[i], NULL);//create new linklist object using the first data point
                cout << "New Node: " << data[i] << endl;
                newPoint = head;//for the first node, set newPoint equal to the head
            }
            else if(i>0 && i<lenOfData)
            {
                newPoint->setLink(new LinkList(data[i], newPoint->getLink()));
                cout << "New Node: " << data[i] << endl;
                //newPoint = newPoint->getLink(); Derp. Forgot to add this line.
                //for each item in data[], add a new node and move newPoint* to the new location
            }
        }
        return head;
    }
预期输出:

New Node: 0
New Node: 1
New Node: 2
New Node: 3
New Node: 4
New Node: 5
New Node: 6
New Node: 7
New Node: 8
New Node: 9
0123456789Press any key to continue . . .
实际输出:

New Node: 0
New Node: 1
New Node: 2
New Node: 3
New Node: 4
New Node: 5
New Node: 6
New Node: 7
New Node: 8
New Node: 9
0987654321Press any key to continue . . . (WTF?)

我不明白。它不可能在列表中向后迭代,对吧?单链表不是这样工作的。我能想到的唯一一件事是链接本身是无序的,但我没有看到我使用的方法有任何问题。你觉得呢?

通过快速扫描代码可以发现,在getListPtr中添加新节点时没有移动newPoint指针。因此,每个新节点都直接添加到head之后,因为newPoint初始化为head并保留在那里。

相关内容

  • 没有找到相关文章

最新更新