lundi 29 juin 2015

What is difference between iterators and pointers


So I wrote this program and it works, but I am being told that I need to use iterators to move about in the functions that I wrote. I am currently using pointers but I thought they were basically the same things. And if there is a difference, how is an iterator used?

UPDATE: So what I am understanding is that an iterator, unlike a pointer, will move to the next item in a list even if that item is not next sequentially in memory? Is this correct. But then how does it know where to move? I have programmed a binary tree that adds and deletes nodes. In each node there are pointers that point to the two children (or just one or none) and also a pointer that points to the parent. In the functions I recursively call the functions that I am using and passing the new pointers. I have a felling that replacing what I have with iterators should be easy, but I am still unsure. Here is the function that adds a node to the binary tree. I would be appreciative if someone would help me in the right direction as to how to use iterators rather than pointers. I will do the other functions in my program in order to ensure that I have at least a little knowledge about how this works

void addelement( node *ptr, int add )          
   {

       if ( ptr->value == add )//check to see if the value can be added
      {
          printf("\nVALUE ALREADY IN TREE, NOT ADDED\n\n");//tell user that                the value is already in the set
        return;
   }


        else if ( ptr->value > add )//see if value to add is less than current node value 
        {

        if ( ptr->lesschild == NULL )//check to see if value can be added or if there is a child
        {
             ptr->lesschild =(node *) malloc(sizeof(struct node));//make a new node 
             ptr->lesschild->value = add;//assign value to add into new node   
             ptr->lesschild->lesschild = NULL;//set pointers of new node to NULL as no children
             ptr->lesschild->greatchild = NULL;
             ptr->lesschild->parent = ptr;//set pointer to parent of node
             return;          
        }
        else//if there is a child then move to child node and recall addelement function
        {
            addelement( ptr->lesschild, add );
            return;
        }
   }

   else if ( ptr->value < add )//see if value to add is less than current node value  
   {

        if ( ptr->greatchild == NULL )//check to see if value can be added or if there is a child
        {
             ptr->greatchild = (node *)malloc(sizeof(node));//make a new node 
             ptr->greatchild->value = add;//assign value to add into new node 
             ptr->greatchild->lesschild = NULL;//set pointers of new node to NULL as no children
             ptr->greatchild->greatchild = NULL;
             ptr->greatchild->parent = ptr;//set pointer of parent of node
             return;   

        }
        else//if there is a child then move to child node and recall addelement function
        {
            addelement( ptr->greatchild, add );
            return;
        }
        }
        }

and here is the class of node

class node //structure declaration of node
{
 public: 
 int value;        //holds value of node
 node *lesschild;  //pointer to lesser value child
 node *greatchild; //pointer to greater value child
 node *parent; //pointer to the parent of the node
}


Aucun commentaire:

Enregistrer un commentaire