Friday 16 December 2011

Bit pattern - Circular Right Shift

Yesterday's exam question
didn't get it during the exam though...was completely out of time.

void circularShiftRight(unsigned& V, int fromBit, int toBit, int times) {
  unsigned int tempV = V;
  unsigned int rightBit = 0;
  unsigned int mask = 0;
  bool checkRightBit;
  int i,j;
  for (j = 0;j < times;j++) {
    checkRightBit = !!(V & (1 << fromBit - 1));
    rightBit = checkRightBit?(1<<toBit - 1):0;

    mask = 0;
    for (i = fromBit;i < toBit + 1;i++)
      mask = mask | (1 << (i-1));

    tempV = tempV & mask;
    tempV = tempV >> fromBit;
    tempV = tempV << (fromBit - 1);
    tempV = tempV | rightBit;
    V = V & ~mask;
    V = tempV | V;
  }
}

void prnBits(unsigned int Val){
  for(int i= sizeof(Val)*8;i>0;i--){
    if(!(i%4)) printf(" ");
    printf("%d", IsSet(Val,i)?1:0);
  }
  printf("\n");
}

int main(){
  unsigned int B = 0xABCDEF11;
  unsigned int C;
  prnBits(B);
  circularShiftRight(B, 5, 12, 4);
  prnBits(B);
  circularShiftRight(B, 5, 12, 4);
  prnBits(B);
  return 0;
}

//Output
 1010 1011 1100 1101 1110 1111 0001 0001
 1010 1011 1100 1101 1110 0001 1111 0001
 1010 1011 1100 1101 1110 1111 0001 0001
Press any key to continue . . .

Tuesday 13 December 2011

Linked list - Move node

Another practice on linked list, moving a node from one position to another position.
This could implement to CMenu to change the CMenuItem position in R0.6.

//queue.cpp

bool Queue::moveNode(int toNode, int fromNode) {
  QNode* nodeToMove = (QNode*)0;
  bool r = false;
  if ((toNode <= _size) && (fromNode <= _size) && toNode != fromNode) {
    r = true;
    // extract the node
    _cur = _head;
    int i;
    for(i = 1;i < fromNode && _cur->_next ;i++) {
        _cur = _cur->_next;
    }
    nodeToMove = _cur;
    if (_cur == _tail) {
      _tail = _cur->_prev;
      _cur->_prev->_next = (QNode*)0;
    }
    else if (_cur == _head) {
      _head = _cur->_next;
      _cur->_next->_prev =  (QNode*)0;
    }
    else {
      _cur->_next->_prev = _cur->_next->_prev->_prev;
      _cur->_prev->_next = _cur->_prev->_next->_next;
    }

    // place the node
    _cur = _head;
    for (i = 1;i < toNode && _cur->_next ;i++) {
      _cur = _cur->_next;
    }
    if (_cur == _head) {
      _head = nodeToMove;
      nodeToMove->_next = _cur;
      nodeToMove->_prev = (QNode*)0;
      _cur->_prev = nodeToMove;
    }
    else if (_cur == _tail) {
      _tail = nodeToMove;
      nodeToMove->_next = (QNode*)0;
      nodeToMove->_prev = _cur;
      _cur->_next = nodeToMove;
    }
    else {
      nodeToMove->_prev = _cur->_prev;
      nodeToMove->_next = _cur;
      _cur->_prev->_next = nodeToMove;
      _cur->_prev = nodeToMove;
    }
  }
  return r;
}


//queuetest.cpp

#include "queue.h"
#include <iostream>
using namespace std;

int main(){
  int i;
  Queue Q;
  for(i=10;i<100;i+=10){
    Q.append(i);
  }
  if(Q.goHead()){
    do{
      cout<<Q.visit()<<", ";
    }while(Q.goNext());
  }
  cout<<endl;
  //cout<<"removed: "<<Q.remove()<<endl;
  //Q.append(3000);
  Q.moveNode(9, 1);
  cout<<"from 1 to 9:  ";
  if(Q.goHead()){
    do{
      cout<<Q.visit()<<", ";
    }while(Q.goNext());
  }
  cout<<endl;
  cout<<"from 9 to 1:  ";
  Q.moveNode(1, 9);
  if(Q.goHead()){
    do{
      cout<<Q.visit()<<", ";
    }while(Q.goNext());
  }
  cout<<endl;
  cout<<"from 7 to 3:  ";
  Q.moveNode(3, 7);
  if(Q.goHead()){
    do{
      cout<<Q.visit()<<", ";
    }while(Q.goNext());
  }
  cout<<endl;

  return 0;
}

//Output
10, 20, 30, 40, 50, 60, 70, 80, 90,
from 1 to 9:  20, 30, 40, 50, 60, 70, 80, 90, 10,
from 9 to 1:  10, 20, 30, 40, 50, 60, 70, 80, 90,
from 7 to 3:  10, 20, 70, 30, 40, 50, 60, 80, 90,
deleting: 10
deleting: 20
deleting: 70
deleting: 30
deleting: 40
deleting: 50
deleting: 60
deleting: 80
deleting: 90
Press any key to continue . . .

Linked list - Remove node given the node position

Since Fardad didn't give out the answer of test1, I decide to do this coding for practice


int Queue::removeNode(int nodeNum) {
  QNode* delNode = (QNode*)0;
  int val = 0;
  // if list is not empty and nodeNum is within the size of list
  if (nodeNum <= _size && _size > 0) {
    //if nodeNum is 1st node in list
    if (nodeNum == 1) {    
      val = remove();
    }
    // if nodeNum is last node in list
    else if (nodeNum == _size) {
      delNode = _tail;
      _tail = _tail->_prev;
      _tail->_next = (QNode*)0;
    }

    else {
      _cur = _head;
      int i;
      for(i = 1;i < nodeNum;i++) {
        _cur = _cur->_next;
      }
      delNode = _cur;
      _cur->_next->_prev = _cur->_next->_prev->_prev;
      _cur->_prev->_next = _cur->_prev->_next->_next;
    }
    _size--;
  }
  if (delNode) {
    val = delNode->_data;
    delete delNode;
  }
  return val;
}