开发者

Why does my linked list code result in link errors?

开发者 https://www.devze.com 2023-03-09 21:44 出处:网络
I\'m new to linklist, and i\'m having a tough time with it. I\'m trying to display some values that i\'ve appended to the nodes, But i keep getting linkerror messages. Here is what I have so far.

I'm new to linklist, and i'm having a tough time with it. I'm trying to display some values that i've appended to the nodes, But i keep getting linkerror messages. Here is what I have so far.

LinkList.h
-


    #ifndef LINKLIST_H
    #define LINKLIST_H

    class LinkList
    {
    private:
        struct ListNode
        {
            int value;
            ListNode *next;
        };

        ListNode *head;

    public:
        LinkList();
        void insertNode(int);

        void deleteNode(int);
        void appendNode(int);
        void display() const;

        //~LinkList();
    };
    #endif


Impl.cpp
-

    #include <iostream>
    #include "LinkList.h"
    using namespace std;

    void LinkList::appendNode(int num)
    {
        ListNode * newNode;
        ListNode * nodePtr;

        newNode = new ListNode;
        newNode->value = num;
        newNode->next = NULL;

        if(!head)
        {
            head = newNode;
            head->value = num;
            head->next=NULL;
        }
        else
        {
            nodePtr = head;

            while(nodePtr->next!=NULL)
                nodePtr = nodePtr->next;
            newNode = new ListNode;
            newNode->value = num;
            newNode->next = NULL;

            nodePtr->next = newNode;
        }
    }

    void LinkList::display() const
    {
        ListNode *nodePtr;
        nodePtr = head;

        while (nodePtr != NULL)
        {
            cout << nodePtr->value << endl;

            nodePtr = nodePtr->next;
        }
    }

LinkList::LinkList() { head = NULL; }

main.cpp
-


    #include <iostream>
    #include "LinkList.h"

    using namespace std;

    int main()
    {
        LinkList min开发者_如何学Goe;

        mine.appendNode(6);
        mine.appendNode(9);
        mine.appendNode(11);

        mine.display();

        return 0;
    }

I fixed some of the initial problems but the program just crashes when it runs and i'm not sure why

I'm not sure what the problems is, any help would be greatly appreciated.


You declared a LinkList constructor, and a destructor, but you did not define them:

LinkList::LinkList() : head(NULL)
{
}

LinkList::~LinkList()
{
    // delete your memory here...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消