#ifndef __CC_DLL_H_
#define __CC_DLL_H_
#include <iostream>
using namespace std;
template <class T>
class DLL;
template <class T>
class Node{
T _data;
Node<T>* _prev;
Node<T>* _next;
Node(T data, Node<T>* prev=(Node<T>*)0, Node<T>* next=(Node<T>*)0);// just incase, review later
friend class DLL<T>;
};
template <class T>
class DLL{
Node<T>* _head;
Node<T>* _curr;
Node<T>* _tail;
void copy(DLL<T>& D);
public:
DLL();
DLL(DLL<T>& D);
virtual ~DLL();
bool isEmpty();
void append(T data);
DLL<T>& operator=(DLL<T>& D);
T remove(); // removes the current node and returns the data
bool del(); // removes the current node returns false if is empty
void insert(T data); // insterts before current
bool goHead();
bool goTail();
bool goNext();
bool goPrev();
T visit(); // returns the current data
int dept() const; // returns the number of Nodes
};
template <class T>
Node<T>::Node(T data, Node<T>* prev, Node<T>* next){
_data = data;
_prev = prev;
_next = next;
}
template <class T>
DLL<T>::DLL(){
_head = _tail = _curr = 0;
}
template <class T>
DLL<T>::~DLL(){
while(del());
}
template <class T>
void DLL<T>::copy(DLL<T>& D){
int curpos;
for(curpos=0;D.goPrev();curpos++); // findout where is current
if(!D.isEmpty()){
do{
this->append(D.visit());
}while(D.goNext());
}
for(D.goHead(), this->goHead(); curpos; D.goNext(), this->goNext(),curpos--);
}
template <class T>
DLL<T>::DLL(DLL<T>& D){
_head = _tail = _curr = 0;
this->copy(D);
}
template <class T>
DLL<T>& DLL<T>::operator=(DLL<T>& D){
while(del());
this->copy(D);
return *this;
}
template <class T>
void DLL<T>::append(T data){
Node<T>* newnode = new Node<T>(data);
if(_tail){ // ! empty
_tail->_next = newnode;
newnode->_prev = _tail;
_tail = _curr = newnode;
}
else{
_tail = _curr = _head = newnode;
}
}
template <class T>
T DLL<T>::remove(){
T data = visit();
del();
return data;
}
template <class T>
bool DLL<T>::del(){
bool ok = false;
if(_curr){
ok = true;
Node<T>* todel = _curr;
(_curr->_next) ? _curr->_next->_prev = _curr->_prev : _tail = _tail->_prev;
(_curr->_prev) ? _curr->_prev->_next = _curr->_next : _head = _head->_next;
(_curr->_next) ? _curr = _curr->_next : _curr = _curr->_prev;
delete todel;
}
return ok;
}
template <class T>
void DLL<T>::insert(T data){
Node<T>* toInsert = new Node<T>(data);
(_curr->_prev) ? (_curr->_prev->_next = toInsert) : (_head = toInsert);
toInsert->_prev = _curr->_prev;
toInsert->_next = _curr;
_curr->_prev = toInsert;
_curr = toInsert;
}
template <class T>
T DLL<T>::visit(){ // retruns data of current
return _curr->_data;
}
template <class T>
bool DLL<T>::goHead(){
return ((_head) && (_curr = _head)) ? true : false;
}
template <class T>
bool DLL<T>::goTail(){
return ((_tail) && (_curr = _tail)) ? true : false;
}
template <class T>
bool DLL<T>::goNext(){
return ((_curr->_next) && (_curr = _curr->_next)) ? true : false;
}
template <class T>
bool DLL<T>::goPrev(){
return ((_curr->_prev) && (_curr = _curr->_prev)) ? true : false;
}
template <class T>
bool DLL<T>::isEmpty(){
return !_curr;
}
template <class T>
void printList(DLL<T>& d){
d.goHead();
}
template <class T>
ostream& operator<<(ostream& os, DLL<T>& d){
int cur;
bool done = false;
for(cur=0;d.goPrev();cur++); // findout where is current
do{
os<<d.visit();
done = !d.goNext();
if(!done){
os<<", ";
}
}while(!done);
for(d.goHead();cur;d.goNext(), cur--); // set current to what it was before
return os;
}
template <class T>
ostream& operator>>(ostream& os, DLL<T>& d){ // printing in reverse!!!!!
int cur;
bool done = false;
for(cur=0;d.goNext();cur++); // findout where is current
do{
os<<d.visit();
done = !d.goPrev();
if(!done){
os<<", ";
}
}while(!done);
for(d.goTail();cur;d.goPrev(), cur--); // set current to what it was before
return os;
}
#endif
#include "dll.h"
int main(){
DLL<int> d;
for(int i=0;i<10;i++){
d.append(i+1);
}
cout<<d<<endl;
d.goHead();
d.insert(1000);
d.goNext();
d.insert(2000);
cout<<d<<endl;
cout>>d<<endl;
cout<<d.remove()<<endl;
cout<<d<<endl;
return 0;
}
New start
Friday, March 29, 2013
Saturday, March 23, 2013
bitsChal2, using object oriented method
#include <iostream>
using namespace std;
class bits{
unsigned int value;
public:
bits(unsigned int val){
value = val;
}
static bool formatted; // by default false, if true, then bits are shown 4 digit at a time...
ostream& print(ostream& os) const{
unsigned m = 1 << (sizeof(value)*8 - 1);
unsigned i = 1;
while(m){
os<<!!(value & m) ? '1' : '0';
if (formatted){
i == 4 ? os<<' ', i = 1 : i++;
}
m >>= 1;
}
return os;
}
~bits(){
value = 0;
}
};
ostream& operator<<(ostream& os, const bits& from){
return from.print(os);
}
bool bits::formatted = false;
int main(){
char A = 0x5c;
char B = 0x95;
char C = 0;
cout<<bits(A)<<endl; //0000101010101010
bits::formatted = true;
cout<<bits(B)<<endl; //0000 0011 0000 1111
return 0;
}
using namespace std;
class bits{
unsigned int value;
public:
bits(unsigned int val){
value = val;
}
static bool formatted; // by default false, if true, then bits are shown 4 digit at a time...
ostream& print(ostream& os) const{
unsigned m = 1 << (sizeof(value)*8 - 1);
unsigned i = 1;
while(m){
os<<!!(value & m) ? '1' : '0';
if (formatted){
i == 4 ? os<<' ', i = 1 : i++;
}
m >>= 1;
}
return os;
}
~bits(){
value = 0;
}
};
ostream& operator<<(ostream& os, const bits& from){
return from.print(os);
}
bool bits::formatted = false;
int main(){
char A = 0x5c;
char B = 0x95;
char C = 0;
cout<<bits(A)<<endl; //0000101010101010
bits::formatted = true;
cout<<bits(B)<<endl; //0000 0011 0000 1111
return 0;
}
Wednesday, March 20, 2013
setBit, copyBits
void setBit(unsigned int& V, int bitNo, bool value){
if(value){
V = V | (1 << bitNo - 1);
}
else {
V = V & ~(1 << bitNo -1);
}
}
void copyBits(unsigned int& V, int bitNo, int NoOfBits, unsigned int mask){
unsigned int m = 1 << (bitNo - 1);
unsigned int check;
int i;
for(i = 0; i < NoOfBits; i++){//using setBit();
check = mask & m;
setBit(V, bitNo++, check);
m <<= 1;
}
/*for (i = 0; i < NoOfBits; i++){
check = mask & m;
if (check) {
V = V | m;
}
else {
V = V & ~m;
}
m <<= 1;
}*/
}
if(value){
V = V | (1 << bitNo - 1);
}
else {
V = V & ~(1 << bitNo -1);
}
}
void copyBits(unsigned int& V, int bitNo, int NoOfBits, unsigned int mask){
unsigned int m = 1 << (bitNo - 1);
unsigned int check;
int i;
for(i = 0; i < NoOfBits; i++){//using setBit();
check = mask & m;
setBit(V, bitNo++, check);
m <<= 1;
}
/*for (i = 0; i < NoOfBits; i++){
check = mask & m;
if (check) {
V = V | m;
}
else {
V = V & ~m;
}
m <<= 1;
}*/
}
bitChal3 -- Convert double to a binary value
#include <iostream>
using namespace std;
void bitDump(void* address, unsigned int size);
int main(){
long double ld = 12345.123456;
bitDump(&ld, sizeof(ld)); // print the bit pattern (cool if formatted)
return 0;
}
//Loop through size and convert each char to 8 binary digits.
void bitDump(void* address, unsigned int size){
char* c = (char*) address;
int cnt = 1;
for(int i = 0; i < size; i++){
unsigned int m = 1 << 7;
while(m){//convert each char to 8 binary digits
printf("%d", !!(c[i]&m));
m >>= 1;
cnt == 4 ? putchar(' '), cnt = 1 : cnt++;
}
}
putchar('\n');
}
using namespace std;
void bitDump(void* address, unsigned int size);
int main(){
long double ld = 12345.123456;
bitDump(&ld, sizeof(ld)); // print the bit pattern (cool if formatted)
return 0;
}
//Loop through size and convert each char to 8 binary digits.
void bitDump(void* address, unsigned int size){
char* c = (char*) address;
int cnt = 1;
for(int i = 0; i < size; i++){
unsigned int m = 1 << 7;
while(m){//convert each char to 8 binary digits
printf("%d", !!(c[i]&m));
m >>= 1;
cnt == 4 ? putchar(' '), cnt = 1 : cnt++;
}
}
putchar('\n');
}
bitChal1.cpp ---Store binary into a string.
#include <iostream>
using namespace std;
const char* bits(unsigned int val);
int main(){
char A = 0x5c;
char B = 0x95;
char C = 0;
cout<<bits(A)<<endl;
return 0;
}
const char* bits(unsigned int val){
int i = 0;
int j = '1' - 1; //The ascii value between char '1' and decimal 1
unsigned int m = 1 << sizeof(val)*8 - 1;
char* s = new char[sizeof(val)*8+1];
while(m){
s[i] = j + !!(m & val);
m = m >> 1;
i++;
}
s[i] = 0;
return s;
}
using namespace std;
const char* bits(unsigned int val);
int main(){
char A = 0x5c;
char B = 0x95;
char C = 0;
cout<<bits(A)<<endl;
return 0;
}
const char* bits(unsigned int val){
int i = 0;
int j = '1' - 1; //The ascii value between char '1' and decimal 1
unsigned int m = 1 << sizeof(val)*8 - 1;
char* s = new char[sizeof(val)*8+1];
while(m){
s[i] = j + !!(m & val);
m = m >> 1;
i++;
}
s[i] = 0;
return s;
}
Wednesday, March 13, 2013
dll.cpp implement
#include "dll.h"
Node::Node(int data, Node* prev, Node* next){
_data = data;
_prev = prev;
_next = next;
}
DLL::DLL(){
_head = _tail = _curr = 0;
}
DLL::~DLL(){
while(del());
}
void DLL::copy(DLL& D){
int curpos;
for(curpos=0;D.goPrev();curpos++); // findout where is current
if(!D.isEmpty()){
do{
this->append(D.visit());
}while(D.goNext());
}
for(D.goHead(), this->goHead();curpos;D.goNext(), this->goNext(),curpos--);
}
DLL::DLL(DLL& D){
_head = _tail = _curr = 0;
this->copy(D);
}
DLL& DLL::operator=(DLL& D){
while(del());
this->copy(D);
return *this;
}
void DLL::append(int data){
Node* newnode = new Node(data);
if(_tail){ // ! empty
_tail->_next = newnode;
newnode->_prev = _tail;
_tail = _curr = newnode;
}
else{
_tail = _curr = _head = newnode;
}
}
int DLL::remove(){
int data = visit();
del();
return data;
}
bool DLL::del(){
bool ok = false;
if(_curr){
ok = true;
Node* todel = _curr;
(_curr->_next) ? _curr->_next->_prev = _curr->_prev : _tail = _tail->_prev;
(_curr->_prev) ? _curr->_prev->_next = _curr->_next : _head = _head->_next;
(_curr->_next) ? _curr = _curr->_next : _curr = _curr->_prev;
delete todel;
}
return ok;
}
void DLL::insert(int data){
Node* toInsert = new Node(data);
(_curr->_prev) ? (_curr->_prev->_next = toInsert) : (_head = toInsert);
toInsert->_prev = _curr->_prev;
toInsert->_next = _curr;
_curr->_prev = toInsert;
_curr = toInsert;
}
int DLL::visit(){ // retruns data of current
return _curr->_data;
}
bool DLL::goHead(){
return ((_head) && (_curr = _head)) ? true : false;
}
bool DLL::goTail(){
return ((_tail) && (_curr = _tail)) ? true : false;
}
bool DLL::goNext(){
return ((_curr->_next) && (_curr = _curr->_next)) ? true : false;
}
bool DLL::goPrev(){
return ((_curr->_prev) && (_curr = _curr->_prev)) ? true : false;
}
bool DLL::isEmpty(){
return !_curr;
}
Node::Node(int data, Node* prev, Node* next){
_data = data;
_prev = prev;
_next = next;
}
DLL::DLL(){
_head = _tail = _curr = 0;
}
DLL::~DLL(){
while(del());
}
void DLL::copy(DLL& D){
int curpos;
for(curpos=0;D.goPrev();curpos++); // findout where is current
if(!D.isEmpty()){
do{
this->append(D.visit());
}while(D.goNext());
}
for(D.goHead(), this->goHead();curpos;D.goNext(), this->goNext(),curpos--);
}
DLL::DLL(DLL& D){
_head = _tail = _curr = 0;
this->copy(D);
}
DLL& DLL::operator=(DLL& D){
while(del());
this->copy(D);
return *this;
}
void DLL::append(int data){
Node* newnode = new Node(data);
if(_tail){ // ! empty
_tail->_next = newnode;
newnode->_prev = _tail;
_tail = _curr = newnode;
}
else{
_tail = _curr = _head = newnode;
}
}
int DLL::remove(){
int data = visit();
del();
return data;
}
bool DLL::del(){
bool ok = false;
if(_curr){
ok = true;
Node* todel = _curr;
(_curr->_next) ? _curr->_next->_prev = _curr->_prev : _tail = _tail->_prev;
(_curr->_prev) ? _curr->_prev->_next = _curr->_next : _head = _head->_next;
(_curr->_next) ? _curr = _curr->_next : _curr = _curr->_prev;
delete todel;
}
return ok;
}
void DLL::insert(int data){
Node* toInsert = new Node(data);
(_curr->_prev) ? (_curr->_prev->_next = toInsert) : (_head = toInsert);
toInsert->_prev = _curr->_prev;
toInsert->_next = _curr;
_curr->_prev = toInsert;
_curr = toInsert;
}
int DLL::visit(){ // retruns data of current
return _curr->_data;
}
bool DLL::goHead(){
return ((_head) && (_curr = _head)) ? true : false;
}
bool DLL::goTail(){
return ((_tail) && (_curr = _tail)) ? true : false;
}
bool DLL::goNext(){
return ((_curr->_next) && (_curr = _curr->_next)) ? true : false;
}
bool DLL::goPrev(){
return ((_curr->_prev) && (_curr = _curr->_prev)) ? true : false;
}
bool DLL::isEmpty(){
return !_curr;
}
Friday, February 8, 2013
One line Display function
Here is the solution without using strlen() function. It looks so confusing because I thought we should aviod using the strlen() function...Can you figure out my logic and help to improve it.
bool nullFound = false;
for (setPos(row, col), i = 0; i < (fieldLen == 0 ? fieldLen + 1 : fieldLen);
(!nullFound ? (nullFound = (str[i] == 0 ? true : false)) : nullFound = true),
fieldLen == 0 ? *this << str : (!nullFound ? putChar(str[i]) : putChar(' ')),
i++);
}
bool nullFound = false;
for (setPos(row, col), i = 0; i < (fieldLen == 0 ? fieldLen + 1 : fieldLen);
(!nullFound ? (nullFound = (str[i] == 0 ? true : false)) : nullFound = true),
fieldLen == 0 ? *this << str : (!nullFound ? putChar(str[i]) : putChar(' ')),
i++);
}
Subscribe to:
Posts (Atom)