Tuesday, October 26, 2010

Queue Template

The queue template I made. Overflow conditions have been skipped as they practically dont occur on modern PC's.

Please specify class T when you declare the object, for example for 'int',
queue type_name > queue_object_name;

template class T >
class queue
{

   struct node{
   T data;
   struct node *next;
   }*frnt,*rear;

public:
queue()
{
                  frnt=rear=NULL;
}

void insert(const T & value)
{
   struct node *ptr;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(frnt==NULL)
                   frnt=ptr;
   else
                   rear->next=ptr;
   rear=ptr;
   cout<<"\nNew item is inserted to the Queue!!!";
   getch();
}

T del()
{
   if(frnt==NULL)
   {
                   cout<<"\nQueue is empty!!";
                   getch();
                   return;
   }
   struct node *temp;
   temp=frnt;
   frnt=frnt->next;
   T t=temp->data;
   delete temp;
   return t;
}

void show()
{
   struct node *ptr1=frnt;
   if(frnt==NULL)
   {
                   cout<<"The Queue is empty!!";
                   getch();
                   return;
   }
   cout<<"\nThe Queue is\n";
   while(ptr1!=NULL)
   {
                    cout<data<<" ->";
                    ptr1=ptr1->next;
   }
}
};

Sunday, September 19, 2010

Windows 7 and the Inevitble Headache of 32-bit C++

Windows 7 is here,
Windows 7 is the future,
Windows 7 doesnt support my C++!!!!!

TurboC is definitely functional but NOT COMPLETELY. This is alarming, but most conventional compilors and IDE's that worked on the traditional 16-bit environment will not work anymore, atleast not properly. The more advanced the OS becomes, the more detached it becomes from its roots, or so is the trend these days...


This means we cant use the good old c++ we learnt in school anymore, as the current IDE's that are based on 32-bit systems use Mingw Compilor or Minimalist GNU for Windows. These are some problems you will encounter.. 

★ Mingw doesnt support many headers that we would be familiar with, like conio.h, graphics.h, stdio.h. Instead, trimmed versions may be available of conio and stdio depending on where you downloaded it from.
REASON - Well all these headers (and more) are implementations by Borland and are not required to be present on every Compilor we see, virtually sending us in Coder's Hell.
SOLUTION - It took me 30 minutes to search for a Mingw-compatible conio header that was complete. Same effort was required to find graphics libraries similar to what we used in TurboC++. A good alternative was the Devpak called WinBGIm.

★ Another problem is the "iostream" header file, firstly, you cant refer to any variables like cin or cout just like that, they throw errors as if they were never there(declared).
REASON - C++ namespaces allows us to group a set of global classes, objects and/or functions under a name. Namespace std contains all the classes, objects and functions of the standard C++ library. Borland allowed users to write code with Borland specific libraries without mentioning namespaces but now this habbit will have to change for good.
SOLUTION - Learn to live with it. Either use std:: everywhere or specify to the compiler that you are using namespace 'std', as given in the following code...
#include <iostream>
using namespace std;
int main()
{
      cout << "Hello";
      return 0;
}

 Another problem that needs a special mention..."GRAPHICS"!!!, its different from what most coders have done, although it retains many functions from the original "graphics.h", so do read the documentation on WinBGIm before venturing any further.

Besides these there will be minor adjustment  and coding issues for those who were used to TurboC.

Thursday, August 26, 2010

LinkLists in C++: Part 4

Fourth in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;

void reverse()
{
int n;
t = start;
while(t != NULL)
{
n++;
t = t->next;
}
for(int i = 0;i <= n-3;i++)
{
t = start;
while(t->next->next->next != NULL)
t = t->next;
t->next->next->next = t->next;
t->next->next = NULL;
}
t = start;
t->next->next = start;
t->next = NULL;
t1 = start;
start = end;
end = t1;
}

void sort()
{
t = start;
while(t != NULL)
{
t1 = start;
while(t1 != NULL)
{
if(t1->next->info > t1->next->next->info)
{
t1->next = t1->next->next;
t1->next->next = t1->next->next->next;
t1->next->next->next = t1->next;
}
t1 = t1->next;
}
t = t->next
}
}

