简体   繁体   中英

Can't assign to object in linked list

Head and tail are getting populated, and print out the values, but nodePtr stays empty for some reason. When I debug in VS2015, head and tail number is getting populated, while field this stays empty

Here's Linked_List

#ifndef _LINKED_LIST_
#define _LINKED_LIST_

#include <iostream>

class LinkedList
{
public:

    struct Node
    {
        int number;
        Node * next;
        Node() : number(NULL), next(NULL) {};
        Node(int number_, Node * next_ = NULL)
        {
            number = number_;
            next = next_;
        }
    }*head, *tail, *nodePtr;


LinkedList();
~LinkedList();

void add(int num);

friend std::ostream& operator<<(std::ostream& out, LinkedList& list);

private:
    int size;
};

#endif // _LINKED_LIST_

Implementation file

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

LinkedList::LinkedList() : head(NULL), tail(NULL), nodePtr(NULL) 
{
    nodePtr = new Node();
}

LinkedList::~LinkedList()
{
    Node * curr, *temp;
    curr = head;
    temp = head;

    while (curr != NULL)
    {
        curr = curr->next;
        delete temp;
        temp = curr;
    }
}


void LinkedList::add(int num)
{
    Node * newNode = new Node();
    newNode->number = num;
    cout << newNode->number;
    if (head == NULL)
    {
        head = newNode;
        tail = newNode;
        size++;
    }
    else
    {
        tail->next = newNode;
        newNode->next = NULL;
        tail = newNode;
        size++;
    }
    //cout << nodePtr->number; //empty, or some random
    //just some tests
    cout << head->number;
    if (head->next != NULL)
    {
        cout << head->next->number;
    }
    cout << tail->number;
    cout << endl;
}

std::ostream & operator<<(std::ostream & out, LinkedList & list)
{
    out << list.nodePtr->number << endl;
    return out;
}

Main.cpp

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

using namespace std;

int main()
{
    int num;
    LinkedList list;

    list.add(1);
    list.add(2);
    list.add(3);
    cout << list;


    cout << "Press 1: ";
    cin >> num;
    return 0;
}

You're missing a fundamental concept here. nodePtr is not some magical node that knows about all your other nodes, or knows about linked lists, or can be used to print all their numbers.

When you do this:

out << list.nodePtr->number << endl;

All you are doing is outputting the value that you initialized when you allocated a new Node and stored a pointer in nodePtr :

nodePtr = new Node();

That called the default constructor for Node which set nodePtr->number to zero. (side-note, you initialized it to NULL , not 0 -- you should not mix integer types with pointer types, so change it to initialize the value to 0).

Its value stays 0 because you never modify it. And nodePtr always points at that single node because you never modified nodePtr .

What you're actually wanting to do is print out your list. Let me suggest the normal way to do this, by starting at head and following the node linkages:

std::ostream & operator<<(std::ostream & out, const LinkedList & list)
{
    for( Node *node = list.head; node != nullptr; node = node->next )
    {
        out << node->number << std::endl;
    }
    return out;
}

And finally, I suggest you remove nodePtr from your class completely.

您仅在构造函数中使用nodePtr,而从不更改其值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM