问题描述
|
所以我不确定如何制作这个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;
}*/
}
解决方法
由于这是家庭作业,因此我不提供示例代码,但是:您必须有一种方法让
mylist
访问iterator
类中的node*
。然后,“ 4”方法可以修改该节点的“ 5”以插入新节点。