Tuesday, August 19, 2008

Reverse Link-List

Without Recurtion :-)



struct node *p,*q,*r;
r=null;
p=head;
while(p)
{
q=p->next;
p->next=r;
r=p;
p=q;
}
head=p;




With recurtion :-)


list *tail; 
list *reverse (list *head)
{
if (head->next == NULL)
{
tail = head;
return;
}else{
reverse (head->next);
head->next->next = head;
head->next = NULL;
}
}

Monday, August 18, 2008

What Is Name Space ?

Namespaces


Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

The format of namespaces is:

namespace identifier
{
entities
}

Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example:

namespace myNamespace
{
int a, b;
}

In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order to access these variables from outside the myNamespace namespace we have to use the scope operator ::. For example, to access the previous variables from outside myNamespace we can write:

myNamespace::a
myNamespace::b

The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example:

// namespaces
#include
using namespace std;

namespace first
{
int var = 5;
}

namespace second
{
double var = 3.1416;
}

int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
5
3.1416

In this case, there are two global variables with the same name: var. One is defined within the namespace first and the other one in second. No redefinition errors happen thanks to namespaces.

using

The keyword using is used to introduce a name from a namespace into the current declarative region. For example:
// using
#include
using namespace std;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

int main () {
using first::x;
using second::y;
cout << x << endl;
cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}
5
2.7183
10
3.1416

Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:

// using
#include
using namespace std;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

int main () {
using namespace first;
cout << x << endl;
cout << y << endl;
cout << second::x << endl;
cout << second::y << endl;
return 0;
}
5
10
3.1416
2.7183

In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like:

// using namespace example
#include
using namespace std;

namespace first
{
int x = 5;
}

namespace second
{
double x = 3.1416;
}

int main () {
{
using namespace first;
cout << x << endl;
}
{
using namespace second;
cout << x << endl;
}
return 0;
}
5
3.1416

Namespace alias

We can declare alternate names for existing namespaces according to the following format:

namespace new_name = current_name;

Namespace std

All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

Sunday, August 17, 2008

For Placements


---------- Forwarded message ----------
From: mittal <sunny88mittal@gmail.com>
Date: Tue, Aug 5, 2008 at 10:30 AM
Subject: Best of luck
To: Thapar BE 2005 CS <thaparbe2005cs@googlegroups.com>



Best of luck to everybody for the placements and i am uploading an
aptitude file for our technical subjects go through it it will help
you in interviews!!!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Thapar BE 2005 CS" group.
To post to this group, send email to thaparbe2005cs@googlegroups.com
To unsubscribe from this group, send email to thaparbe2005cs+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.co.in/group/thaparbe2005cs?hl=en
-~----------~----~----~----~------~----~------~--~---


What Is Template ?

Templates


Function templates

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.

The format for declaring function templates with type parameters is:

template function_declaration;
template function_declaration;

The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.


For example, to create a template function that returns the greater one of two objects we could use:

template <class myType>
myType GetMax (myType a, myType b) {
return (a>b?a:b);
}
Here we have created a template function with myType as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. As you can see, the function template GetMax

returns the greater of two parameters of this still-undefined type.

To use this function template we use the following format for the function call:

function_name (parameters);

For example, to call GetMax to compare two integer values of type int we can write:

int x,y;
GetMax <int> (x,y);

When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of myType by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.