Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:35:30+00:00 2026-05-31T09:35:30+00:00

I have a class List: #ifndef UTIL_H #define UTIL_H #include <iostream> #include <exception> namespace

  • 0

I have a class List:

#ifndef UTIL_H
#define UTIL_H

#include <iostream>
#include <exception>

namespace Util
{

class IndexOutOfBounds : public std::exception
{
    virtual const char* what()
    {
        return "Index out of bounds";
    }
};

template<class T>
class Iterator;

template <class T>
class ListNode
{

    template<class R>
    friend class Iterator;
    template<class R>
    friend class List;

    public:

    ListNode()
    {
        init();
    }
    ListNode(T info)
    {
        init();
        this->info=info;
    }
    static void link(ListNode<T> first, ListNode<T> second)
    {
        first.next=&second;
        second.prev=&first;
    }
    template<class R>
    friend std::ostream& operator<< (std::ostream& out, const ListNode<R>& node);
    template<class R>
    friend std::istream& operator>> (std::istream& in, const ListNode<R>& node);

    private:

    T info;
    ListNode<T>* next;
    ListNode<T>* prev;
    void init()
    {
        next=prev=this;
    }
};

template<class T>
std::ostream& operator<< (std::ostream& out , const ListNode<T>& node)
{
    out << node.info;
    return out;
}

template<class T>
std::istream& operator>> (std::istream& in , const ListNode<T>& node)
{
    in >> node.info;
    return in;
}

template <class T>
class List
{
    friend class ListNode<T>;
    template <class R>
    friend class Iterator;
    private:

    unsigned int length;
    ListNode<T>* node;

    public:

    List()
    {
        node=new ListNode<T>();
        length=0;
    }
    unsigned int size() const
    {
        return length;
    }
    void insert(int i,T info) throw()
    {
        ListNode<T>* ptr=node,*next_ptr;
        try
        {
            if(i>(int)length || i<0)
                throw IndexOutOfBounds();
            for(int j=0;j<i;j++)
                ptr=ptr->next;
            next_ptr=ptr->next;
            ptr->next=new ListNode<T>(info);
            ptr->next->prev=ptr;
            ListNode<T>::link(*(ptr->next),*next_ptr);
            length++;
        }
        catch(IndexOutOfBounds& e)
        {
            throw e;
        }
    }
    void push_back(T info) throw()
    {
        try
        {
            insert(length,info);
        }
        catch(IndexOutOfBounds& e)
        {
            throw e;
        }
    }
    void push_front(T info) throw()
    {
        try
        {
            insert(0,info);
        }
        catch(IndexOutOfBounds& e)
        {
            throw e;
        }
    }
    void remove(int i) throw()
    {
        ListNode<T>* ptr=node,*next_ptr;
        try
        {
            if(i>=length || i<0)
                throw IndexOutOfBounds();
            for(int j=0;j<i;j++)
                ptr=ptr->next;
            next_ptr=ptr->next->next;
            delete ptr->next;
            ListNode<T>::link(*ptr,*next_ptr);
            length--;
        }
        catch(IndexOutOfBounds& e)
        {
            throw e;
        }
    }
    void pop_back() throw()
    {
        try
        {
            remove(length-1);
        }
        catch(IndexOutOfBounds& e)
        {
            throw e;
        }
    }
    void pop_front() throw()
    {
        try
        {
            remove(0);
        }
        catch(IndexOutOfBounds& e)
        {
            throw e;
        }
    }
    Iterator<T> begin() const
    {
        Iterator<T> result;
        result.ptr=node->next;
        return result;
    }
    Iterator<T> last() const
    {
        Iterator<T> result;
        result.ptr=node->prev;
        return result;
    }
    Iterator<T> end() const
    {
        Iterator<T> result;
        result.ptr=node;
        return result;
    }
    template <class R>
    friend std::ostream& operator<< (std::ostream& out , const List<R>& l) ;
    template <class R>
    friend std::istream& operator>> (std::istream& in , const List<R>& l);
    typedef Iterator<T> iterator;

};

template<class T>
std::ostream& operator<< (std::ostream& out ,const List<T>& l)
{
    int k=0;
    for(Iterator<T> i=l.begin();i!=l.end();++i)
        out << *i << "\t";
    return out;
}

template<class T>
std::istream& operator>> (std::istream& in , const List<T>& l)
{
    for(Iterator<T> i=l.begin();i!=l.end();i++)
        in >> *i;
    return in;
}

template <class T>
class Iterator
{
    friend class List<T>;
    friend class ListNode<T>;
    private:

    ListNode<T>* ptr;

    public:

    Iterator()
    {
        ptr=NULL;
    }
    Iterator(const Iterator<T>& i)
    {
        ptr=i.ptr;
    }
    Iterator<T>& operator= (Iterator<T> i)
    {
        ptr=i.ptr;
        return *this;
    }
    Iterator<T>& operator++ ()
    {
        ptr=ptr->next;
        return *this;
    }
    Iterator<T> operator++ (int)
    {
        Iterator<T> i=*this;
        ++*this;
        return i;
    }
    Iterator<T>& operator-- ()
    {
        ptr=ptr->prev;
        return *this;
    }
    Iterator<T> operator-- (int)
    {
        Iterator<T> i=*this;
        --*this;
        return i;
    }
    T& operator* ()
    {
        return ptr->info;
    }
    template<class R>
    friend bool operator!= (const Iterator<R>& i, const Iterator<R>& j);
    template<class R>
    friend bool operator== (const Iterator<R>& i, const Iterator<R>& j);

};

template <class T>
bool operator!= (const Iterator<T>& i, const Iterator<T>& j)
{
    return i.ptr!=j.ptr;
}

template <class T>
bool operator== (const Iterator<T>& i, const Iterator<T>& j)
{
    return i.ptr==j.ptr;
}

}

#endif

If in the main I try to print it:

int main(int argc, char** argv)
{
List<int> l;
for(int i=0;i<10;i++)
    l.push_back(i);
std::cout << l;
return 0;
}

I get the screen full of numbers flowing, I have to kill the process from terminal.
So it goes into ain infinite loop and I don’t understand why.
Maybe the Iterator operator != has some issue, I don’t understand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:35:31+00:00Added an answer on May 31, 2026 at 9:35 am

    You don’t terminate your linked list, in the last node the next pointer points back to the element itself.

    Either fix this in the ListNode constructor (preferred), or when inserting new element to the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#ifndef __TEST__ #define __TEST__ namespace std { template<typename T> class list; } template<typename T>
I have created MutexCondition class like this /*MutexCondtion.h file*/ #ifndef MUTEXCONDITION_H_ #define MUTEXCONDITION_H_ #include
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
I have a class that wraps List<> I have GetValue by index method: public
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have code that looks like this: template<class T> class list { public: class
I have a class public class PAUserAllowedTimesModel { public List<AllowedTime> Times { get; set;
I have a header file... #include <SFML\Graphics.hpp> #include <SFML\Graphics\Drawable.hpp> #include <SFML\System.hpp> #include <iostream> #ifndef
I have a .h file with this in it: #ifndef CS240_LINKED_LIST_H #define CS240_LINKED_LIST_H #include
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.