0

all.

I'm currently working on my singly linked list and hit a road block with my iterator class. This is so far what I have and I'll start with the node, then the singly linked list, then the iterator it self.

My nodes look like this currently. I apologize in advance for "syntax errors" shown in my code below. I can't properly indent with out compromising the code text in this website. I'm still getting use to how stackoverflow works.

#include <iostream>
#include <cstddef>
class SingleNode 
{
private:
     int key_;
     SingleNode * next_;
public:
      SingleNode(int key, SingleNode * next = NULL)
        : key_(key), next_(next)
     {
     }

     void set_key(int key)
     {
        key_ = key;
     }

     int get_key() const
     {
        return key_;
     }

     int & get_key()
     {
        return key_;
     }

     SingleNode * get_next() const
     {
        return next_;
     }

     SingleNode * set_next(SingleNode * next)
     {
        next_ = next;
     }
};

std::ostream & operator<<(std::ostream & cout, const SingleNode & node)
{
    cout << "< " << ' ' << &node << ' '
         << "key: " << node.get_key() << ' '
         << "next: " << node.get_next() << '>';
     return cout;
}

Now for my SinglyLinked list

#include <iostream>
#include <cstddef>
#include "SingleNode.h"

class SingleNode;

class SingleLinked
{
private:
    SingleNode * phead_;
public:
    SingleLinked()
       : phead_(NULL)
    {
    }

    class iterator
    {
        friend class SingleLinked;

    private:
        SingleNode * p;

        iterator(SingleNode * phead)
               : p(phead)
        {
        }

    public:
        iterator()
           : p(NULL)
        {
        }

        int & operator*()
        {
            return p->get_key();
        }

        int & operator++()
        {
            p = p->get_next();
        }
    };

    iterator begin() const
    {
        return iterator(phead_);
    }

    SingleNode * get_phead() const
    {
        return phead_;
    }

    SingleNode * insert_head(int key)
    {
         phead_ = new SingleNode(key, phead_);
    }

    SingleNode * insert_tail(int key)
    {
        SingleNode * p = new SingleNode(key, NULL);
        SingleNode * r = p;
        SingleNode * q = phead_;

        while (q->get_next() != NULL)
        {
            q = q->get_next();
        }
        q->set_next(r);
    }

    void delete_head()
    {
        SingleNode * p = phead_;
        phead_ = p->get_next();
    }

    void delete_tail()
    {
         SingleNode * p = phead_;
         while (p->get_next() != NULL)
         {
            p = p->get_next();
         }

         SingleNode * r = phead_;
         while (r->get_next() != p)
         {
            r = r->get_next();
         }

         r->set_next(NULL);
    }

    void print()
    {
        while (phead_ != NULL)
        {
           std::cout << *phead_ << std::endl;
           phead_ = phead_->get_next();
        }
    }
};

The idea is this, I want to use the SingleLinked class as a friend class to the iterator. That way, once the SingleLinked class is initialized in the main, it automatically constructs the iterator inside of it. This runs into some problems as soon as I start using the begins method.

This is the main so far.

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

int main()
{
     SingleLinked list;
     list.insert_head(1);
     list.insert_tail(2);

     typename SingleLinked::iterator p = list.begin();
     std::cout << *p << std::endl;
     return 0;
}

Once I try to cout that iterator p, it shows a segmentation fault in the console. What is going inside my SingleLinked class itself that is causing this?

haxpor
  • 2,431
  • 3
  • 27
  • 46
  • 2
    (A) The code you posted doesn't compile due to a missing `;`. (B) You should really turn on and pay attention to compiler warnings. You are missing return statements in several functions declared to return a value. That is the source of your crash. – Miles Budnek Nov 17 '19 at 05:40
  • @MilesBudnek I'd beg to differ, bud. – Kenneth McAusland Nov 17 '19 at 05:49
  • @KennethMcAusland, in what way do you differ? – R Sahu Nov 17 '19 at 05:50
  • @MilesBudnek If we are talking about just the List itself, it compiles on my g++ compiler just fine. Don't know what compiler you're using but outside of that missing ; thing you were referring to.... and I hope to god you didn't just copy and past this all the way through... it compiles. – Kenneth McAusland Nov 17 '19 at 05:52
  • @MilesBudnek The only thing that causes a crash is that segmentation faul from the iterator class itself though. Don't know what's going on with it. – Kenneth McAusland Nov 17 '19 at 05:52
  • @KennethMcAusland, use the compiler flag `-Wall`. You might notice a few more things. – R Sahu Nov 17 '19 at 05:53
  • @RSahu Cin-wall? I heard about this, exactly how in the main would I use that? Like say std::cin-Wall >> *p ?? – Kenneth McAusland Nov 17 '19 at 05:55
  • @KennethMcAusland, you would use that as a compiler flag, as in `g++ -std=c++11 -Wall` or `g++ -std=c++14 -Wall` – R Sahu Nov 17 '19 at 05:58
  • 2
    Here are the [errors and warnings](http://coliru.stacked-crooked.com/a/1b81f11c81e60487) I observe when compiling the code you posted here. After fixing the missing `;` and all of the missing return values, the [program compiles and runs](http://coliru.stacked-crooked.com/a/affca937dceefb90) without issue. – Miles Budnek Nov 17 '19 at 06:04
  • @MilesBudnek The missing `;` was edited in 16 minutes ago. – JaMiT Nov 17 '19 at 06:05
  • @JaMiT Ah, didn't notice the edit. The rest is still valid though. – Miles Budnek Nov 17 '19 at 06:06
  • @JaMiT That will depend on your compiler and compiler options. The program's behavior is undefined, and at least for GCC 9.2 using -O2, it [crashes](http://coliru.stacked-crooked.com/a/990ae1e661116d31) with the missing return values. – Miles Budnek Nov 17 '19 at 06:09
  • @MilesBudnek Hmm... looks like I did indeed have optimizations off. So the crash comes when warnings are off and optimizations are on. – JaMiT Nov 17 '19 at 06:14
  • I really don't have any words on this one guys. I just re did this whole thing and for some reason... without doing any editing... it works. Shrugs. – Kenneth McAusland Nov 17 '19 at 06:22
  • thanks for the help anyway guys. I really do appreciate it. – Kenneth McAusland Nov 17 '19 at 06:22
  • 1
    Read [this](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – n. m. could be an AI Nov 17 '19 at 06:31

0 Answers0