#ifndef __TQUEUE_H__
#define __TQUEUE_H__
/* template signatures:
1- all related classes and types will have the signature
2- the name of the class right after the template will NOT have the signature
3- the constructor names will NOT have the signature
4- the destructor name will NOT have the signature
*/
/* Tqueue requirements:
1- Template class must be copied safely.
2- Template class must work with operator<
*/
template <class type>
class Tqueue;
template <class type>
class Tqnode{
Tqnode<type>* _next;
Tqnode<type>* _prev;
type _data;
Tqnode(type data,
Tqnode<type>* prev = (Tqnode<type>*)0,
Tqnode<type>* next = (Tqnode<type>*)0):_data(data){
_next = next;
_prev = prev;
}
friend class Tqueue<type>;
};
template <class type>
class Tqueue{
Tqnode<type>* _head;
Tqnode<type>* _curr;
Tqnode<type>* _tail;
public:
Tqueue(){
_head = _tail = _curr = (Tqnode<type>*)0;
}
virtual ~Tqueue(){
goHead();
while(_curr){
_head = _curr;
_curr = _curr->_next;
delete _head;
}
}
bool goNext();
bool goPrev();
bool goHead();
bool goTail();
bool isEmpty(){
return !_curr;
}
void append(type data){ // appends to tail
Tqnode<type>* temp= new Tqnode<type>(data, _tail);
if(_curr){
_tail = temp;
_tail->_prev->_next = _tail;
// _temp->_prev->_next = _temp;
}
else{
_curr = _head = _tail = temp;
}
}
type remove(){ // removes the data from the head of the list
if(_head == _tail){
type data = _head->_data;
delete _head;
_head = _tail = _curr = (Tqnode<type>*)0;
return data;
}
else{
type data = _head->_data;
Tqnode<type>* todel = _head;
if(_head == _curr){
_curr = _curr->_next;
}
_head = _head->_next;
_head->_prev = (Tqnode<type>*)0;
delete todel;
return data;
}
}
void insertBefore(type data) { // before current (prev side)
Tqnode<type>* preadd = new Tqnode<type>(data);
if(_curr) {
preadd->_prev=_curr->_prev;
preadd->_next=_curr;
if(_head!=_curr) {
_curr->_prev->_next = preadd;
}
else {
_head = preadd;
}
_curr->_prev = preadd;
}
else {
_curr = _head = _tail = preadd;
}
}
void insertAfter(type data) { // after current (next side)
Tqnode<type>* nextadd = new Tqnode<type>(data);
if(_curr) {
nextadd->_prev = _curr;
nextadd->_next = _curr->_next;
if(_curr != _tail) {
_curr->_next->_prev= nextadd;
}
else {
_tail = nextadd;
}
_curr->_next= nextadd;
}
else {
_curr = _head = _tail = nextadd;
}
}
type visit(){ // returs the data of the current
return _curr->_data;
}
type removeCurrent() { // removes curent
Tqnode<type>* todel = _curr;
type data = _curr->_data;
if(_head != _tail) {
if(_head == _curr) {
_curr->_next->_prev = (Tqnode<type>*) 0;
_curr = _curr->_next;
_head = _curr;
}
else if(_curr == _tail) {
_curr->_prev->_next = _curr->_next;
_curr = _curr->_prev;
_tail = _curr;
}
else {
_curr->_next->_prev = _curr->_prev;
_curr->_prev->_next = _curr->_next;
_curr = _curr->_next;
}
}
else {
_head = _tail = _curr = (Tqnode<type>*)0;
}
delete todel;
return data;
}
bool sort(bool Ascending = true) { // sorts the nodes
int ret = 0;
if(Ascending && _curr) {
Tqnode<type>* seq = _head;
int i=0;
type data = 0;
while(seq->_next) {
seq = seq->_next;
i++;
}
seq = _head;
for(int j=0; j <= i; j++){
for(int k=0; k < i; k++) {
if(seq->_data > seq->_next->_data)
{
data = seq->_data;
seq->_data = seq->_next->_data;
seq->_next->_data= data;
ret = 1;
}
seq = seq->_next;
}
seq = _head;
}
}
else if(!Ascending && _curr) {
Tqnode<type>* seq = _head;
int i=0;
type data = 0;
while(seq->_next) {
seq = seq->_next;
i++;
}
seq = _head;
for(int j=0; j <= i; j++){
for(int k=0; k < i; k++) {
if(seq->_data < seq->_next->_data)
{
data = seq->_data;
seq->_data = seq->_next->_data;
seq->_next->_data= data;
ret = 1;
}
seq = seq->_next;
}
seq = _head;
}
}
return ret;
}
void push(type data) {
Tqnode<type>* add = new Tqnode<type>(data);
if(!_curr) {
_curr = _head = _tail = add;
}
else {
_head->_prev=add;
add->_next=_head;
_head = add;
}
}
type pop() {
if(_curr) {
type data = _head->_data;
Tqnode<type>* del = _head;
if(_head == _curr) {
_curr = _curr->_next;
}
_head = _head->_next;
delete del;
return data;
}
}
};
template <class type>
bool Tqueue<type>::goTail(){
bool res = false;
if(_curr){ // is not null
_curr = _tail;
res = true;
}
return res;
}
template <class type>
bool Tqueue<type>::goHead(){
bool res = false;
if(_curr){ // is not null
_curr = _head;
res = true;
}
return res;
}
template <class type>
bool Tqueue<type>::goNext(){
bool res = false;
if(_curr && _curr->_next){
_curr = _curr->_next;
res = true;
}
return res;
}
template <class type>
bool Tqueue<type>::goPrev(){
bool res = false;
if(_curr != _head){
_curr = _curr->_prev;
res = true;
}
return res;
}
#endif
balwinder open source@seneca
Tuesday, 20 March 2012
Monday, 12 March 2012
Return the bits present in a integer by using a function
#include<iostream>
using namespace std;
#define len sizeof(int)*8
char* bits(unsigned int j) {
int c = 0;
int cur=0;
int i=sizeof(int)*8;
static char str[len+1] = {'\0'};
while(c <= i-1) {
cur = j<<(i-c-1)>>(i-1);
if(cur==1) str[i-1-c] = '1';
else str[i-1-c]= '0';
c++;
}
return str;
}
int main() {
int i = 2345;
cout<<"i: "<<bits(i)<<endl;
return 0;
}
using namespace std;
#define len sizeof(int)*8
char* bits(unsigned int j) {
int c = 0;
int cur=0;
int i=sizeof(int)*8;
static char str[len+1] = {'\0'};
while(c <= i-1) {
cur = j<<(i-c-1)>>(i-1);
if(cur==1) str[i-1-c] = '1';
else str[i-1-c]= '0';
c++;
}
return str;
}
int main() {
int i = 2345;
cout<<"i: "<<bits(i)<<endl;
return 0;
}
Thursday, 8 March 2012
Find the given Bit number in a integer value is ON or OFF
bool isOn(unsigned int val, int bitNo){
return ((val<<(32-bitNo))>>31);
}
bool isOff(unsigned int val, int bitNo){
return !((val<<(32-bitNo))>>31);
}
return ((val<<(32-bitNo))>>31);
}
bool isOff(unsigned int val, int bitNo){
return !((val<<(32-bitNo))>>31);
}
Sunday, 4 March 2012
Challenging Question's Solution
int validMonth(int mon, char* errmes){ return ((mon >= 1 && mon <= 12) || !strcpy(errmes, "Invalid month!")); }
Miniprint with bugs fixed better code
#include <cstdio>
#include <cstdarg>
using namespace std;
// this has bugs , fix it
void miniprint(const char *format, ...){
va_list ap;
int done =0;
va_start(ap, format);
while(*format){
if(*format == '%'){
done = 0;
while(!done)
{
format++;
switch(*format){
case 'd':
printf("%d", va_arg(ap,int)) && (done = 1);
break;
case 'f':
printf("%0.2f", va_arg(ap,double)) && (done = 1);
break;
case 's':
printf("%s", va_arg(ap,char*)) && (done = 1);
break;
case 'x':
printf("%x", va_arg(ap,int)) && (done = 1);
break;
case 'X':
printf("%X", va_arg(ap,int)) && (done = 1);
break;
case 'p':
printf("%p", va_arg(ap,void*)) && (done = 1);
break;
default:
format++;
}
}
}
else{
putchar(*format);
}
format++;
}
va_end(ap);
}
int main(){
miniprint("hello I am %s and I make %.6f Dollars a year\n", "Homer", 12345.67);
return 0;
}
#include <cstdarg>
using namespace std;
// this has bugs , fix it
void miniprint(const char *format, ...){
va_list ap;
int done =0;
va_start(ap, format);
while(*format){
if(*format == '%'){
done = 0;
while(!done)
{
format++;
switch(*format){
case 'd':
printf("%d", va_arg(ap,int)) && (done = 1);
break;
case 'f':
printf("%0.2f", va_arg(ap,double)) && (done = 1);
break;
case 's':
printf("%s", va_arg(ap,char*)) && (done = 1);
break;
case 'x':
printf("%x", va_arg(ap,int)) && (done = 1);
break;
case 'X':
printf("%X", va_arg(ap,int)) && (done = 1);
break;
case 'p':
printf("%p", va_arg(ap,void*)) && (done = 1);
break;
default:
format++;
}
}
}
else{
putchar(*format);
}
format++;
}
va_end(ap);
}
int main(){
miniprint("hello I am %s and I make %.6f Dollars a year\n", "Homer", 12345.67);
return 0;
}
Monday, 9 January 2012
Going to start my new experience
I am very excited, because its my first time that i am going to work on open source software. very exited :)
Subscribe to:
Posts (Atom)