简体   繁体   English

链表中的运算符重载

[英]Operator overloading in Linked List

I am moving here from my other post. 我将从其他职位搬到这里。 But this time I am able to obtain some type of output. 但是这次我可以获取某种类型的输出。 But I cannot seem to iterate through my nodes and get them to print out individually as how it should look on the output. 但是我似乎无法遍历我的节点并让它们分别打印出来,就像它在输出中的外观一样。 Here is what I have so far and also a screenshot of what the program should output. 这是我到目前为止的内容,也是该程序应输出的屏幕截图。

在此处输入图片说明

LList.h LList.h

#ifndef LList_h
#define LList_h

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

class LList
{
    public:
        LList(void);            //constructor
        LList(const LList &);   //copy constructor
        ~LList();               //destructor
        LList *next;            //points to next node
        void push_back(const string &str);
        void push_front(const string &str);
        friend ostream& operator<<(ostream& out, const LList& llist);
        LList &operator=(const LList &l);       

    private:
        Node *_head;
        Node *_tail;
        LList *front;       //points to front of the list
};

inline LList::LList(void)
{
    cerr << "head = tail = 0 at 0024f8d0\n";

    _head = 0;
    _tail = 0;
    front = 0;
}

inline void LList::push_back(const string &str)
{
    Node *p = new Node(str);
    if (_tail == 0)
    {
        _head = _tail = p;
    }
    else
    {
        _tail ->next(p);
        _tail = p;
    }

    if (_head == 0)
    {
        _head = _tail = p;
    }
    else
    {
        _head ->next(p);
        _head = p;
    }
}

inline void LList::push_front(const string &str)
{
    Node *p = new Node(str);
    if (_tail == 0)
    {
        _head = _tail = p;
    }
    else
    {
        _tail ->next(p);
        _tail = p;
    }

    if (_head == 0)
    {
        _head = _tail = p;
    }
    else
    {
        _head ->next(p);
        _head = p;
    }
}

LList & LList::operator=(const LList &l)
{
    _head = l._head;
    _tail = l._tail;
    front = l.front;
    return *this;
}

inline LList::~LList()
{
}


#endif

maind.cpp maind.cpp

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

using namespace std;

ostream& operator<<(ostream& out, const LList& llist);

int main( )
{
    LList a;

    a.push_back(  "30" );
    a.push_front( "20" );
    a.push_back(  "40" );
    a.push_front( "10" );
    a.push_back(  "50" );

    cout << "list a:\n" << a << '\n';
    return 0;
}

ostream &operator <<( ostream &out, const LList & llist )
{
    for( LList *p = llist.front; p != 0; p = p -> next )
        out << p -> next;

    return out;
}
out << p -> next;

This line will skip your first element and cause undefined behavior (possibly segfault) on your last element. 该行将跳过您的第一个元素,并在最后一个元素上导致未定义的行为(可能是段错误)。 This should be out<<p . 这应该是out<<p

Your operator<< will print nothing because LList::front is never assigned to. 您的operator<<将不打印任何内容,因为从未分配LList::front It's always null. 它始终为空。

Your push algorithms make no sense. 您的推送算法没有任何意义。 To push something at the back of the list, you only want head to be modified if the list is empty, but you have: 要将某些内容推到列表的后面,您只希望在列表为空的情况下修改head ,但是您需要:

if (_head == 0)
{
    _head = _tail = p;
}
else
{
    _head ->next(p);
    _head = p;
}

Why are you setting _head to p if the list had entries in it already? 如果列表中已经包含条目,为什么将_head设置为p Your code has a number of similar bugs -- the logic just isn't right. 您的代码有许多类似的错误-逻辑不正确。

The end should probably just be: 最后应该只是:

if (_head == 0)
    _head = p;

If there's already a node at the head, adding an entry to the back doesn't affect head at all. 如果头部已经有一个节点,则在背面添加一个条目根本不会影响头部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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