Tuesday, March 20, 2012

MinGW Simple Installation Guide

MinGW is a minimal GNU development environment for MS Windows (http://www.mingw.org/Welcome_to_MinGW_org). On MinGW, MSYS provides a collection of GNU tools, which provides some of the GNU utilities and enables the use of autotools build system (http://www.mingw.org/wiki/MSYS).

Installation:

1. Main program: Install the MinGW from: http://sourceforge.net/projects/mingw/files/
2. Set system variable PATH to include the X:\MinGW\bin; where X is the root drive
3. mingw-get: Install the mingw-get-inst from
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20111118/
4. Open a CMD console on Windows:
  1) gmake, gcc, g++, gdb: Install the make, compiler and debuger
  2) msys: Install the GNU utilities collection tool - msys.bat
 
 > mingw-get install gcc g++ gmake gdb msys
 > cd X:\MinGW\msys\1.0\postinstall
 > pi.bat
 > X:\MinGW\msys\1.0\msys.bat


Done!

Sunday, March 4, 2012

Simulating a Queue using two Stacks

// Simulating a queue by two stacks, one for enqueue, another for dequeue
#include <iostream>
#include <stack>
#include <queue>

using std::stack;
using std::queue;
using std::cin;
using std::cout;
using std::endl;
using std::boolalpha;

template<class T>
class squeue {
public:
 T& back()
 {
  assert (!empty());
  while (!_s2.empty())
  {
   _s1.push(_s2.top());
   _s2.pop();
  }
  return _s1.top();
 }
 
 T& front()
 {
  assert (!empty());
  while (!_s1.empty())
  {
   _s2.push(_s1.top());
   _s1.pop();
  }
  return _s2.top();
 }

 void push(const T& e) //enqueue
 {
  while (!_s2.empty())
  {
   _s1.push(_s2.top());
   _s2.pop();
  }
  _s1.push(e);
 }

 void pop() //dequeue
 {
  assert (!empty());
  while (!_s1.empty())
  {
   _s2.push(_s1.top());
   _s1.pop();
  }
  _s2.pop();
 }

 size_t size() const
 {
  return _s1.size() + _s2.size();
 }
 
 bool empty() const
 {
  return size() == 0;
 }
private:
 stack<T> _s1, _s2;
};

int main()
{ 
 squeue<int> sq;
 queue<int> q; // the control
 
 // push, back
 for (size_t i=0; i<5; i++)
 {
  sq.push(i); q.push(i);
  cout << sq.back() << " " << q.back() << "\n";
 }
 
 // size
 cout << "\n" << sq.size() << " " << q.size() << "\n\n";
 
 // front, pop
 for (size_t i=0; i<5; i++)
 {
  cout << sq.front() << " " << q.front() << "\n";
  sq.pop(); q.pop();
 }
 
 // empty
 cout << boolalpha << "\n" << sq.empty() << " " << q.empty() << endl;
 
 return 0;
}