LinkLists in C++: Part 3

Third in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;


void listnode()
{
t = start;
cout <<"\nTHE CONTENTS ARE\n";
while(t != NULL)
{
cout <info <
t = t->next;
}
}

void count()
{
t = start;
int ctr = 0;
while(t != NULL)
{
ctr++;
t = t->next;
}
cout <<"\nNUMBER OF NODES ARE" <
}

void search()
{
t = start;
int n, flag = 0;
cout <<"\nENTER INFO TO BE SEARCHED ::";
cin >>n;
while(t!= NULL)
{
if(t->info == n)
{
flag = 1;
break;
}
}
if(flag == 0)
cout <<"\nNOT FOUND";
else
cout <<"\nFOUND";
}

LinkLists in C++: Part 2

Second in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;



void delnode()
{
int ch;
cout <<"\n1.DELETE IN THE BEGINNING.";
cout <<"\n2.DELETE AT THE END.";
cout <<"\n3.DELETE IN THE MIDDLE.";
cout <<"\nENTER CHOICE";
cin >> ch;
switch(ch)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delmid();
break;
default:cout <<"\nWRONG CHOICE";
}
}

void delbeg()
{
t = start->next;
start->next = NULL;
delete start;
start = t;
}

void delend()
{
t = start;
while(t->next->next != NULL)
t=t->next;
t->next = NULL;
delete end;
end = t;
}

void delmid()
{
int n;
cout <<"\nENTER THE INFO TO BE DELETED";
cin >>n;
t = start;
while(t != NULL)
{
if(t->next->info == n)
{
t1 = t->next;
t->next = t1->next;
t1->next = NULL;
delete t1;
break;
}
t = t->next;
}
}

LinkLists in C++: Part 1



First in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;

void addnode()
{
int ch;
if(start == NULL)
{
start = new node;
if(start == NULL)
{
cout <<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO";
cin >>start->info;
start->next = NULL;
end = start;
}
else
{
cout <<"\n1.ADD IN THE BEGINNING.";
cout <<"\n2.ADD AT THE END.";
cout <<"\n3.ADD IN THE MIDDLE.";
cout <<"\n4.EXIT.";
cout <<"\n\nENTER CHOICE ::";
cin >> ch;
switch(ch)
{
case 1:addbeg();
break;
case 2:addend();
break;
case 3:addmid();
break;
case 4:exit(0);
break;
default:cout <<"WRONG CHOICE.";
}
}
}

void addbeg()
{
t = new node;
if(t == NULL)
{
cout <<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO :: ";
cin >>t->info;
t->next = start;
start = t;
}

void addend()
{
t = new node;
if(t == NULL)
{
cout <<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO :: ";
cin >>t->info;
t->next = NULL;
end->next = t;
end = t;

}

void addmid()
{
int ch;
cout <<"\n1.ADD BEFORE A GIVEN NODE.";
cout <<"\n2.ADD AFTER A GIVEN NODE.";
cout <<"\nENTER CHOICE :: ";
cin >> ch;
switch(ch)
{
case 1:addbefore();
break;
case 2:addafter();
break;
default:cout <<"\nWRONG CHOICE";
}
}

void addbefore()
{
int n;
cout <<"\nENTER THE NODE INFO.";
cin >>n;
t = start;
while(t != NULL)
{
if(t->next->info == n)
{
t1 = new node;
if(t1 == NULL)
{
cout<<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO";
cin >>t1->info;
t1->next = t->next;
t->next = t1;
}
t = t->next;
}
}

void addafter()
{
int n;
cout <<"\nENTER THE NODE INFO.";
cin >>n;
t = start;
while(t != NULL)
{
if(t->info == n)
{
t1 = new node;
if(t1 == NULL)
{
cout<<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO";
cin >>t1->info;
t1->next = t->next;
t->next = t1;
}
t = t->next;
}
}