Not sure how to implement this insert method in c++ with iterator
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()开发者_Go百科 != 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;
}*/
}
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.
精彩评论