Wednesday 20 March 2013

Int to Binary String

//intToBinary.cpp
//HYQ
//Mar 20,2013

const char* bits(unsigned int val){
    int i;
    unsigned int m;
    char* v = new char[(sizeof(val)*8)+1];
    int c=48;//ascII char '0' 

    for(i=0,m=1<<(sizeof(val)*8-1);
      m;//until 0
      v[i]=(c+!!(val&m)),m=m>>1,i++);
    v[i]=0;
    return v;
}

Wednesday 13 March 2013

DLL Program

Program of DLL
Function description:
1.DLL constructor
2.DLL destructor
3.DLL Copy constructor
4.DLL operator=()
5.DLL copy()
6.DLL append()
7.DLL insert()
8.DLL remove()
8.DLL del()
//dll.cpp
//HYQ
//Mar 14, 2013
//Release v0.3

DLL::DLL(){
  _head = _tail = _curr =  0;
}

DLL::~DLL(){
  while (del());
}

DLL::DLL(DLL& D){//prevent copying
 _head = _tail = _curr = 0;
 copy(D);
} 

DLL& DLL::operator=(DLL& D){
  while (del());
  copy(D);
  return *this;
}

void DLL::copy(DLL& D){
  int currPos;
  for(currPos=0;D.goPrev();currPos++); // findout where is current
  if (!D.isEmpty()) {
    for(this->append(D.visit());D.goNext();this->append(D.visit()));
  }
  for(D.goHead(),this->goHead();currPos;D.goNext(),this->goNext(), currPos--); // set current to what it was before
}  


void DLL::append(int data){// add after tail
  Node* newNode = new Node(data, _tail);

  if (_tail!=0){
    _tail = _curr = _curr->_next = newNode;
  }
  else{
    _head = _tail = _curr = newNode;
  }
}

void DLL::insert(int data){// inserts data before current
  Node* newNode = new Node(data,_curr->_prev,_curr);
  
  if (!isEmpty()){
    if (_curr->_prev){
      _curr = _curr->_prev = _curr->_prev->_next = newNode;
    }
    else{
      _head = _curr = _curr->_prev = newNode;
    }
  }
  else{
    _head = _tail = _curr = newNode;
  }
}

int DLL::remove(){// remove the currect goes next if possible
 int data = visit();
 del();
 return data;
}


bool DLL::del(){
  bool ok;

  if (ok = !isEmpty()){
    Node* ToDel = _curr;
    
    if (_curr->_next){//NextNode exists, current node is not tail
      _curr->_next->_prev = _curr->_prev;
    }
    else{
      _tail = _tail->_prev;
    }

    if (_curr->_prev){//current node is not head
      _curr->_prev->_next = _curr->_next;
    }
    else{
       _head = _head->_next;
    }
    if(_curr->_next){
      _curr = _curr->_next;
    }
    else{
      _curr = _curr->_prev;
    }
    delete ToDel;
  }
  return ok;
}


Saturday 9 March 2013

Program of Queue - get a node's value,previous node,print a queue

Program of Queue
Function description:
1.get a node's value
2.print a queue
3.Find previous node

//queue.cpp
//H.Y.Q
//Mar 08, 2013
//Release v0.2

//Get the data's value of current Node
int Queue::get(const Node* currentNode) const {
  return currentNode->_data;
}

//Print the Node's data value in the queue
void Queue::print(){
  Node* toPrint = _head;
  int i=0;

  while ((toPrint !=0) && 
          (cout << (++i)<< ". Data: "<< get(toPrint) << "\tAddress: " << (void*) toPrint << endl) && 
          (toPrint = nextNode(toPrint)));
}


//Return the previous Node of current Node
Node* Queue::prevNode(const Node* currentNode) const{
  Node* prev = _head;    
  while (prev != 0 && nextNode(prev) != currentNode){
      prev = nextNode(prev);
  }
  return prev;
}