RandomPleb
RandomPleb

Reputation: 424

Implementing Push function in Queue

I'm not entirely sure where I'm going wrong here but I keep getting weird values for Push. I don't know if my error is in Push itself or in the Display, but I assume it is in Push. Note that Push here is supposed to be similar to "std::deque::push_back".

Here is the related implementation code:

fsu::Queue <char> q;
q.Push('a');
q.Display(std::cout, '\0');

Here is what I get:

0x100100a20
0x100100a20
0x0

Related definitions:

void      Push    (const T& t); // push t onto queue
void      Display (std::ostream& os, char ofc = '\0') const; // output contents
                                    //through os
queue             ();              // default constructor
Queue             (const Queue&);  // copy constructor
~Queue            ();              // destructor
Queue& operator = (const Queue&);  // assignment operator

private:
    class Link
    {
        Link ( const T& t );  // 1-parameter constructor
        T      element_;
        Link * nextLink_;
        friend class Queue<T>;
    };
Link * firstLink_;
Link * lastLink_;

void         Copy   (const Queue<T>& q);
static Link* NewLink(const T& t);

And here is what I have to for the implementation:

template < typename T >
fsu::Queue<T>::Queue () : firstLink_(0), lastLink_(0)              // default constructor
{
  //empty
}

template < typename T >
fsu::Queue<T>::Queue (const Queue& q) : firstLink_(0), lastLink_(0)
{
  Copy(q);
}

template < typename T >
fsu::Queue<T>& fsu::Queue<T>::operator = (const Queue& q) // assignment operator
{
  if (this != &q)
  {
    Clear();
    Copy(q);
  }
  return *this;
}

template < typename T >
fsu::Queue<T>::Link::Link ( const T& t ) : element_(t), nextLink_(0)  // 1-parameter     constructor
{
};

template < typename T >
typename fsu::Queue<T>::Link* fsu::Queue<T>::NewLink (const T& t)
{
  Link * newLink = new(std::nothrow) Link (t);
  if (0 == newLink)
  {
    // exception handler
    std::cerr << "** Queue error: memory allocation failure\n";
    return 0;
  }
  return newLink;
}

template < typename T >
void fsu::Queue<T>::Copy (const Queue<T>& q)
{
  if (firstLink_ != 0)
  {
    std::cerr << "** Error: Queue::Copy called by non-empty object\n";
    //  exit(EXIT_FAILURE);
  }
  if (q.firstLink_ == 0)
    return;
  Link* qlink = q.firstLink_;
  while (qlink != 0)
  {
    Push(qlink -> element_);
    qlink = qlink -> nextLink_;
  }
  }

template < typename T >
void fsu::Queue<T>::Push (const T& t) // push t onto queue
{
if (Empty())
{
    Link * newLink = new Link(t);
    newLink -> nextLink_ = NULL;
    firstLink_ = newLink;
    lastLink_ = newLink;
}
else
{
    //to be implemented
}
}

template < typename T >
void fsu::Queue<T>::Display (std::ostream& os, char ofc) const // output contents
// through os
{
  os << firstLink_ << std::endl;
  os << lastLink_ << std::endl;
  Link * currLink = firstLink_;
  while (currLink)
  {
    currLink = currLink -> nextLink_;
    os << currLink << std::endl;
  }
 }

Upvotes: 1

Views: 1526

Answers (1)

JohnB
JohnB

Reputation: 13713

  os << firstLink_ << std::endl;
  os << lastLink_ << std::endl;

Prints the addresses pointed to (i.e. the value the variable carries) rather than the item pointed to.

You must dereference the pointers! And then you get a Link structure, and from that you have to select the _element member, which you want to print:

  os << (*firstLink_)._element << std::endl;
  os << (*lastLink_).element << std::endl;

shorter and more common:

  os << firstLink_->_element << std::endl;
  os << lastLink_->_element << std::endl;

The need to explicitly dereference is a difference to, e.g. Java and C#, where references are "hidden" from the programmer and dealt with by the language. In C and C++, a pointer variable simply holds a memory address. You can manipulate memory addresses directly (google for "pointer arithmetic"); hence it is necessary to distinguish the pointer value (i.e. the address) from the value pointed to, which is located at this address.

Hope this is not too confusing.

Upvotes: 1

Related Questions