1

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

//////////////
//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?)

我不明白。它不能在列表中向后迭代,可以吗?单链表不是这样工作的。我唯一能想到的是链接本身是乱序的,但我没有看到我使用的方法有任何问题。你怎么看?

4

1 回答 1

3

通过快速浏览代码,您不会在 getListPtr 中添加新节点时移动 newPoint 指针。因此,每个新节点都直接添加到 head 之后,因为 newPoint 被初始化为 head 并保留在那里。

于 2013-04-22T04:12:57.263 回答