Archive for the ‘Uncategorized’ Category

#include <iostream>
using namespace std;

#include "tqueue.h"

template <class T>
void print(Tqueue<T>& Q){
  bool done = false;
  Q.goHead();
  cout<<"NULL <- [HEAD] ";
  if(!Q.isEmpty()){
    do{
      cout<<Q.visit();
      if(Q.goNext()) cout<<" <=> ";
      else{
        done = true;
      }
    }while(!done);
  }
  cout<<" [TAIL] -> NULL"<<endl;
  cout<<endl<<endl;
}

int main(){
  Tqueue<int> Q;
  cout<<"Before Data to be Added:"<<endl;
  print(Q);

  for(int i=1;i<=6;i+=1){
    Q.append((rand()%rand())%100+1);
  }
  cout<<"After Data Added Ramndomly:"<<endl;
  print(Q);

  cout<<"After Assending Order:"<<endl;
  Q.sort();
  print(Q);
  cout<<"After Dessending Order:"<<endl;
  Q.sort(0);
  print(Q);

  cout<<"Let's POP some data:"<<endl;
  cout<<Q.pop()<<endl;
  cout<<Q.pop()<<endl;
  cout<<Q.pop()<<endl<<endl;
  cout<<"After Poped some data:"<<endl;
  print(Q);

  cout<<"After Pushed some data:"<<endl;
  Q.push(111);
  Q.push(555);
  Q.push(999);
  print(Q);

  Q.append(55);
  cout<<"AFTER APPENDED A DATA:"<<endl;
  print(Q);


  cout<<"MOVE CURRENT TO SECOND NODE (if exist) AND USE INSERTBEFORE TO ADD 1234:"<<endl;
  Q.goHead();
  Q.goNext();
  Q.insertBefore(1234);
  print(Q);

  cout<<"MOVE CURRENT TO FIFTH NODE (if exist) AND USE INSERTAFTER TO ADD 1000:"<<endl;
  Q.goHead();
  Q.goNext();
  Q.goNext();
  Q.goNext();
  Q.goNext();
  Q.insertAfter(1000);
  print(Q);

  cout<<"MOVE CURRENT TO SECOND NODE (if exist) AND REMOVE:"<<endl;
  Q.goHead();
  Q.goNext();
  Q.removeCurrent();
  print(Q);

  getchar();
  return 0;
}

Priority Queue

Posted: March 5, 2012 in Uncategorized

QUEUE.CPP

#include "queue.h"
Node::Node(int data, Node* next){
  _data = data;
  _next = next;
}
Queue::Queue(){
  _head = (Node*)0;
}
Queue::~Queue(){
  while(!isEmpty()) remove();
}
int Queue::remove(int pos){
  int ret = 0;
  Node* cur = _head;
  Node* prev = (Node*)0;
  bool flag = false;
  int i = 1;
  while(cur && i != pos){
    prev = cur;
    cur = cur->_next;
    i++;
  }
  if (i == pos && cur){
    ret = cur->_data;
    if (!prev){
      _head = _head->_next;
      delete cur;
    }
    else{
      cur = cur->_next;
      delete prev->_next;
      prev->_next = cur;
    }
  }
  return ret;
}
bool Queue::add(int val, int pos){
  Node* cur = _head;
  Node* prev = (Node*)0;
  bool flag = false;
  int i = 0;
  while(cur && i != pos){
    prev = cur;
    cur = cur->_next;
    i++;
  }
  if(pos == -1) pos = i;
  if(i == pos){
    if (i == 0){
      _head = new Node(val, _head);
      flag = true;
    }
    else{
      prev->_next = new Node(val, cur);
      flag = true;
    }
  }
  return flag;
}
bool Queue::isEmpty(){
  return !_head;
}

QUEUE.H

#ifndef __TV_QUEUE_H__
#define __TV_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();
  virtual ~Queue();
  int remove(int pos=1);
  bool add(int, int pos = -1);
  bool isEmpty();
};

QUEMAIN.CPP

#include <iostream>
using namespace std;
#include "queue.h"
int main(){
  Queue Q;
  for(int i = 10;i<101;i+=10){
    Q.add(i);
  }
  cout<<"Removed the "<<Q.remove(1)<<endl;
  cout << "append at 9th is: ";
  cout<<(Q.add(2222, 9)?"SUCCESS":"FAIL")<<endl;
  while(!Q.isEmpty()){
    cout<<Q.remove()<<endl;
  }
  getchar();
  return 0;
}

CIRCULAR LEFT SHIFT

Posted: March 5, 2012 in Uncategorized

QUESTION

A circular left shift removes the bits from left and inserts them to right side of the integer:

1001 1100  after one circular shift to left will be:  0011 1001

void CirShiftLeft(unsigned int* val, int NoOfShifts);

Example (assuming int size is 2 bytes):

unsigned int a = 0xB52A; /*1011010100101010 */
CirShiftLeft(&a, 3);
Printf(“%X”, a);
Output:

A955    that is: 1010100101010101

void CirShiftLeft(unsigned int* val, int NoOfShifts){
  char bin[80]="";
  int i = 0;
  int tmp = *val;
  while(tmp){
    bin[i++] = (tmp % 2) ;
    tmp /= 2;
  }
  for(int j = i - 1 - NoOfShifts; j >= 0 ; j--){
    tmp = tmp * 2 + bin[j];
  }
  for(int j = i-1; j > i - 1 - NoOfShifts  ; j--){
    tmp = tmp * 2 + bin[j];
  }
  *val = (unsigned int)tmp;
}

TEST1 – 20113 Q1

Posted: March 1, 2012 in Uncategorized
#include <cstdio>


void concat(int no, char** file) {
  FILE* fp1 = fopen(file[1], "w");
  FILE* fp2;      
  int i;
  char ch;

  for(i = 2; i < no ; i++){
    fp2 = fopen(*(file+i), "r");
    while((ch=fgetc(fp2))!=EOF){
      fputc(ch, fp1);
    }
    fclose(fp2);
  }
  fclose(fp1);
}

    
int main(int argc, char** argv) {
  if(argc < 3)
    printf("Usage: %s DestFile SrcFile1 [SrcFile2 SrcFile3 ...]<ENTER>\n", *argv);
  else
    concat(argc, argv);
  return 0;
}

COLOR Console

Posted: March 1, 2012 in Uncategorized

Test 1 – 20113 Q2 and Q3

Posted: March 1, 2012 in Uncategorized
#include <cstdio>
#include <cstring>



//20113 Answer to question 2
void getStr(char *str, int (*IsValid)(const char*), const char* prompt){
  int done = false;
  while(!done){
    printf(prompt);
    scanf("%s", str);
    if(IsValid && IsValid(str)){
      done = 1;
      printf("Hello! yourphone number is: %s\n", str);
    }
  }
}

//20113 Answer to question 3
int ValidPhone(const char* st){
  int flag = true;
  int i;
  if (strlen(st)!=10) flag = false;
  for(i = 0; flag && st[i] != '\0' && st[i] >= '0' && st[i] <= '9'; i++);
  if (!flag && i != 10) flag = false;
  return flag;
}

int main() {
  char string[80];
  getStr(string, ValidPhone, "Please enter your phone: ");
  return 0;
}