Wednesday, July 10, 2013

Double Link List (part of the code)


//Moves the current pointer to the previous item of the list

void goPrev(){
        if(cur!=front){   // in case the current Node is the head of the list
          cur=cur->prev;
        }     
      }


 //sets the value within current item
 void setValue(int d){
        if(cur){
          cur->data =d;
        }       
      }


//in the DLLNode struct  add the constructor

DLLNode(int d,DLLNode* n, DLLNode* p):data(d),next(n),prev(p){}  


   //Insert a new node before or after the current item
     
void insertBefore(int d){

          if(cur){
            DLLNode* p = new DLLNode(d,cur,cur->prev);
            if(cur->prev){// the cur Node is not the head of the list
                if(p){
                    cur->prev->next =p;
                    cur->prev = p;
                }
            }
            else{  // the cur Node is the head of the list
                if(p){
                  cur->prev = p;
                  front = p;  // change the front to p
                }
          }        
      }
          else  // the list is empty
          {
              DLLNode* p = new DLLNode(d, NULL,NULL);
              if(p)
                front = back = cur = p;         
          }

        size++;
    }







No comments:

Post a Comment