Sunday, March 31, 2013
setBit and copyBit
void setBit(unsigned int& V, int bitNo, bool val){
if(val){
V=(1<<(bitNo-1))|V;
}
else{
V=(1<<(bitNo-1))&V;
}
}
void copyBits(unsigned int& V, int bitFrom, int length, unsigned int mask){
int m=1<<(bitFrom-1);
for(int i=(bitFrom);i<bitFrom+length;i++){
setBit(V,i,!!(mask&m));
m=m<<1;
}
}
Monday, March 18, 2013
Add method of the Queue
queue.h:#ifndef __XL_QUEUE_H__#define __XL_QUEUE_H__class Queue;class Node{int _data;Node* _next;Node(int data, Node* next=(Node*)0);friend class Queue;};class Queue{Node* _head;public:Queue();void add(int data);int remove();bool isEmpty();virtual ~Queue();};#endifqueue.cpp:#include "queue.h"Node::Node(int data, Node* next){_data = data;_next = next;}Queue::Queue(){_head = (Node*)0;}void Queue::add(int data){Node* toAdd = _head;Node* new = new Node(data);while(toAdd->_next){last=last->_next;}tail->_next = new;}int Queue::remove(){int ret = _head->_data;Node* toDel = _head;_head = _head->_next;delete toDel;return ret;}bool Queue::isEmpty(){return true;}Queue::~Queue(){while(!isEmpty()){remove();}}
Subscribe to:
Posts (Atom)