Challenge accepted :)
I've looked at what Amir and Saeid did, nice job, but I was wondering how you guys checked it if your code not even complied without doing this function modifiable with & sign.
I decided to go a bit further and I did my operator[] method able to add new Nodes to the end of the Queue if we have index > size.
Here is the code
int& Queue::operator[](unsigned int index){
Node* temp = _head;
int i;
if (index >= _size) {
while(index>=_size){
int data = (_tail->_data/10)+(_head->_data/10);
Node* newnode = new Node(data);
_tail->_next = newnode;
_tail = newnode;
_size++;
}
}
for (i = 0; i < index % _size; i++){
temp = temp-> _next;
}
return temp-> _data;
}
---------------------------------------------------------
So then in the tester instead of:
for(i=0;Q.size();i++){
cout<<Q[i]<<endl;
Q[i] = Q[i] * 10;
}
we have:
for(i=0;i<10;i++){
cout<<Q[i]<<endl;
Q[i] = Q[i] * 10;
}
And it will continue add Nodes with valid data.
---------------------------------------------------------
Comments are welcome...
I've looked at what Amir and Saeid did, nice job, but I was wondering how you guys checked it if your code not even complied without doing this function modifiable with & sign.
I decided to go a bit further and I did my operator[] method able to add new Nodes to the end of the Queue if we have index > size.
Here is the code
int& Queue::operator[](unsigned int index){
Node* temp = _head;
int i;
if (index >= _size) {
while(index>=_size){
int data = (_tail->_data/10)+(_head->_data/10);
Node* newnode = new Node(data);
_tail->_next = newnode;
_tail = newnode;
_size++;
}
}
for (i = 0; i < index % _size; i++){
temp = temp-> _next;
}
return temp-> _data;
}
---------------------------------------------------------
So then in the tester instead of:
for(i=0;Q.size();i++){
cout<<Q[i]<<endl;
Q[i] = Q[i] * 10;
}
we have:
for(i=0;i<10;i++){
cout<<Q[i]<<endl;
Q[i] = Q[i] * 10;
}
And it will continue add Nodes with valid data.
---------------------------------------------------------
Comments are welcome...