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 6006245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:30:41+00:00 2026-05-23T01:30:41+00:00

So I am unsure of how to make this insert_after method. Yes, this is

  • 0

So I am unsure of how to make this insert_after method. Yes, this is a hw assignment but I have been looking at this trying to figure how to implement this for hours. Also, I can’t get my error checking to work correctly as it will completely abort the program.

Help a C++ noob out?

#include <iostream>
#include <cassert>
#include <stdexcept>
using namespace std;

template<class T> class mylist;

template<class T>
ostream& operator<<(ostream& out, const mylist<T>&l);

template <typename T>
class mylist {
public:
    struct node {
        T data;
        node* next_ptr;
        node(const T&d, node* n):data(d),next_ptr(n){}
    };
    node* head_ptr;
    friend ostream& operator<<  <>(ostream& out, const mylist<T>&l);

    class iterator {
        node* ptr;
    public:
        iterator(node*p):ptr(p){}
        iterator next(){return ptr->next_ptr;}
        T& operator*(){return ptr->data;}
        bool operator!=(const iterator& other){return ptr!=other.ptr;}
        iterator operator++(){ptr=ptr->next_ptr; return *this;}
    };
public:

    mylist():head_ptr(0) {}

    iterator begin(){return iterator(head_ptr);}
    iterator end(){return iterator(0);} 

    // returns a reference to the data in the ith node in the list
    // raise an out_of_bounds exception if not enough elements
    //****can't get error check to work here****/
    T& at(unsigned i)
    {   
        try{
        cout << endl;       
        unsigned j = 0;
        //node* node_pointer;
        iterator iter = begin();        
        while(j < i && iter.next() != NULL) 
        {

            ++iter;         
            j++;
            /*if(!(iter.next() != NULL))
            {   throw out_of_range("out of bounds");
                }*/         
        }                       
        cout << "leaving now" << endl;      
        return *iter;}
        catch(out_of_range& oor){cerr << "out of range" << oor.what() << endl;}

    }

    // same as at
    //****can't get error check to work here****/
    T& operator[](unsigned i)
    {
        try{
        cout << endl;       
        unsigned j = 0;
        //node* node_pointer;
        iterator iter = begin();        
        while(j < i && iter.next() != NULL) 
        {       
            ++iter;         
            j++;
            /*if(!(iter.next() != NULL))
            {   throw out_of_range("out of bounds");
                }*/             
        }                       
        cout << "leaving now" << endl;      
        return *iter;}
            catch(out_of_range& oor){cerr << "out of range" << oor.what() << endl;}
    }   

    // insert after the node 'pointed' by place
    void insert_after(const T&data, const iterator &place) 
    {       
        if(empty())
            push_front(data);




    }

    // removes the first node, returns its data
    T pop_front(){
        return 0;
    }

    // removes the node after the node 'pointed' by place
    void remove_after(const iterator &place) {

    }

    // destructor, needs to delete all nodes in the list
    ~mylist() {

    }

    // you're done here

    // insert at beginning of list
    void push_front(const T& data) {
        head_ptr=new node(data,head_ptr);
    }

    bool empty() { return head_ptr==0;}

    void push_back(const T&data) {
        if(empty())
            push_front(data);

        node* last_ptr=head_ptr;
        while(last_ptr->next_ptr != 0)
            last_ptr=last_ptr->next_ptr;
        // pointing to last node on the list
        last_ptr->next_ptr=new node(data,0);
    }

    unsigned length() {
        unsigned l=0;
        node*current_ptr=head_ptr;
        while(current_ptr!=0) {
            l++;
            current_ptr=current_ptr->next_ptr;
        }
        return l;
    }

    void print_all(void) {
        cout << "mylist{";
        for(node*current_ptr=head_ptr;  
                current_ptr!=0; 
                current_ptr=current_ptr->next_ptr){
            cout << current_ptr->data << " ";
        }
        cout <<"}";
    }
};


template<typename T>
ostream& operator<<(ostream& out, const mylist<T>&l) {
    out << "mylist{";

    typename mylist<T>::node* current_ptr;

    for(current_ptr=l.head_ptr; current_ptr!=0;     
                    current_ptr=current_ptr->next_ptr) {
        out << current_ptr -> data << " ";
    } 
    out <<"}";
    return out;
}


int main(void)
{
    mylist<int>::node h(4,0);
    mylist<int> l;
    mylist<int> z;
    cout << l.length() << endl; 
    l.push_front(6);
    l.push_front(7);
    cout << l.length() << endl;
    l.push_back(10);        
    l.print_all();
    unsigned int i = 5;     

    cout << "data at i = "<< i <<" is: " << l.at(i) << endl;
    cout << "data at l[" << i <<"] is " << l[i] << endl;
    z.insert_after(23,iter);    
    z.push_front(18);
    z.push_front(x.data);
    z.print_all();
    cout << endl << "goodbye" << endl;

    /*for(mylist<int>::iterator curr=l.begin(); curr!=l.end(); ++curr) {
        cout << *curr << endl;
    }*/ 

}
  • 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-23T01:30:42+00:00Added an answer on May 23, 2026 at 1:30 am

    Since this is homework I will not give example code, but: you must have a way for mylist to access the node* in your iterator class. The insert_after method can then modify the next_ptr of that node to insert a new node.

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

Sidebar

Related Questions

I've been looking around trying to determine some Hibernate behavior that I'm unsure about.
I have been trying to make a simple web service and have been following
I have no actual php errors, but When I make my query this I
I'm trying to make an App-wide media-upload which should have the possibility of being
Ok I was a little unsure on how best name this problem :) But
I'm unsure of the best solution for this but this is what I've done.
Unsure how to get this string to work correctly. Keeps making my page blank.
I am unsure on which approach will give me a better performance: I have
I am unsure what would be the best way to implement a success message
I'm a little unsure of how to ask this question, so I'll start as

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.