Tuesday 20 March 2012

insertBefore and insertAfter functions

template <class type>
 void Tqueue<type>::insertBefore(type data){
  if(_curr){
    Tqnode<type>* temp= new Tqnode<type>(data,_curr->_prev, _curr);
    if(temp->_prev) temp->_prev->_next = temp; 
    _curr->_prev = temp;
    _curr=temp;
  }else{
  Tqnode<type>* temp= new Tqnode<type>(data);
    _curr = _head = _tail = temp;
     }
 }
 template <class type>
 void Tqueue<type>::insertAfter(type data){
  if(_curr){
    Tqnode<type>* temp= new Tqnode<type>(data,_curr, _curr->_next);
    if(temp->_next) temp->_next->_prev = temp; 
    _curr->_next = temp;
    _curr=temp;
  }else{
  Tqnode<type>* temp= new Tqnode<type>(data);
    _curr = _head = _tail = temp;
     }
 }

Monday 12 March 2012

Using cout to print binary version of int

  #include<iostream>
  #include<cstdio>
  using namespace std;

  char*  bits(int val){
  const int size=sizeof(int)*8;
  static char str[size+1];
  int i;
  int j;
  for(i =(sizeof(int)*8-1), j=0;i>=0 ;i--, j++){ 
    val & (1 << i)&&(str[j]='1')||(str[j]='0');
  }
  str[j]=0;
  return str;
 }
 int main(){
  cout<<bits(2345);
  putchar('\n');
  return 0;
 }

Friday 2 March 2012

Test 1 – 20113 Q4

stack.h
#pragma once
#include 
using namespace std;
class Stack;
class Node{
  int _data;
  Node* _next;
  Node(int data, Node* next);
  friend class Stack;
};
 
class Stack{
  Node* _top;
public:
 Stack();
  void push(int data);
  int pop();
  bool isEmpty();
  int depth();
  bool delNode(int num); 
  int append(int );
  virtual ~Stack();
};
stack.cpp
#include "stack.h"
Node::Node(int data, Node* next){
  _data = data;
  _next = next;
}
Stack::Stack(){
  _top = (Node*)0;
}
void Stack::push(int data){
 Node* tempnode = new Node(data, _top);
 _top = tempnode;
}
int Stack::pop(){
  int ret = _top->_data;
  Node* toDel = _top;
  _top = _top->_next;
  delete toDel;
  return ret;
}
int Stack::depth(){
  int ret=0;
  Node* temp=_top;
  while(temp){
   ret += !!(temp);
   temp=temp->_next;
  }
  return ret;
}
bool Stack:: delNode(int num){
 int i;
 bool done = false;
 Node* cur =_top;
    Node* prev = (Node*)0;
 for(i=1; cur != (Node*)0 && !done && i<=num; i++){
    if(i == num ){
      Node* toDel= cur;
      if(cur == _top){
        pop();
        done = true;
      }
      else {
        prev->_next = cur->_next;
        delete toDel;
        done = true;
      }
    }
    else {
      prev = cur;
      cur = cur->_next;
    }
  }
  return done;
}
int Stack:: append(int data){
   if(_top){
  Node* temp = _top;
  Node* Last = _top;
  while(temp){
  Last = temp;
  temp = temp->_next;
  }
  Last->_next = new Node(data, (Node*)0); 
 }else
  push(data);
 return data;
 }
bool Stack::isEmpty(){
  return _top == (Node*)0;
}
Stack::~Stack(){
  while(!isEmpty()) pop();
}

Thursday 1 March 2012

Test 1 – 20113 Q2

#include 
#include 
#include 
using namespace std;
 
int main(int argc,char* argv[])
{
    FILE *fs1;
    FILE *fs2;
    FILE *ft;
    char ch;
    int i;
    if ( argc != 4 ) /* argc should be 4 for correct execution */
      {      
        printf( "Arguments provided are not sufficiet\n");  
      }
    else{
    fs1 = fopen( argv[2], "r" );
    fs2 = fopen( argv[3], "r" );
 
    if( fs1 == NULL || fs2 == NULL )
     {
       perror("Error ");
       exit(EXIT_FAILURE);//EXIT_FAILURE -- unsuccessful termination

     }
 
    ft = fopen(argv[1],"w");
 
    if( ft == NULL )
     {
       perror("Error ");
       exit(EXIT_FAILURE);
     }
 
    while( fscanf(fs1,"%c", &ch) != EOF )
    fprintf(ft,"%c", ch);
      
    while( fscanf(fs2,"%c", &ch)  != EOF )
       fprintf(ft,"%c", ch);
  
 
    printf("Two files were merged into %s file successfully.\n",argv[1]);
 
    fclose(fs1);
    fclose(fs2);
    fclose(ft);  
  
   }
   return 0;
}

Thursday 9 February 2012

Important concept covered in class

can be in a walkthrough:-

a**b   ---->  a*(*b)  ----->  a * (where b is pointing to)

Todays first Challenging Question ans

One line answer for validation function

int validMonth(int mon, char* errmes){
  return((mon>0 && mon <=12) || !strcpy(errmes, "Invalid month!"));
}

Monday 9 January 2012

Hi,
Welcome to my blog.
It was wonderful experience with OOP244.  I am looking forward to have the same experience with OOP344.