【问题标题】:Not sure how to implement this insert method in c++ with iterator不知道如何用迭代器在 C++ 中实现这个插入方法
【发布时间】:2011-06-10 03:45:34
【问题描述】:

所以我不确定如何制作这个 insert_after 方法。是的,这是一个硬件任务,但我一直在研究这个,试图弄清楚如何实现这个几个小时。此外,我无法让我的错误检查正常工作,因为它会完全中止程序。

帮助一个 C++ 菜鸟?

#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;
    }*/ 

}

【问题讨论】:

    标签: c++ iterator linked-list


    【解决方案1】:

    由于这是作业,我不会给出示例代码,但是:您必须有一种方法让mylist 访问您的iterator 类中的node*。然后insert_after 方法可以修改该节点的next_ptr 以插入一个新节点。

    【讨论】:

    • +1。进一步的小提示 - 尝试尽可能有选择地允许访问 ptr,以便使用 iterator 的外部客户端代码不会破坏封装。
    • 对不起,伙计们,我想我得再研究一下,这样我才能开始理解你的建议。谢谢
    • @infinite_loop: 好吧,首先让迭代器的node* ptr public (直到你让它工作),然后在insert_after,*place.ptr 将是要插入的节点之后:您想为其next_ptr 分配一个新节点,该节点应构造为容纳data 和现有的place.ptr-&gt;next_ptr。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多